Verified Commit 9253a7ae authored by aguestuser's avatar aguestuser

[215] make all jobs cancellable (and cancel them in .stop())

parent 51e6a532
......@@ -7,6 +7,7 @@ const app = {
socketPool: null,
api: null,
metrics: null,
jobs: null,
}
app.run = async ({ db, socketPool, api, metrics, jobs, signal }) => {
......@@ -40,7 +41,7 @@ app.run = async ({ db, socketPool, api, metrics, jobs, signal }) => {
logger.log(`...created metrics registry!`)
logger.log('Running startup jobs...')
await jobsService.run().catch(logger.fatalError)
app.jobs = jobsService.run().catch(logger.fatalError)
logger.log('...ran startup jobs!')
logger.log('Starting signal service...')
......@@ -54,7 +55,12 @@ app.run = async ({ db, socketPool, api, metrics, jobs, signal }) => {
app.stop = async () => {
const { logger } = require('./util')
logger.log('Shutting down signalboost...')
await Promise.all([app.socketPool.stop(), app.db.stop(), app.api.stop()])
await Promise.all([
() => app.socketPool.stop(),
() => app.db.stop(),
() => app.api.stop(),
() => app.jobs.stop(),
])
logger.log('...Signalboost shut down!')
}
......
......@@ -5,11 +5,18 @@ const smsSenderRepository = require('./db/repositories/smsSender')
const hotlineMessageRepository = require('./db/repositories/hotlineMessage')
const diagnostics = require('./diagnostics')
const util = require('./util')
const { values } = require('lodash')
const {
job: { healthcheckInterval, inviteDeletionInterval, recycleInterval, signaldStartupTime },
signal: { diagnosticsPhoneNumber },
} = require('./config')
const cancelations = {
deleteInvitesJob: null,
recycleJob: null,
healtcheckJob: null,
}
const run = async () => {
logger.log('--- Running startup jobs...')
......@@ -38,14 +45,14 @@ const run = async () => {
*****************/
logger.log('----- Launching invite scrubbing job...')
util.repeatEvery(
cancelations.deleteInvitesJob = util.repeatUntilCancelled(
() => inviteRepository.deleteExpired().catch(logger.error),
inviteDeletionInterval,
)
logger.log('----- Launched invite scrubbing job.')
logger.log('---- Launching recycle request processing job...')
util.repeatEvery(
cancelations.recycleJob = util.repeatUntilCancelled(
() => phoneNumberRegistrar.processRecycleRequests().catch(logger.error),
recycleInterval,
)
......@@ -54,13 +61,20 @@ const run = async () => {
logger.log('---- Launching healthcheck job...')
const launchHealthchecks = async () => {
await util.wait(signaldStartupTime)
util.repeatEvery(() => diagnostics.sendHealthchecks().catch(logger.error), healthcheckInterval)
cancelations.healtcheckJob = util.repeatUntilCancelled(
() => diagnostics.sendHealthchecks().catch(logger.error),
healthcheckInterval,
)
}
if (diagnosticsPhoneNumber) launchHealthchecks()
logger.log('---- Launched healthcheck job...')
logger.log('--- Startup jobs complete!')
logger.log('--- Registrar running!')
return { stop }
}
module.exports = { run }
const stop = () => values(cancelations).forEach(fn => fn())
module.exports = { run, stop }
......@@ -20,17 +20,6 @@ describe('jobs service', () => {
processRecycleRequestsStub,
sendHealthchecksStub
/****
* TODO(aguestuser|2020-09-10):
*
* The fact that this suite kicks of long-running recurring jobs but never
* cancels them causes other unit tests to fail non-determinitically, because they
* asserting on the same functions that are called repeatedly in this suite.
*
* If this gets annoying enough, we should likely figure out a `jobs.stop()` function
* that cancels all the `repeatEvery` calls a
**/
describe('running the service', () => {
let originalReregisterValue = process.env.REREGISTER_ON_STARTUP
before(async () => {
......@@ -59,6 +48,7 @@ describe('jobs service', () => {
after(() => {
process.env.REREGISTER_ON_STARTUP = originalReregisterValue
sinon.restore()
jobs.stop()
})
describe('one-off jobs', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment