Something went wrong. Try again.
Shuttle runs GitLab CI pipelines for Tangled repositories on an ordinary GitLab runner, and publishes the job logs to the repository owner's tangled.org/dracoblue.de/shuttle
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239package queue
import ( "context" "errors" "strings" "sync")
var ( ErrEmpty = errors.New("queue: empty") ErrDuplicate = errors.New("queue: duplicate job"))
type Job struct { ID int64 `json:"id"` RepoDID string `json:"repo_did"` Ref string `json:"ref"` SHA string `json:"sha"` BeforeSHA string `json:"before_sha,omitempty"` JobName string `json:"job_name"` Status string `json:"status"` RepoURL string `json:"repo_url,omitempty"` Image Image `json:"image,omitempty"` Services []Image `json:"services,omitempty"` Variables []Variable `json:"variables,omitempty"` BeforeScript []string `json:"before_script,omitempty"` Script []string `json:"script,omitempty"` AfterScript []string `json:"after_script,omitempty"` Cache []Cache `json:"cache,omitempty"` Hooks []Hook `json:"hooks,omitempty"` CreatedAt string `json:"created_at,omitempty"` StartedAt string `json:"started_at,omitempty"` FinishedAt string `json:"finished_at,omitempty"` Stage string `json:"stage,omitempty"` StageIndex int `json:"stage_index,omitempty"` When string `json:"when,omitempty"` AllowFailure bool `json:"allow_failure,omitempty"` ExitCodes []int `json:"allow_failure_exit_codes,omitempty"` FailureAllowed bool `json:"failure_allowed,omitempty"` TimeoutSeconds int `json:"timeout_seconds,omitempty"` RetryMax int `json:"retry_max,omitempty"` RetryWhen []string `json:"retry_when,omitempty"` RetryExitCodes []int `json:"retry_exit_codes,omitempty"` Attempt int `json:"attempt,omitempty"` FailureReason string `json:"failure_reason,omitempty"` Coverage string `json:"coverage,omitempty"` Artifacts []Artifact `json:"artifacts,omitempty"` Dependencies []Dependency `json:"dependencies,omitempty"` // Tags are the expanded job tags. Empty means the job is untagged. Tags []string `json:"tags,omitempty"`}
type Artifact struct { Name string `json:"name"` Untracked bool `json:"untracked"` Paths []string `json:"paths"` Exclude []string `json:"exclude"` When string `json:"when"` Type string `json:"artifact_type"` Format string `json:"artifact_format"` ExpireIn string `json:"expire_in"`}
type Dependency struct { ID int64 `json:"id"` Name string `json:"name"` Token string `json:"token,omitempty"` ArtifactsFile DependencyArtifactsFile `json:"artifacts_file"`}type DependencyArtifactsFile struct { Filename string `json:"filename"` Size int64 `json:"size"`}
type Hook struct { Name string `json:"name"` Script []string `json:"script"`}
type Cache struct { Key string `json:"key"` Untracked bool `json:"untracked"` Policy string `json:"policy"` Paths []string `json:"paths"` When string `json:"when"` FallbackKeys []string `json:"fallback_keys"`}
type Image struct { Name string `json:"name"` Alias string `json:"alias,omitempty"` Entrypoint []string `json:"entrypoint,omitempty"` Command []string `json:"command,omitempty"` Variables []Variable `json:"variables,omitempty"` PullPolicies []string `json:"pull_policy,omitempty"` ExecutorOptions *ExecutorOptions `json:"executor_opts,omitempty"`}
type ExecutorOptions struct { Docker *DockerOptions `json:"docker,omitempty"`}
type DockerOptions struct { Platform string `json:"platform,omitempty"` User string `json:"user,omitempty"`}
type Variable struct { Key string `json:"key"` Value string `json:"value"` Public bool `json:"public"` Masked bool `json:"masked"` File bool `json:"file"` Raw bool `json:"raw"`}
type Queue interface { Enqueue(context.Context, Job) (Job, error) Claim(context.Context) (Job, error) // ClaimMatching claims the first pending job for which can returns true. // A nil can matches every job. ClaimMatching(context.Context, func(Job) bool) (Job, error) Ack(context.Context, int64) error Requeue(context.Context, int64) error}
// RunnerCap is a runner's tag set and whether it accepts untagged jobs.type RunnerCap struct { Tags []string RunUntagged bool}
// RunnerCanRun reports whether a runner may take a job.// A runner must carry every job tag. Untagged jobs go only to runners with runUntagged.// Comparison is case-sensitive. Extra runner tags are allowed.func RunnerCanRun(jobTags, runnerTags []string, runUntagged bool) bool { if len(jobTags) == 0 { return runUntagged } have := make(map[string]struct{}, len(runnerTags)) for _, tag := range runnerTags { have[tag] = struct{}{} } for _, tag := range jobTags { if _, ok := have[tag]; !ok { return false } } return true}
// PendingReason explains why no registered runner can take the job.// An empty string means at least one runner can.func PendingReason(jobTags []string, runners []RunnerCap) string { for _, runner := range runners { if RunnerCanRun(jobTags, runner.Tags, runner.RunUntagged) { return "" } } if len(jobTags) == 0 { return "pending: no runner with run_untagged can take this untagged job" } return "pending: no runner has all tags [" + strings.Join(jobTags, ", ") + "]"}
type Memory struct { mu sync.Mutex nextID int64 pending []Job claimed map[int64]Job seen map[int64]struct{}}
func NewMemory() *Memory { return &Memory{nextID: 1, claimed: make(map[int64]Job), seen: make(map[int64]struct{})}}
func (q *Memory) Enqueue(_ context.Context, j Job) (Job, error) { q.mu.Lock() defer q.mu.Unlock() if j.ID == 0 { j.ID, q.nextID = q.nextID, q.nextID+1 } else if j.ID >= q.nextID { q.nextID = j.ID + 1 } if _, ok := q.seen[j.ID]; ok { return Job{}, ErrDuplicate } if j.Status == "" { j.Status = "pending" } q.seen[j.ID] = struct{}{} q.pending = append(q.pending, j) return j, nil}
func (q *Memory) Claim(ctx context.Context) (Job, error) { return q.ClaimMatching(ctx, nil)}
func (q *Memory) ClaimMatching(_ context.Context, can func(Job) bool) (Job, error) { q.mu.Lock() defer q.mu.Unlock() for i, j := range q.pending { if can != nil && !can(j) { continue } q.pending = append(q.pending[:i], q.pending[i+1:]...) j.Status = "running" q.claimed[j.ID] = j return j, nil } return Job{}, ErrEmpty}
func (q *Memory) Ack(_ context.Context, id int64) error { q.mu.Lock() defer q.mu.Unlock() if _, ok := q.claimed[id]; !ok { return ErrEmpty } delete(q.claimed, id) return nil}
func (q *Memory) Requeue(_ context.Context, id int64) error { q.mu.Lock() defer q.mu.Unlock() j, ok := q.claimed[id] if !ok { return ErrEmpty } delete(q.claimed, id) j.Status = "pending" q.pending = append(q.pending, j) return nil}