3 const common
= require('./common');
4 const Enum
= require('./enum');
5 const { Chores: BaseChores
} = require('@squeep/chores');
6 const _fileScope
= common
.fileScope(__filename
);
9 * Wrangle periodic tasks what need doing.
12 class Chores
extends BaseChores
{
13 constructor(logger
, db
, options
) {
15 this.options
= options
;
18 this.establishChore(Enum
.Chore
.CleanTokens
, this.cleanTokens
.bind(this), options
?.chores
?.tokenCleanupMs
);
19 this.establishChore(Enum
.Chore
.CleanScopes
, this.cleanScopes
.bind(this), options
?.chores
?.scopeCleanupMs
);
23 * Attempt to remove tokens which are expired or otherwise no longer valid.
24 * @param {Number} atLeastMsSinceLast
26 async
cleanTokens(atLeastMsSinceLast
= this.options
?.chores
?.tokenCleanupMs
|| 0) {
27 const _scope
= _fileScope('cleanTokens');
28 this.logger
.debug(_scope
, 'called', atLeastMsSinceLast
);
32 await
this.db
.context(async (dbCtx
) => {
33 const codeValidityTimeoutSeconds
= Math
.ceil(this.options
.manager
.codeValidityTimeoutMs
/ 1000);
34 tokensCleaned
= await
this.db
.tokenCleanup(dbCtx
, codeValidityTimeoutSeconds
, atLeastMsSinceLast
);
37 this.logger
.info(_scope
, 'finished', { tokensCleaned
});
40 this.logger
.error(_scope
, 'failed', { error: e
});
47 * Attempt to remove ephemeral scopes which are no longer referenced by tokens.
48 * @param {Number} atLeastMsSinceLast
50 async
cleanScopes(atLeastMsSinceLast
= this.options
?.chores
?.scopeCleanupMs
|| 0) {
51 const _scope
= _fileScope('cleanScopes');
52 this.logger
.debug(_scope
, 'called', atLeastMsSinceLast
);
56 await
this.db
.context(async (dbCtx
) => {
57 scopesCleaned
= await
this.db
.scopeCleanup(dbCtx
, atLeastMsSinceLast
);
60 this.logger
.info(_scope
, 'finished', { scopesCleaned
});
63 this.logger
.error(_scope
, 'failed', { error: e
});
70 module
.exports
= Chores
;