exframe-scheduler
v2.9.3
Published
Schedules actions for state machines
Downloads
131
Readme
exframe-scheduler
A module for managing and executing scheduled state changes.
Usage
const scheduler = require('exframe-scheduler').create(stateMachine);
Configuration Settings
- stateMachine - The name of the state machine being managed.
Methods
startProcessing
Starts polling the database for actions that need to be executed.
scheduler.startProcessing(interval, actionProcessor);
Arguments:
- interval integer The interval, in seconds, the module will wait between database polls. (default = 60)
- actionProcessor function Required. A callback function to execute when an action is triggered. The relevant documentActions document (see below) will be passed to the callback function.
insertOrGetDocument
Retrieves the documentActions document for the given documentNumber. Creates a documentActions document if one does not exist.
Returns a Promise that resolves with an object containing the documentActions and a flag indicating whether or not a new document was created.
scheduler.insertOrGetDocument(documentActions);
Arguments
- documentActions object Required. A full or partial documentActions object
- documentNumber string Required. The document number of interest.
- actions array An array of actions objects. (default = [])
- name string Required. The name of the action.
- date datetime Required. The datetime the action should be executed.
Resolves with
- new boolean true if a new documentActions document was created. Otherwise, false.
- document object The documentActions document (see below).
Example
const result = await insertOrGetDocument({ documentNumber: policyNumber });
console.log(result);
/*
{
new: false,
document: {
_id : ObjectId('5bc8d51e6f5461df54193d34'),
documentNumber : '24-8412753-01',
stateMachine : 'policy',
nextRunAt : '2018-10-28T18:46:53.000Z',
editHash : 'Oc2ajQTQop',
lockEnd : null,
lockId : null,
actions : [
{
_id : '5bc8d522b635cb001d0799cb',
name : 'sendNonPaymentNotice',
date : '2018-10-28T18:46:53.000Z'
},
{
_id : '5bc8d522b635cb001d0799ca',
name : 'cancelForNonPayment',
date : '2018-11-17T19:46:53.000Z'
},
{
_id : '5bc8d522b635cb001d0799c9',
name : 'renewPolicy',
date : '2019-08-19T18:46:53.000Z'
},
{
_id : '5bc8d522b635cb001d0799c8',
name : 'setToNotInForce',
date : '2019-10-18T18:46:53.000Z'
}
]
}
}
*/
updateDocument
Updates an existing documentActions document.
Returns a promise that resolves with the new document if the update succeeded or null if the update did not occur.
scheduler.updateDocument(documentActions);
Arguments:
- documentActions object Required. A documentActions object:
- documentNumber string Required. The document number of interest.
- editHash string Required. The edit hash that ensures no updates have occurred since this document was retrieved.
- lockId string The lockId acquired when the document was locked. If the document is locked and the lockId matches, the document will be updated. Otherwise, the document will only be updated if it is not locked.
- actions array An array of actions objects. (default = [])
- name string Required. The name of the action.
- date datetime Required. The datetime the action should be executed.
Example
const result = await updateDocument('Oc2ajQTQop', { documentNumber: '24-8412753-01', actions: [], lockId: 'P-2a_QTWoi' });
console.log(result);
/*
{
_id : ObjectId('5bc8d51e6f5461df54193d34'),
documentNumber : '24-8412753-01',
stateMachine : 'policy',
nextRunAt : null,
editHash : 'Gc6a-QTQ21',
lockEnd : '2018-10-28T18:10:23.000Z',
lockId : 'P-2a_QTWoi',
actions : []
}
}
*/
lockDocument
Locks an existing documentActions document for a specified amount of time, indicating to other processes that something else is working on it.
Returns a promise that resolves with the lockId if the lock was successful or null if a lock was not able to be obtained.
scheduler.lockDocument(documentNumber, timeout);
Arguments:
- documentNumber string Required. The documentNumber of the document of interest.
- timeout integer The amount of time for which to lock the document in seconds. (default = 600)
Example
const result = await lockDocument('24-8412753-01', 120);
console.log(result);
/*
{
lockId: 'Oc2ajQTQop'
}
*/
unlockDocument
Unlocks an existing documentActions document.
Returns a promise that resolves with the object { lockId: null }, indicating that the document has been unlocked when successful, or null if unsuccessful.
scheduler.unlockDocument(documentNumber, lockId);
Arguments:
- documentNumber string Required. The documentNumber of the document of interest.
- lockId string The lockId obtained when the document was initially locked.
Example
const result = await unlockDocument('24-8412753-01', 'Oc2ajQTQop');
console.log(result);
/*
{
lockId: null
}
*/
countActiveDocuments
Counts documentactions where nextRunAt != null.
Returns a promise that resolves with an integer, indicating the number of document actions meeting the criteria.
scheduler.countActiveDocuments(args);
Arguments:
- earliest string Optional. The earliest (inclusive) nextRunAt DateTime to consider in the count.
- latest string Optional. The latest (inclusive) nextRunAt DateTime to consider in the count.
Example
const result = await countActiveDocuments({ earliest: '2020-01-01T04:00:00.000Z', latest: '2020-02-01T03:59:59.999Z'});
console.log(result);
/*
452
*/