github.com/rotari-dev/rotari/internal/jobcontrol

Overview

package jobcontrol // import "github.com/rotari-dev/rotari/internal/jobcontrol"

Package jobcontrol cancels, suspends, and resumes a project's running jobs.

It finds the running run through the project's run lock, resolves job and
attempt selections against it, and signals each job through the executor that
owns it. It refuses to signal local processes from a host other than the
one that started them. It should not decide run planning or finalization;
a cancelled run finalizes through the normal run path.

Constants

const (
	StateRunning = "running"
	StatePending = "pending"
)
    Job states that Selection.States names.

Variables

var States = map[string][]string{
	"cancel":  {StateRunning, StatePending},
	"suspend": {StateRunning},
	"resume":  {StateRunning},
}
    States are the job states each operation acts on: cancel reaches pending
    jobs too, and suspend and resume only running ones.

Types

type CancelSource string
    CancelSource names who asked for a cancel, as a run's cancels.jsonl records
    it.

const (
	// CancelFromCLI is rotari cancel.
	CancelFromCLI CancelSource = "cli"
	// CancelFromWait is Ctrl-C in wait or in an attached run.
	CancelFromWait CancelSource = "wait"
	// CancelFromDisconnect is a supervisor acting on a client that
	// disconnected with the cancel disconnect action.
	CancelFromDisconnect CancelSource = "disconnect"
	// CancelFromWeb is the Web UI's Cancel job or Cancel run.
	CancelFromWeb CancelSource = "web"
	// CancelFromMCP is the MCP control_jobs tool.
	CancelFromMCP CancelSource = "mcp"
)
    Cancel sources.

type Controller struct {
	Store     state.Store
	Executors executor.Registry
}
    Controller signals jobs through a registry of executors.

func (controller Controller) Cancel(baseDir, project, runID string, jobIDs []string, wait bool, source CancelSource) (string, error)
    Cancel cancels the selected running jobs of project in baseDir, or its whole
    run when jobIDs is empty. A non-empty runID must be the project's active
    run, and an array job's ID selects its unfinished tasks. A whole-run cancel
    marks the queue as cancelling and cancels each unfinished job directly,
    whichever process calls it. With wait, it returns once the run lock is
    released.

    Each request, refused or not, is recorded in the run's cancels.jsonl with
    source, what it targeted, what it did to each job, and its error. A record
    that cannot be written is reported: as a warning in the message of a request
    that succeeded, and in the error of one that failed.

func (controller Controller) CancelJobs(runDir, project, runID string, jobIDs []string, source CancelSource) (string, error)
    CancelJobs cancels jobIDs in runDir, as Cancel does for those jobs,
    and records the request in runDir's cancels.jsonl with source. It does not
    check that runDir holds the project's active run; Cancel does.

func (controller Controller) Control(baseDir, project, runID string, jobIDs []string, operation string) (string, error)
    Control suspends or resumes, as named by operation, the selected running
    jobs of project in baseDir, or every running job when jobIDs is empty.
    A non-empty runID must be the project's active run. Attempt IDs must name
    the latest attempt of a running job, and an array job's ID selects its
    running tasks.

func (controller Controller) Select(baseDir, project, runID string, selection Selection, now time.Time) (string, []string, error)
    Select resolves project's active run, which must be runID when runID is not
    empty, and returns its ID and the sorted IDs of its unfinished jobs, array
    tasks one by one, that selection chooses. Durations of running jobs end at
    now. It fails when a name matches no job of the run or no job matches.

type Selection struct {
	Names  []string
	Scope  model.CommandSelector
	Filter jobfilter.Filter
	States []string
}
    Selection chooses unfinished jobs of a run by job name or by filters.
    Names select jobs whose name, or whose command's name, is one of them.
    Scope, Filter, and States narrow further; a job is running once its executor
    owns it and pending before, and empty States allow both.