Last active
August 19, 2020 19:17
-
-
Save luciyer/8c0c11f53f7b8ff3a95cf292816f783b to your computer and use it in GitHub Desktop.
REST "agenda" with long-running jobs
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
require("dotenv").config() | |
const Agenda = require("agenda") | |
const jobs = require("./jobs") | |
const db_uri = process.env.MONGODB_URI || "mongodb://localhost/dev" | |
const connection_options = { | |
db : { | |
address: db_uri, | |
collection: "tasks", | |
options: { useNewUrlParser: true, useUnifiedTopology: true } | |
} | |
} | |
//initialize queue | |
const queue = new Agenda(connection_options) | |
queue.define("deploy package", async job => { | |
// run async job and capture result | |
const job_result = await jobs.deploy() | |
job.attrs.job_result = job_result | |
// optionally - hit some endpoint that client listens to | |
}) | |
module.exports = queue |
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
module.exports = () => { | |
// some job that takes a while to finish! | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve({ | |
field: "val", | |
other: "other" | |
}) | |
}, 5000) | |
}) | |
} |
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
require("dotenv").config() | |
const express = require("express") | |
const bodyParser = require("body-parser") | |
const ObjectId = require("mongodb").ObjectID | |
const agenda = require("./src/agenda") | |
const app = express() | |
app | |
.use(bodyParser.json()) | |
.listen(process.env.PORT || 8080, async () => { | |
// start processing queue | |
await agenda.start() | |
}) | |
app.post("/api/deploy", async (req, res) => { | |
// some function to validate input | |
const some_info = validateInput(req.body) | |
// add job to queue (optionally with data) | |
const queued_job = await agenda.now("deploy package", { info: some_info }) | |
//return job w/ id | |
res.status(200).json(queued_job) | |
}) | |
app.get("/api/jobs/:job_id", async (req, res) => { | |
let formatResponse = (job) => { | |
return { | |
name: job.attrs.name, | |
run_at: job.attrs.lastRunAt, | |
done_at: job.attrs.lastFinishedAt, | |
result: job.attrs.job_result | |
} | |
} | |
try { | |
const job_id = req.params.job_id, | |
filter = { _id: new ObjectId(job_id) }, | |
job_list = await agenda.jobs(filter); | |
if (job_list && job_list.length > 0) { | |
const result = formatResponse(job_list[0]) | |
console.log("Job status:", result) | |
res.status(200).json(result) | |
} else { | |
return res.status(500).json("No job found with that ID.") | |
} | |
} catch (e) { | |
console.error(e.message) | |
res.status(500).json(e.message) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment