-
Notifications
You must be signed in to change notification settings - Fork 0
/
celeryWorker.js
41 lines (32 loc) · 1.1 KB
/
celeryWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const celery = require('celery-node')
const dotenv = require('dotenv')
const validator = require('validator').default
const celeryTasks = require('./lib/celery-tasks')
// load .env / Environment Variables
dotenv.config()
const celeryWorker = celery.createWorker(
process.env.BROKER_URL,
process.env.BACKEND_URL
)
const validatePhoneno = (phoneno) => {
console.log('Validate Phone no. Task Received')
// REGEX + Named Capturing | requires EcmaScript / JavaScript
// ^(?<country_code>((\+\d{1,}\s+)|()))(?<phoneno>(\d{10}))$
const phoneDetails = String(phoneno)
.trim()
.match(/^(?<countryCode>((\+\d{1,}\s+)|()))(?<number>(\d{10}))$/)
if (!phoneDetails) return null
return {
countryCode: phoneDetails.groups['countryCode'],
number: Number(phoneDetails.groups['number']),
}
}
const validateEmail = (email) => {
console.log('Validate Email Task Received')
return validator.isEmail(email)
}
// REGISTER TASKS
celeryWorker.register(celeryTasks.VALIDATE_PHONE_NO, validatePhoneno)
celeryWorker.register(celeryTasks.VALIDATE_EMAIL, validateEmail)
// START WORKER
celeryWorker.start()