@@ -23,7 +23,11 @@ func (r *JobRepository) Init(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *JobRepository) InitJob(ctx context.Context, agentID string, job models.JobForInsert) (int64, error) {
|
||||
func (r *JobRepository) InitJob(
|
||||
ctx context.Context,
|
||||
agentID string,
|
||||
job models.JobForInsert,
|
||||
) (int64, error) {
|
||||
commandJSON, err := json.Marshal(job.Command)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("marshal command: %w", err)
|
||||
@@ -34,9 +38,12 @@ func (r *JobRepository) InitJob(ctx context.Context, agentID string, job models.
|
||||
stdinVal = job.Stdin
|
||||
}
|
||||
|
||||
result, err := r.DB.ExecContext(ctx,
|
||||
result, err := r.DB.ExecContext(
|
||||
ctx,
|
||||
`INSERT INTO jobs (agent_id, command, stdin, stdout, stderr, status) VALUES (?, ?, ?, '', '', 0)`,
|
||||
agentID, string(commandJSON), stdinVal,
|
||||
agentID,
|
||||
string(commandJSON),
|
||||
stdinVal,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -45,10 +52,18 @@ func (r *JobRepository) InitJob(ctx context.Context, agentID string, job models.
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func (r *JobRepository) UpdateJobInDB(ctx context.Context, jid int64, msg models.JobForUpdate) (models.Job, error) {
|
||||
result, err := r.DB.ExecContext(ctx,
|
||||
func (r *JobRepository) UpdateJobInDB(
|
||||
ctx context.Context,
|
||||
jid int64,
|
||||
msg models.JobForUpdate,
|
||||
) (models.Job, error) {
|
||||
result, err := r.DB.ExecContext(
|
||||
ctx,
|
||||
`UPDATE jobs SET stdout = ?, stderr = ?, status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||
msg.Stdout, msg.Stderr, msg.Status, jid,
|
||||
msg.Stdout,
|
||||
msg.Stderr,
|
||||
msg.Status,
|
||||
jid,
|
||||
)
|
||||
if err != nil {
|
||||
return models.Job{}, err
|
||||
@@ -81,10 +96,10 @@ func (r *JobRepository) GetJobByID(ctx context.Context, jid int64) (models.Job,
|
||||
return models.Job{}, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal([]byte(commandJSON), &job.JobForInsert.Command); err != nil {
|
||||
if err := json.Unmarshal([]byte(commandJSON), &job.Command); err != nil {
|
||||
return models.Job{}, fmt.Errorf("unmarshal command: %w", err)
|
||||
}
|
||||
|
||||
job.JobForInsert.Stdin = stdinVal
|
||||
job.Stdin = stdinVal
|
||||
return job, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user