Skip to content

JobSource

do- edited this page Aug 11, 2025 · 72 revisions

JobSource is an ancestor to factories producing Jobs bound to a given Application and all having some common nature, for instance:

Basically, this is a collection of event handers and other options that are set to a job at the moment of creation.

Constructor

This class is considered abstract parent, so the constructor should be only called through super:

super (app, options)

Parameters

Name Description
app an Application instance.
options a bag of options (see below)

Options

Name Type Default Description
name String The unique name with which this JobSource is registered in the hosting Application
globals Object {...app.globals, src: this, logger: this.logger} an object of properties to be injected in each new job as is. For function valued properies, this works like using mixins.
generators Object {...app.generators} an object of functions to calculate properties (called without any parameters) injected in each new job
pools Object {...app.pools} an object of ResourcePools to inject proxies to necessary shared resources in each new job
on Object {...app.handlers} a bag of event handlers
logger app.logger A winston Logger
request Object {} request parameters, common to all jobs produced
lag Number or Array or Lag 0 The minimum time between the constructor call and the finished event
maxLatency Number Infinity the maximum time between since the job creation to the end or error event, in milliseconds
maxPending Number Infinity the maximum number of jobs pending in parallel

Properties

Name Type Description Notes
pending Set created jobs still in progress
capacity Number computed as maxPending - pending.size: the number of jobs allowed to be created at once
tracker Tracker listening to the job's events and registering it with logger In the base class, without [Tracker.LOGGING_EVENTS] defined, does nothing
[Tracker.LOGGING_ID] String computed as name

Methods

Public

createJob (request = {}, options = {})

Returns a new Job instance with the request provided (with this.request prepended), parent set from options (if any) and globals, generators, pools, handlers applied.

If maxPending is exhausted by pending.size, throws a JobSource.OverflowError.

If the numeric value of lag is infinite (which normally means that the Lag instance observed the maximum consecutive errors allowed), throws a JobSource.LockedError.

onJobInit (job)

This asynchronous method is executed prior to init for every Job created.

onJobEnd (job)

This asynchronous method is executed just after handling the end event for every Job emitting it.

onJobError (job)

This asynchronous method is executed just after handling the error event for every failed Job.

reset ()

Emits the reset event. Makes sense to notify lag if it's a Lag instance.

Internal

addHandler (event, handler)

Adds an event handler to an internal collection.

Parameters

Name Description
event a String, one of Job's event names.
handler a job => {...} function to be set as handler

getJobLoggingDetails (job)

Returns the [Tracker.LOGGING_DETAILS] property for the job. In the base implementation, it's {request: job.request, user: job.user}

Static members

Name Description
JobSource.LockedError the class of error thrown when lag is infinite
JobSource.OverflowError the class of error thrown when maxPending is exhausted

Events

Name When emitted Payload
job-start When a new job emits start The job
job-end When a job emits end The job
job-error When a job emits error The job
job-finish When a job emits finish The job
job-next When a job emits next The job
reset When reset () is called
Clone this wiki locally