Verified Commit 6fe7e2e3 authored by Mari's avatar Mari Committed by aguestuser

[215] launch job for recycling recycleable numbers

parent 249cd2b2
const defaults = {
recyclePhoneNumberDelay: 500, //
inviteDeletionInterval: 1000 * 60 * 60, // 1 hour
inviteExpiryInMillis: 1000 * 60 * 60 * 24 * 14, // 2 weeks
hotlineMessageExpiryInMillis: 1000 * 60 * 60 * 24 * 28, // 4 weeks
......
......@@ -9,7 +9,7 @@ const {
job: { inviteExpiryInMillis, inviteDeletionInterval },
} = require('../../config')
// (Database, string, string, string) -> Promise<boolean>
// (string, string, string) -> Promise<boolean>
const issue = async (channelPhoneNumber, inviterPhoneNumber, inviteePhoneNumber) => {
// issues invite IFF invitee is not already invited
const [, wasCreated] = await app.db.invite.findOrCreate({
......@@ -18,26 +18,26 @@ const issue = async (channelPhoneNumber, inviterPhoneNumber, inviteePhoneNumber)
return wasCreated
}
// (Database, string, string) -> Promise<number>
// (string, string) -> Promise<number>
const count = (channelPhoneNumber, inviteePhoneNumber) =>
app.db.invite.count({ where: { channelPhoneNumber, inviteePhoneNumber } })
// (Database, string, string, string) -> Promise<Array<Membership,number>>
// (string, string, string) -> Promise<Array<Membership,number>>
const accept = (channelPhoneNumber, inviteePhoneNumber, language = defaultLanguage) =>
Promise.all([
membershipRepository.addSubscriber(channelPhoneNumber, inviteePhoneNumber, language),
app.db.invite.destroy({ where: { channelPhoneNumber, inviteePhoneNumber } }),
])
// (Database, string, string) -> Promise<number>
// (string, string) -> Promise<number>
const decline = async (channelPhoneNumber, inviteePhoneNumber) =>
app.db.invite.destroy({ where: { channelPhoneNumber, inviteePhoneNumber } })
// (Database, number) -> Promise<void>
// (number) -> Promise<void>
const launchInviteDeletionJob = () =>
repeatEvery(() => deleteExpired().catch(logger.error), inviteDeletionInterval)
// Database -> Promise<number>
// () -> Promise<number>
const deleteExpired = async () =>
app.db.invite.destroy({
where: {
......
const app = require('../..')
const moment = require('moment')
const { repeatEvery, loggerOf } = require('../../util')
const logger = loggerOf('db|recycleablePhoneNumberRepository')
const recycleService = require('../../registrar/phoneNumber/recycle')
const {
job: { recyclePhoneNumberDelay },
} = require('../../config')
const enqueue = channelPhoneNumber =>
app.db.recycleablePhoneNumber.create({
......@@ -12,4 +19,14 @@ const dequeue = channelPhoneNumber =>
const findByPhoneNumber = channelPhoneNumber =>
app.db.recycleablePhoneNumber.findOne({ where: { channelPhoneNumber } })
module.exports = { enqueue, dequeue, findByPhoneNumber }
const checkForRecycleablePhoneNumbers = () =>
repeatEvery(() => recyclePhoneNumbers().catch(logger.error), recyclePhoneNumberDelay)
const recyclePhoneNumbers = async () => {
// console.log(recycle)
const recycleablePhoneNumbers = await app.db.recycleablePhoneNumber.findAll({})
recycleablePhoneNumbers.forEach(recycleablePhoneNumber => {
recycleService.recycle(recycleablePhoneNumber.channelPhoneNumber)
})
}
module.exports = { enqueue, dequeue, findByPhoneNumber, checkForRecycleablePhoneNumbers }
......@@ -4,6 +4,7 @@ const inviteRepository = require('./db/repositories/invite')
const smsSenderRepository = require('./db/repositories/smsSender')
const hotlineMessageRepository = require('./db/repositories/hotlineMessage')
const diagnostics = require('./diagnostics')
const recycleablePhoneNumberRepository = require('./db/repositories/recycleablePhoneNumber')
const run = async () => {
logger.log('--- Running startup jobs...')
......@@ -28,11 +29,16 @@ const run = async () => {
inviteRepository.launchInviteDeletionJob()
logger.log('----- Launched data cleaning jobs.')
logger.log('----- Launching job to check for recycleable numbers...')
recycleablePhoneNumberRepository.checkForRecycleablePhoneNumbers()
logger.log('----- Launched recycleable numbers job')
logger.log('---- Launching healthcheck job...')
diagnostics.launchHealthcheckJob()
logger.log('---- Launched healthcheck job...')
logger.log('--- Startup jobs complete!')
logger.log('--- Registrar running!')
}
module.exports = { run }
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