Back to

Package sjs

Latest Go to latest

The highest tagged major version is .

Published: Apr 30, 2020 | License: ISC | Module:



const (
	JobStatusPaused  = "paused"
	JobStatusRunning = "running"

func Schedule

func Schedule(
	ctx context.Context,
	workerMap *WorkerMap,
	j *Job,
	errCh *OptErr,

Schedule a job to run.

type Heartbeat

type Heartbeat struct {
	NotifyURL string
	Jobs      []*JobData

Heartbeat sent from Clients to the Service.

type Job

type Job struct {
	ID        int
	CreatedAt time.Time

	// Name describes the type of job, for instance, "calculateInvoice" or
	// "sendInvoice". Workers will subscribe to jobs from that server.
	Name JobName

	// LastRun indicates when the job last started. If a job has never been
	// run, this is nil.
	LastRun *time.Time

	// RunEvery describes the interval on which to run the job.
	RunEvery int

	// RunEveryPeriod can be "second" or "day of month".
	RunEveryPeriod JobPeriod

	// TimeoutInSeconds is the max length of time that a specific job's
	// execution is allowed before it's canceled. If nil, the job may run
	// forever.
	TimeoutInSeconds *int

	// PayloadData included every time sjs notifies a worker.
	PayloadData []byte

	// JobStatus indicates whether the job is running or paused.
	JobStatus JobStatus

func JobFromData

func JobFromData(jd *JobData) (*Job, error)

JobFromData converts JobData to a job and validates the job, reporting any errors.

func (*Job) Valid

func (j *Job) Valid() error

Valid reports whether a job is valid or not. If invalid, this reports an error describing the validation issue.

type JobData

type JobData struct {
	Name             JobName
	RunEvery         int
	RunEveryPeriod   JobPeriod
	TimeoutInSeconds int
	JobStatus        JobStatus
	PayloadData      []byte

JobData is sent when registering worker capabilities. This enables the creation of jobs with that. Using a zero value for TimeoutInSeconds is treated as no timeout. When created, jobs default to running.

type JobName

type JobName string

type JobPeriod

type JobPeriod string

JobPeriod determines how often the job should be run. Second indicates that the job should run every X seconds. Day of month indicates that the job should run on every X day of the month, such as the Jan 1st, Feb 1st, Mar 1st, etc.

const (
	JobPeriodSecond     JobPeriod = "second"
	JobPeriodDayOfMonth           = "dayOfMonth"

type JobResult

type JobResult struct {
	JobID     int
	Succeeded bool
	StartedAt time.Time
	EndedAt   time.Time

	// ErrMessage is nil if the job succeeded.
	ErrMessage *string

JobResult represents the result of a particular job. Any job will have 1 or more JobResults from prior runs.

type JobStatus

type JobStatus string

type Logger

type Logger interface {
	Printf(s string, args ...interface{})

type OptErr

type OptErr struct {
	C chan error

OptErr is a channel that we can modify in a threadsafe way. The mutex is required to read or modify the underlying channel, which can be created at any time by the caller of the library. The exported "C" fieldname representating the channel is inspired by the standard library's time.Ticker design.

func (*OptErr) Send

func (o *OptErr) Send(err error)

type OptLogger

type OptLogger struct{ Log Logger }

func (*OptLogger) Printf

func (o *OptLogger) Printf(s string, args ...interface{})

type Worker

type Worker struct {
	NotifyURL string
	Jobs      []*Job
	APIKey    string
	// contains filtered or unexported fields

func (*Worker) Run

func (w *Worker) Run(ctx context.Context, j *Job) error

type WorkerMap

type WorkerMap struct {
	// contains filtered or unexported fields

WorkerMap maps job names to workers. It is thread-safe.

func NewWorkerMap

func NewWorkerMap() *WorkerMap

func (*WorkerMap) AddWorker

func (m *WorkerMap) AddWorker(
	ctx context.Context,
	lg *OptLogger,
	w *Worker,
	errCh *OptErr,

func (*WorkerMap) GetOrCreateWorkerForNotifyURL

func (m *WorkerMap) GetOrCreateWorkerForNotifyURL(ul string) *Worker

GetOrCreateWorkerForNotifyURL does not add the worker to the WorkerMap. That happens when the worker is fully assembled by the calling function. This is threadsafe and never returns nil.

func (*WorkerMap) GetWorkerForJobName

func (m *WorkerMap) GetWorkerForJobName(name JobName) *Worker

GetWorkerForJobName is not threadsafe. Guard the underlying WorkerMap with a lock.

func (*WorkerMap) PurgeWorkersEvery

func (m *WorkerMap) PurgeWorkersEvery(dur time.Duration)

func (*WorkerMap) Workers

func (m *WorkerMap) Workers() []*Worker

Workers returns a slice of all workers active for the job server.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier