Skip to content

Commit

Permalink
Fastify support (#201)
Browse files Browse the repository at this point in the history
* feat: Add Fastify support (#194)

* Create Fastify middleware

* Add fastify standalone script

* add fastify middleware tests

* Add Fastify middleware documentation

* Fix Vue template...

* fix ObjectId

* move fastify to dev dependencies

Co-authored-by: Vasyl Boroviak <[email protected]>

* Fix after merge conflicts

* Run prettier for all JS files

Co-authored-by: Maxime Chabert <[email protected]>
  • Loading branch information
koresar and maximechbt authored Jan 31, 2022
1 parent b1f921c commit 816993e
Show file tree
Hide file tree
Showing 7 changed files with 1,172 additions and 6,518 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,33 @@ await app.listen(3002);

Then browse to `http://localhost:3002/`.

#### Fastify

```shell
npm i fastify
```

```js
const agenda = new Agenda().database(
"mongodb://127.0.0.1/agendaDb",
"agendaJobs"
);

const Fastify = require("fastify");
const fastify = new Fastify();

fastify.register(
Agendash(
agenda,
{ middleware: "fastify" }
);
);

await fastify.listen(3002);
```

Then browse to `http://localhost:3002/`.

### Standalone usage

Agendash comes with a standalone Express app which you can use like this:
Expand Down
61 changes: 61 additions & 0 deletions bin/agendash-standalone-fastify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node
"use strict";
const Agenda = require("agenda");
const agendash = require("../app");
const Fastify = require("fastify");
const program = require("commander");

program
.option(
"-d, --db <db>",
"[required] Mongo connection string, same as Agenda connection string"
)
.option(
"-c, --collection <collection>",
"[optional] Mongo collection, same as Agenda collection name, default agendaJobs",
"agendaJobs"
)
.option(
"-p, --port <port>",
"[optional] Server port, default 3000",
(n, d) => Number(n) || d,
3000
)
.option(
"-t, --title <title>",
"[optional] Page title, default Agendash",
"Agendash"
)
.option(
"-p, --path <path>",
"[optional] Path to bind Agendash to, default /",
"/"
)
.parse(process.argv);

if (!program.db) {
console.error("--db required");
process.exit(1);
}

if (!program.path.startsWith("/")) {
console.error("--path must begin with /");
process.exit(1);
}

const fastify = Fastify();

const agenda = new Agenda().database(program.db, program.collection);

fastify.register(
agendash(agenda, {
middleware: "fastify",
title: program.title,
})
);

fastify.listen(program.port, function () {
console.log(
`Agendash started http://localhost:${program.port}${program.path}`
);
});
7 changes: 1 addition & 6 deletions lib/controllers/agendash.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ module.exports = function (agenda, options) {

if (options.query && options.property) {
if (options.isObjectId) {
preMatch[options.property] = ObjectId(
options.query
);
preMatch[options.property] = ObjectId(options.query);
} else if (/^\d+$/.test(options.query)) {
preMatch[options.property] = Number.parseInt(options.query, 10);
} else {
Expand Down Expand Up @@ -287,7 +285,6 @@ module.exports = function (agenda, options) {
const jobs = await collection
.find({
_id: { $in: jobIds.map((jobId) => ObjectId(jobId)) },

})
.toArray();
if (jobs.length === 0) {
Expand All @@ -308,9 +305,7 @@ module.exports = function (agenda, options) {
return Promise.reject(new Error("Agenda instance is not ready"));
}

//const collection = agenda._collection.collection || agenda._collection;
return agenda.cancel({
//_id: { $in: jobIds.map((jobId) => collection.s.pkFactory(jobId)) },
_id: { $in: jobIds.map((jobId) => ObjectId(jobId)) },
});
};
Expand Down
75 changes: 75 additions & 0 deletions lib/middlewares/fastify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const path = require("path");

module.exports = (agendash) => (instance, opts, done) => {
const { api, requeueJobs, deleteJobs, createJob } = agendash;

instance.register(require("fastify-static"), {
root: path.join(__dirname, "../../public"),
});

instance.get("/", function (req, reply) {
return reply.sendFile("index.html");
});

instance.get("/api", async (request, response) => {
try {
const {
job,
state,
skip,
limit,
q,
property,
isObjectId,
} = request.query;
const apiResponse = await api(job, state, {
query: q,
property,
isObjectId,
skip,
limit,
});
response.send(apiResponse);
} catch (error) {
response.status(400).send(error);
}
});

instance.post("/api/jobs/requeue", async (request, response) => {
try {
const newJobs = await requeueJobs(request.body.jobIds);
response.send(newJobs);
} catch (error) {
response.status(404).send(error);
}
});

instance.post("/api/jobs/delete", async (request, response) => {
try {
const deleted = await deleteJobs(request.body.jobIds);
if (deleted) {
response.send({ deleted: true });
} else {
response.send({ message: "Jobs not deleted" });
}
} catch (error) {
response.status(404).send(error);
}
});

instance.post("/api/jobs/create", async (request, response) => {
try {
await createJob(
request.body.jobName,
request.body.jobSchedule,
request.body.jobRepeatEvery,
request.body.jobData
);
response.send({ created: true });
} catch (error) {
response.status(400).send(error);
}
});

done();
};
Loading

0 comments on commit 816993e

Please sign in to comment.