Files
threshold/internal/scheduler/job/doc.go
T
Hugo 27c432a748 feat(scheduler): Drop completed job runs as they complete
Before this change, upon the completion of a job run, the scheduler
would update a job_run entry with the completed status as well as the
number of objects affected by the run. Additionally, we then had a
cleaner job whose scope was deleting these completed entries from
job_run.

This commit changes this design to drop the cleaner job the and rolls
its functionality directly into the scheduler. Now, when any given job
run is complete, the scheduler will automatically delete it from the
job_run table itself.

In addition, the completed status has been removed as a valid status as
it is not set anymore.

This change follows a decision that given the data in these job_run
entries is already being deleted by the cleaner job, updating its state
when a job completes just to have it deleted is not efficient.
Additionally, no use-case for this data was surfaced.
2024-11-05 17:10:39 +00:00

50 lines
1.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
// Package job provides a Job and a Run struct suitable for persisting to
// the repository.
//
// A Job represents work that should be run at a predetermined time and needs to be synchronized
// across servers to ensure that one and only one instance of a job is running at any given time.
// The job's name must be unique to the plugin that registered the job, the (plugin_id, name)
// uniqueness is enforced by the database.
//
// A Run represents a single execution of a job, only a single run can be in a
// running state for a specific job. The private_id (primary key) is generated by the
// repository during RunJobs.
//
// # Repository
//
// A repository provides methods for creating, updating, retrieving, and
// deleting Jobs. It also provides methods to run jobs, update progress of runs
// and complete, fail or interrupt runs.
// A new repository should be created for each transaction. For example:
//
// var wrapper wrapping.Wrapper
// ... init wrapper ...
//
// // db implements both the reader and writer interfaces.
// db, _ := db.Open(db.Postgres, url)
//
// var repo *job.Repository
// repo, _ = job.NewRepository(db, db, wrapper)
//
// var j *job.Job
// j, _ = repo.UpsertJob(context.Background(), j, "name", "description")
//
// var runs []*Run
// repo, _ = job.NewRepository(db, db, wrapper)
// runs, _ = repo.RunJobs(context.Background(), "serverId")
//
// ... run job ...
//
// var run *Run
// repo, _ = job.NewRepository(db, db, wrapper)
// run, _ = repo.UpdateProgress(ctx, run, []string{"TotalCount", "CompletedCount"})
//
// nextJobRun = time.Now().Add(time.Hour)
//
// repo, _ = job.NewRepository(db, db, wrapper)
// _ = repo.CompleteRun(ctx, run.PrivateId nextJobRun)
package job