21. Background Jobs
The background jobs mechanism, in its simplest implementation, involves running functions by timeout with a specific interval. These functions can use apiSys, which allows calling internal API on behalf of a system user and accessing any method of any class.
Typically, all logic for a background job is described directly in a class method, and the background job file only calls this method. For example, the Request_work.archiveClosed method might implement archiving of requests completed a certain time ago. The method can be called with a given frequency (once an hour, daily, etc.), select closed but not yet archived requests, and perform their processing.
Implementation Features
- Execution Process: Background jobs do not run in separate operating system processes. By default, all jobs are executed in the same process as the main application. If running in a separate process is necessary, a corresponding mechanism exists (see
modules/child_processes). - Startup and Delays: Background jobs start with a delay after the application starts. Jobs from the list do not start simultaneously, but sequentially with a small delay between them. Delay parameters can be configured during initialization; however, by default, startup is performed from the
www.tsfile with optimal values. - Job Registration: The list of background jobs is defined in the
modules/background_jobs/jobs.tsfile, where functions from themodules/background_jobs/jobs/directory are imported.
Creating a New Background Job
To develop a new background job, you need to:
- Copy an existing job file (e.g.,
modules/background_jobs/jobs/example.ts). - Edit it, specifying the call to the required method and setting the repetition interval.
- Register the new function in
modules/background_jobs/jobs.ts.
Configuration Management
The behavior of background jobs can be managed through the config/config.json configuration file in the bj section (for more details, see config.json Parameters).
bj:use
Allows completely disabling the startup of all background jobs if set to false.
bj:doNotLog
Allows disabling logging for the entire chain of methods called from background jobs.
bj:<BackgroundJob_Name>
Allows point-disabling a specific background job by its name.