-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Worker/Queue to process expired DNS records (#357)
- Loading branch information
1 parent
b634fbf
commit 8f7eeb1
Showing
5 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Worker, Queue, UnrecoverableError } from 'bullmq'; | ||
|
||
import { redis } from '~/lib/redis.server'; | ||
import logger from '~/lib/logger.server'; | ||
|
||
import { getExpiredRecords } from '~/models/record.server'; | ||
import { addNotification } from '../notifications/notifications.server'; | ||
import { deleteDnsRequest } from '../dns/delete-record-flow.server'; | ||
|
||
const { EXPIRATION_REPEAT_FREQUENCY_MS, JOB_REMOVAL_FREQUENCY_MS } = process.env; | ||
|
||
// constant for removing job on completion/failure (in milliseconds) | ||
const JOB_REMOVAL_INTERVAL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days | ||
declare global { | ||
var __expiration_request_init__: boolean; | ||
} | ||
// queue name | ||
const expirationRequestQueueName = 'expiration-request'; | ||
|
||
// queue initialization | ||
const expirationRequestQueue = new Queue(expirationRequestQueueName, { | ||
connection: redis, | ||
}); | ||
|
||
export function addExpirationRequest() { | ||
return expirationRequestQueue.add(expirationRequestQueueName, { | ||
repeat: { every: Number(EXPIRATION_REPEAT_FREQUENCY_MS) || 24 * 60 * 60 * 1000 }, | ||
removeOnComplete: { age: Number(JOB_REMOVAL_FREQUENCY_MS) || JOB_REMOVAL_INTERVAL_MS }, | ||
removeOnFail: { age: Number(JOB_REMOVAL_FREQUENCY_MS) || JOB_REMOVAL_INTERVAL_MS }, | ||
}); | ||
} | ||
|
||
// worker definition | ||
const expirationRequestWorker = new Worker( | ||
expirationRequestQueueName, | ||
async (job) => { | ||
try { | ||
logger.info('process DNS record expiration'); | ||
let dnsRecords = await getExpiredRecords(); | ||
Promise.all( | ||
dnsRecords.map(async ({ id, username, type, subdomain, value, user }) => { | ||
// delete records from Route53 and DB | ||
await deleteDnsRequest({ id, username, type, subdomain, value }); | ||
// add notification jobs (assuming deletion went successfully) | ||
await addNotification({ | ||
emailAddress: user.email, | ||
subject: 'DNS record expiration subject', | ||
message: 'DNS record expiration message', | ||
}); | ||
}) | ||
); | ||
} catch (err) { | ||
throw new UnrecoverableError(`Unable to process DNS record expiration: ${err}`); | ||
} | ||
logger.info('TODO: process certificate expiration'); | ||
}, | ||
{ connection: redis } | ||
); | ||
|
||
process.on('SIGINT', () => expirationRequestWorker.close()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters