This commit is contained in:
@@ -72,35 +72,49 @@ func (h *JobsHandlers) AddJob(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
agent, ok := h.tracker.GetAgent(in.AgentID)
|
||||
if !ok {
|
||||
c.Status(http.StatusNotFound)
|
||||
c.Error(fmt.Errorf("agent not found"))
|
||||
return
|
||||
}
|
||||
|
||||
command, err := resolveCommand(c, h.svc, in.InterpreterID, in.Command)
|
||||
result, err := h.runCommand(c, in.AgentID, in.InterpreterID, in.Command, in.Stdin)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, result)
|
||||
}
|
||||
|
||||
// runCommand resolves command, submits a job to the agent, and returns AddJobOut.
|
||||
// Shared between jobs and scripts handlers.
|
||||
func (h *JobsHandlers) runCommand(
|
||||
c *gin.Context,
|
||||
agentID string,
|
||||
interpID int64,
|
||||
command string,
|
||||
stdin *string,
|
||||
) (*AddJobOut, error) {
|
||||
agent, ok := h.tracker.GetAgent(agentID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("agent not found")
|
||||
}
|
||||
|
||||
cmd, err := resolveCommand(c, h.svc, interpID, command)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jid, err := agent.AddJob(models.JobForInsert{
|
||||
Command: command,
|
||||
Stdin: in.Stdin,
|
||||
Command: cmd,
|
||||
Stdin: stdin,
|
||||
})
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
waitURL := fmt.Sprintf("%s/api/v1/jobs/%d/wait", h.whereami, jid)
|
||||
|
||||
c.JSON(http.StatusCreated, AddJobOut{
|
||||
return &AddJobOut{
|
||||
ID: jid,
|
||||
Command: command,
|
||||
Command: cmd,
|
||||
WaitURL: waitURL,
|
||||
})
|
||||
}, nil
|
||||
}
|
||||
|
||||
// WaitJob waits for a submitted job to complete (long-poll).
|
||||
|
||||
Reference in New Issue
Block a user