Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
feat: define and create jobs using @hokify/agenda
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwigoric committed Nov 23, 2023
1 parent a4cb547 commit dcf4e5b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 71 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"watch": "nodemon --watch src index.js"
},
"dependencies": {
"agenda": "^5.0.0",
"@hokify/agenda": "^6.3.0",
"argon2": "0.31.2",
"cookie-parser": "~1.4.6",
"cors": "^2.8.5",
Expand Down
14 changes: 7 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import 'dotenv/config'

// MongoDB
import database from './db/conn.js'
await database.init().catch((err) => {
console.error(err)
process.exit(1)
})

// Initialize schedules
import './schedules/agenda.js'
await database
.init()
.then(() => import('./schedules/agenda.js'))
.catch((err) => {
console.error(err)
process.exit(1)
})

// Routes
import indexRouter from './routes/index.js'
Expand Down
4 changes: 1 addition & 3 deletions src/db/conn.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class Database {
// Set the MongoDB URI
const url = process.env.MONGODB_URI || DEFAULT_MONGODB_URI

return mongoose.connect(url).then(() => {
console.log('Database connection successful')
})
return mongoose.connect(url)
// Do not catch the error; the app should crash on database connection failure
}

Expand Down
27 changes: 22 additions & 5 deletions src/schedules/agenda.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
// Packages
import Agenda from 'agenda'
import { Agenda } from '@hokify/agenda'
import mongoose from 'mongoose'

const agenda = new Agenda()

agenda.mongo(mongoose.connection, 'agenda-jobs')
// Configure Agenda
const agenda = new Agenda({
ensureIndex: true,
processEvery: '1 minute',
mongo: mongoose.connection
})

// Import jobs
import jobs from './jobs/index.js'

// Define jobs
for (const job in jobs) agenda.define(job.name, job.handler)
for (const jobInfo of Object.values(jobs)) agenda.define(jobInfo.name, jobInfo.handler)

const graceful = async () => {
await agenda.stop()
process.exit(0)
}

process.on('SIGTERM', graceful)
process.on('SIGINT', graceful)

async function start() {
await agenda.start()
for (const jobInfo of Object.values(jobs)) await agenda.every(jobInfo.every, jobInfo.name)
}
await start()

export default agenda
6 changes: 5 additions & 1 deletion src/schedules/jobs/loan-interests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const name = 'process-loan-interests'

const handler = async (job, done) => {
console.log('i ran!')

const { Loan } = job.context.models

// TODO: Process loan interests

done()
}

export default { name, handler }
const every = '* * * * *'

export default { name, handler, every }
81 changes: 27 additions & 54 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,20 @@ __metadata:
languageName: node
linkType: hard

"@hokify/agenda@npm:^6.3.0":
version: 6.3.0
resolution: "@hokify/agenda@npm:6.3.0"
dependencies:
cron-parser: "npm:^4"
date.js: "npm:~0.3.3"
debug: "npm:~4"
human-interval: "npm:~2"
luxon: "npm:^3"
mongodb: "npm:^4"
checksum: 22c097492c31417e8dad72d1706f6a03e48f4fd0372bdb1eca32110bf033c9538c9432911408718c7f317af51d39a4a87f00a0f53c5a35bc4cec6adb162372ea
languageName: node
linkType: hard

"@humanwhocodes/config-array@npm:^0.11.13":
version: 0.11.13
resolution: "@humanwhocodes/config-array@npm:0.11.13"
Expand Down Expand Up @@ -1271,20 +1285,6 @@ __metadata:
languageName: node
linkType: hard

"agenda@npm:^5.0.0":
version: 5.0.0
resolution: "agenda@npm:5.0.0"
dependencies:
cron-parser: "npm:^3.5.0"
date.js: "npm:~0.3.3"
debug: "npm:~4.3.4"
human-interval: "npm:~2.0.1"
moment-timezone: "npm:~0.5.37"
mongodb: "npm:^4.11.0"
checksum: a1aa5d4301712f6308c7972c7a4601ed6135561928b4e64a557b6a73aa40e21ee490a527006686ab46fddf2e023a5fde6f5c573ce3ba2a3946c4e8f4816fdd35
languageName: node
linkType: hard

"agent-base@npm:6":
version: 6.0.2
resolution: "agent-base@npm:6.0.2"
Expand Down Expand Up @@ -1791,13 +1791,12 @@ __metadata:
languageName: node
linkType: hard

"cron-parser@npm:^3.5.0":
version: 3.5.0
resolution: "cron-parser@npm:3.5.0"
"cron-parser@npm:^4":
version: 4.9.0
resolution: "cron-parser@npm:4.9.0"
dependencies:
is-nan: "npm:^1.3.2"
luxon: "npm:^1.26.0"
checksum: 09bb87285e36baddec1b3a87f441c80d3e01f349e341c8cf6f9f01045a532bd5388f9fd52dfc374818e58a93cb172b000379ce02a411e4fc94a99302e7fe2330
luxon: "npm:^3.2.1"
checksum: ffca5e532a5ee0923412ee6e4c7f9bbceacc6ddf8810c16d3e9fb4fe5ec7e2de1b6896d7956f304bb6bc96b0ce37ad7e3935304179d52951c18d84107184faa7
languageName: node
linkType: hard

Expand Down Expand Up @@ -1830,7 +1829,7 @@ __metadata:
languageName: node
linkType: hard

"debug@npm:4, debug@npm:4.x, debug@npm:^4.1.1, debug@npm:^4.3.2, debug@npm:^4.3.4, debug@npm:~4.3.4":
"debug@npm:4, debug@npm:4.x, debug@npm:^4.1.1, debug@npm:^4.3.2, debug@npm:^4.3.4, debug@npm:~4, debug@npm:~4.3.4":
version: 4.3.4
resolution: "debug@npm:4.3.4"
dependencies:
Expand Down Expand Up @@ -2770,7 +2769,7 @@ __metadata:
languageName: node
linkType: hard

"human-interval@npm:~2.0.1":
"human-interval@npm:~2":
version: 2.0.1
resolution: "human-interval@npm:2.0.1"
dependencies:
Expand Down Expand Up @@ -2978,16 +2977,6 @@ __metadata:
languageName: node
linkType: hard

"is-nan@npm:^1.3.2":
version: 1.3.2
resolution: "is-nan@npm:1.3.2"
dependencies:
call-bind: "npm:^1.0.0"
define-properties: "npm:^1.1.3"
checksum: 1f784d3472c09bc2e47acba7ffd4f6c93b0394479aa613311dc1d70f1bfa72eb0846c81350967722c959ba65811bae222204d6c65856fdce68f31986140c7b0e
languageName: node
linkType: hard

"is-negative-zero@npm:^2.0.2":
version: 2.0.2
resolution: "is-negative-zero@npm:2.0.2"
Expand Down Expand Up @@ -3296,10 +3285,10 @@ __metadata:
languageName: node
linkType: hard

"luxon@npm:^1.26.0":
version: 1.28.1
resolution: "luxon@npm:1.28.1"
checksum: dfba84bb3cd72f359c77115d374937c31eccdfa6b383284b6848c075f85e02269d4d9a717608944f2c11c4ca5d3df0fc18197ce4ca21313ddd72619b39ee034c
"luxon@npm:^3, luxon@npm:^3.2.1":
version: 3.4.4
resolution: "luxon@npm:3.4.4"
checksum: c14164bc338987349075a08e63ea3ff902866735f7f5553a355b27be22667919765ff96fde4d3413d0e9a0edc4ff9e2e74ebcb8f86eae0ce8b14b27330d87d6e
languageName: node
linkType: hard

Expand Down Expand Up @@ -3502,22 +3491,6 @@ __metadata:
languageName: node
linkType: hard

"moment-timezone@npm:~0.5.37":
version: 0.5.43
resolution: "moment-timezone@npm:0.5.43"
dependencies:
moment: "npm:^2.29.4"
checksum: f8b66f8562960d6c2ec90ea7e2ca8c10bd5f5cf5ced2eaaac83deb1011b145d0154e8d77018cf5e913d489898a343122a3d815768809653ab039306dce1db1eb
languageName: node
linkType: hard

"moment@npm:^2.29.4":
version: 2.29.4
resolution: "moment@npm:2.29.4"
checksum: 157c5af5a0ba8196e577bc67feb583303191d21ba1f7f2af30b3b40d4c63a64d505ba402be2a1454832082fac6be69db1e0d186c3279dae191e6634b0c33705c
languageName: node
linkType: hard

"mongodb-connection-string-url@npm:^2.6.0":
version: 2.6.0
resolution: "mongodb-connection-string-url@npm:2.6.0"
Expand Down Expand Up @@ -3562,7 +3535,7 @@ __metadata:
languageName: node
linkType: hard

"mongodb@npm:^4.11.0":
"mongodb@npm:^4":
version: 4.17.1
resolution: "mongodb@npm:4.17.1"
dependencies:
Expand Down Expand Up @@ -4764,8 +4737,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "unboundmnl-area2-server@workspace:."
dependencies:
"@hokify/agenda": "npm:^6.3.0"
"@rushstack/eslint-patch": "npm:^1.6.0"
agenda: "npm:^5.0.0"
argon2: "npm:0.31.2"
cookie-parser: "npm:~1.4.6"
cors: "npm:^2.8.5"
Expand Down

0 comments on commit dcf4e5b

Please sign in to comment.