Verified Commit a6fba7f7 authored by Mari's avatar Mari Committed by aguestuser

[215] methods for dequeueing if used recently and recycling if not used during grace period

parent 59d83e15
const defaults = {
recyclePhoneNumberDelay: 500, //
recyclePhoneNumberInterval: 1000 * 15, // 30 secs
recycleGracePeriod: 1000 * 60 * 2, // 3 mins
inviteDeletionInterval: 1000 * 60 * 60, // 1 hour
inviteExpiryInMillis: 1000 * 60 * 60 * 24 * 14, // 2 weeks
hotlineMessageExpiryInMillis: 1000 * 60 * 60 * 24 * 28, // 4 weeks
......
......@@ -2,31 +2,64 @@ 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 },
job: { recyclePhoneNumberInterval, recycleGracePeriod },
} = require('../../config')
const channelRepository = require('./channel')
const { recycle } = require('../../registrar/phoneNumber/recycle')
// (String) -> Promise
const enqueue = channelPhoneNumber =>
app.db.recycleablePhoneNumber.create({
channelPhoneNumber,
whenEnqueued: new Date().toISOString(),
})
// (String) -> Promise
const dequeue = channelPhoneNumber =>
app.db.recycleablePhoneNumber.destroy({ where: { channelPhoneNumber } })
// (String) -> Promise
const findByPhoneNumber = channelPhoneNumber =>
app.db.recycleablePhoneNumber.findOne({ where: { channelPhoneNumber } })
// launches job to recycle recycleable numbers
const checkForRecycleablePhoneNumbers = () =>
repeatEvery(() => recyclePhoneNumbers().catch(logger.error), recyclePhoneNumberDelay)
repeatEvery(() => recyclePhoneNumbers().catch(logger.error), recyclePhoneNumberInterval)
/**
* RECYCLING HELPER FUNCTIONS
*/
const recyclePhoneNumbers = async () => {
// console.log(recycle)
const recycleablePhoneNumbers = await app.db.recycleablePhoneNumber.findAll({})
recycleablePhoneNumbers.forEach(recycleablePhoneNumber => {
recycleService.recycle(recycleablePhoneNumber.channelPhoneNumber)
// dequeue recycleableNumbers that were used within recycleDelay window
recycleablePhoneNumbers
.filter(usedRecently)
.forEach(async ({ channelPhoneNumber }) => await dequeue(channelPhoneNumber))
// recycle channel if enqueued before recycleDelay window
recycleablePhoneNumbers.filter(enqueuedAwhileAgo).forEach(async ({ channelPhoneNumber }) => {
await dequeue(channelPhoneNumber)
await recycle(channelPhoneNumber)
})
}
// (Object) -> boolean
const enqueuedAwhileAgo = ({ createdAt }) => {
// difference between now and grace period
const recycleDelayWindow = moment().subtract(recycleGracePeriod)
return moment(createdAt).diff(recycleDelayWindow) < 0
}
// (Object) -> boolean
const usedRecently = async ({ channelPhoneNumber }) => {
const channel = await channelRepository.findDeep(channelPhoneNumber)
const lastUsed = moment(channel.messageCount.updatedAt)
const recycleDelayWindow = moment().subtract(recycleGracePeriod)
return lastUsed.diff(recycleDelayWindow) > 0
}
module.exports = { enqueue, dequeue, findByPhoneNumber, checkForRecycleablePhoneNumbers }
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