-
Notifications
You must be signed in to change notification settings - Fork 1
Job
Job
is a class of single-use objects created for each execution of a business method taken from Application's modules
according to its namingConventions
.
(The name means job as in Queueing theory, not Oracle Database's periodic tasks)
This class implements the basic workflow:
- take
this.request
(a plain object); -
determine
this.method
to call (ifnull
, do nothing more); - execute it, either obtaining
this.result
or catchingthis.error
; - finally, release all resources possibly used.
Every step is transparently tracked down. The execution time can be limited.
Job
is presumed to never be inherited from. Its instances are configured by parent JobSources.
Name | Description |
---|---|
[Symbol('todo')] |
An Array of Promises to be awaited for during outcome ()
|
[Symbol('timestamps')] |
An object of event-to-timestamp pairs plus the 'created' key with the constructor call timestamp |
[Symbol('maxLatency')] |
The maximum time between the constructor call and the end or error event, Infinity by default |
[Symbol('minLatency')] |
The minimum time between the constructor call and the next event, 0 by default |
Name | Description |
---|---|
app |
the Application instance this job belongs to |
src |
the JobSource this job comes from (if any) |
parent |
absent by default, by convention this property should be set to this by a job creating another job. |
id |
the unique identifier of this job to use in logs etc. By default, is generated with crypto.randomUUID, but this can be overridden by setting id in generators object |
request |
request parameters, an an Object. Think PHP's $_REQUEST generalization. |
user |
By convention, this property must represent the system's actor performing the actual Job : for security checks, logging etc. Missing by default. |
method |
the business method to be executed, with [ModuleMap.MODULE] and [ModuleMap.METHOD_NAME] properties set |
module |
the module containing the business method to be executed: fetched from method [ModuleMap.MODULE]
|
result |
the result returned by the business method and to be returned by outcome ()
|
error |
the error thrown by the business method and to be thrown by outcome () , with [Job.INSTANCE] set to the originated job
|
logger |
A winston Logger |
tracker |
the Tracker listening to the job's events and registering it with logger
|
[Tracker.LOGGING_DETAILS] |
computed via src.getJobLoggingDetails
|
[Tracker.LOGGING_ID] |
computed as id
|
[Tracker.LOGGING_PARENT] |
computed as src
|
[Tracker.LOGGING_EVENTS] |
set from app.globals entry, adjusted with src.globals . See also Application's default globals
|
Never to be called directly. Job
instances must be produced only by calling the factory method createJob ()
.
This asynchronous method implements the job's lifecycle:
- calling
app.getMethod (request)
to determine themethod
to execute; - invoking
callMethod
with the business method and then:- if it throws an Error, set it to
error
and rethrow; - if it throws something other, set to
error
() and rethrow aNotAnError
value; - otherwise, set the output value to
result
and return it.
- if it throws an Error, set it to
At each step but the last one, outcome ()
calls broadcast ()
with the corresponding event name to process asynchronous event handlers before proceeding to the next stage. The next
event is just emitted, so all its handlers must be synchronous and neither of them can throw an error.
Each event handler defined as a (not arrow) function
sees the Job
instance as this
and can modify its content. The execution order is fixed, so for each event subsequent handlers see the job state modified by previous ones. In particular, if the business method throws something, the error
property may be rewritten by error
handlers and if finally it gets undefined
, outcome ()
returns undefined
instead of throwing anything.
This method, designed to be called from business methods, throws a Job.CustomError
instance with the given message
and an extra [Job.PAYLOAD]
property. Moreover, if the payload
is a non null
, object all of its properties are copied into the error instance.
This generator method iterates over all job's resources provided by pools of a given clazz
.
This synchronous method registers the given promise to be awaited for before outcome ()
to proceeding to the next stage. It is to be called from event handlers.
This is a wrapper around waitFor
that postpones a Promise immediately rejecting with the given error.
This asynchronous method invokes func
as a method of this job and, normally, returns its result or rethrows the error. If [Symbol('maxLatency')]
is finite and the time is out, throws an expirationError ()
instead.
This synchronous method returns (not throws) an Error with the message "Timeout expired".
This synchronous method returns period
minus the number of milliseconds elapsed from the job creation.
This synchronous method sets the Symbol('maxLatency')
private property to the non-negative integer ms
. If set to a finite number, the end
event cannot occur later than ms
milliseconds since the job creation. Incase of such a delay, the expirationError
result is thrown.
This synchronous method sets the Symbol('minLatency')
private property to the non-negative integer ms
. If set, the next
event will be emitted not earlier than ms
milliseconds since the job creation, even if the result is obtained and all resources are freed.
This asynchronous method implements a single step of the job's lifecycle:
- writes the current timestamp into
this [Symbol ('timestamps')] [event]
; - initializes the TODO list;
- emits the
event
(which handlers should either be synchronous or callwaitFor (promise)
); - awaits for all promises from the TODO list.
Name | When emitted | Purpose |
---|---|---|
init |
Beginning of the lifecycle | Logging, adjusting the request content (e. g. fetching it from a stream) |
start |
Just before calling the method
|
Security checks, logging |
end |
The result is obtained and set as a property |
Rewriting result , reporting it |
error |
The error is caught and set as a property |
Rewriting error , reporting it |
finish |
Guaranteed to be emitted after all end / error handlers are done working |
Cleaning up, logging |
next |
After asynchronous finish handlers, but not earlier than Symbol('minLatency') ms from job creation |
Scheduling chained jobs. Only synchronous functions allowed |