Skip to content

Commit

Permalink
Update tests and standalone agendash with @cawstudios/agenda
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinoothna Kinnera committed Sep 21, 2024
1 parent 9252090 commit d0a91ee
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 251 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ name: CI
on: [push, pull_request]
jobs:
test:
name: Node ${{ matrix.node }}, Mongo ${{ matrix.mongodb-version }}, Agenda ${{ matrix.agenda-version }}
name: agendash
runs-on: ubuntu-latest
strategy:
matrix:
node: ['14', '16', '18']
mongodb-version: [ 3.6, 4.0, 4.2, 4.4, 5.0 ]
agenda-version: [ 3.1, 4.0 ]
# agenda-version: [ 3.1, 4.0 ]
steps:
- uses: actions/checkout@v2

Expand All @@ -25,7 +25,7 @@ jobs:
- run: npm install

- name: Install agenda
run: npm i agenda@${{ matrix.agenda-version }}
run: npm i "@cawstudios/agenda"

- run: npm test
env:
Expand Down
106 changes: 64 additions & 42 deletions bin/agendash-standalone-fastify.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,83 @@
#!/usr/bin/env node
"use strict";
const Agenda = require("agenda");
const { Agenda, DataSource } = require("@cawstudios/agenda");
const agendash = require("../app");
const { Command } = require("commander");
const Fastify = require("fastify");
const program = require("commander");

const program = new Command();

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 /",
"/"
)
.option("-t, --dataSourceType <type>", "Data source type (mongo or postgres)", /^(mongo|postgres)$/i)
.option("-d, --db <connection>", "DB connection string")
.option("-c, --collection <name>", "Mongo collection name", "agendaJobs")
.option("-p, --port <number>", "Server port", parseInt, 3000)
.option("-t, --title <string>", "Page title", "Agendash")
.option("-p, --path <path>", "Path to bind Agendash to", "/")
.parse(process.argv);

if (!program.db) {
const options = program.opts();

// Validation
if (!options.dataSourceType) {
console.error("--dataSourceType required (mongo or postgres)");
process.exit(1);
}

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

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

const fastify = Fastify();
// DB Configuration
const dbConfig = {
[DataSource.POSTGRES]: {
dataSource: DataSource.POSTGRES,
dataSourceOptions: {
db: {
connectionString: options.db
}
}
},
[DataSource.MONGO]: {
dataSource: DataSource.MONGO,
dataSourceOptions: { db: { address: options.db, collection: options.collection } }
}
}[options.dataSourceType];

const agenda = new Agenda().database(program.db, program.collection);
if (!dbConfig) {
console.error("Unsupported data source");
process.exit(1);
}

async function startServer() {
try {
const fastify = Fastify();

const agenda = new Agenda();
await agenda.database(dbConfig);

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

fastify.listen({ port: program.port }, function () {
console.log(
`Agendash started http://localhost:${program.port}${program.path}`
);
});
} catch (error) {
console.error("Failed to start Agendash:", error);
process.exit(1);
}
}

fastify.listen({ port: program.port }, function () {
console.log(
`Agendash started http://localhost:${program.port}${program.path}`
);
});
startServer();
70 changes: 46 additions & 24 deletions bin/agendash-standalone-hapi.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,67 @@
#!/usr/bin/env node
"use strict";
const { Agenda } = require("agenda");
const { Agenda, DataSource } = require("@cawstudios/agenda");
const agendash = require("../app");
const Hapi = require("@hapi/hapi");
const program = require("commander");
const { Command } = require("commander");

const program = new Command();
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("-t, --dataSourceType <type>", "Data source type (mongo or postgres)", /^(mongo|postgres)$/i)
.option("-d, --db <connection>", "DB connection string")
.option("-c, --collection <name>", "Mongo collection name", "agendaJobs")
.option("-p, --port <number>", "Server port", parseInt, 3000)
.option("-t, --title <string>", "Page title", "Agendash")
.option("-p, --path <path>", "Path to bind Agendash to", "/")
.parse(process.argv);

if (!program.db) {
const options = program.opts();
// Validation
if (!options.dataSourceType) {
console.error("--dataSourceType required (mongo or postgres)");
process.exit(1);
}

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

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

// DB Configuration
const dbConfig = {
[DataSource.POSTGRES]: {
dataSource: DataSource.POSTGRES,
dataSourceOptions: {
db: {
connectionString: options.db
}
}
},
[DataSource.MONGO]: {
dataSource: DataSource.MONGO,
dataSourceOptions: { db: { address: options.db, collection: options.collection } }
}
}[options.dataSourceType];

if (!dbConfig) {
console.error("Unsupported data source");
process.exit(1);
}


const init = async () => {
const server = Hapi.server({
port: 3002,
host: "localhost",
});

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

await server.register(require("@hapi/inert"));
await server.register(
Expand Down
69 changes: 45 additions & 24 deletions bin/agendash-standalone-koa.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
#!/usr/bin/env node
"use strict";
const { Agenda } = require("agenda");
const { Agenda, DataSource } = require("@cawstudios/agenda");
const agendash = require("../app");
const Koa = require("koa");
const program = require("commander");
const { Command } = require("commander");

const program = new Command();
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("-t, --dataSourceType <type>", "Data source type (mongo or postgres)", /^(mongo|postgres)$/i)
.option("-d, --db <connection>", "DB connection string")
.option("-c, --collection <name>", "Mongo collection name", "agendaJobs")
.option("-p, --port <number>", "Server port", parseInt, 3000)
.option("-t, --title <string>", "Page title", "Agendash")
.option("-p, --path <path>", "Path to bind Agendash to", "/")
.parse(process.argv);

if (!program.db) {
const options = program.opts();
// Validation
if (!options.dataSourceType) {
console.error("--dataSourceType required (mongo or postgres)");
process.exit(1);
}

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

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

// DB Configuration
const dbConfig = {
[DataSource.POSTGRES]: {
dataSource: DataSource.POSTGRES,
dataSourceOptions: {
db: {
connectionString: options.db
}
}
},
[DataSource.MONGO]: {
dataSource: DataSource.MONGO,
dataSourceOptions: { db: { address: options.db, collection: options.collection } }
}
}[options.dataSourceType];

if (!dbConfig) {
console.error("Unsupported data source");
process.exit(1);
}

const init = async () => {
const agenda = new Agenda().database(program.db, program.collection);
const agenda = new Agenda();
await agenda.database(dbConfig);

const app = new Koa();
const middlewares = agendash(agenda, {
Expand Down
24 changes: 18 additions & 6 deletions lib/controllers/agendash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";
const semver = require("semver");
// const semver = require("semver");
const { ObjectId } = require("mongodb"); // We rely on the Agenda's "mongodb", thus our package.json lists "*" as required version

module.exports = function (agenda, options) {
Expand Down Expand Up @@ -110,7 +110,13 @@ module.exports = function (agenda, options) {
return Promise.reject(new Error("Agenda instance is not ready"));
}

const jobs = await agenda.db.getJobs({ '_id': { 'IN': jobIds.map((jobId) => jobId) }})
const jobs = await agenda.db.getJobs({ _id: { 'IN': jobIds.map((jobId) => {
try {
return new ObjectId(jobId);
} catch (error) {
return jobId;
}
}) }})
if (jobs.length === 0) {
throw new Error("Job not found");
}
Expand All @@ -124,13 +130,19 @@ module.exports = function (agenda, options) {
return "Jobs create successfully";
};

const deleteJobs = (jobIds) => {
const deleteJobs = async (jobIds) => {
if (!agenda) {
return Promise.reject(new Error("Agenda instance is not ready"));
return new Error("Agenda instance is not ready");
}

return agenda.cancel({
_id: { 'IN': jobIds.map((jobId) => jobId) },
return await agenda.cancel({
_id: { 'IN': jobIds.map((jobId) => {
try {
return new ObjectId(jobId);
} catch (error) {
return jobId;
}
}) },
});
};

Expand Down
19 changes: 19 additions & 0 deletions test/helpers/test-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { DataSource, Agenda } = require("@cawstudios/agenda");

async function setupAgenda() {
const agenda = new Agenda({
dataSource: DataSource.MONGO,
dataSourceOptions: {
db: {
address: "mongodb://127.0.0.1/agendash-test-db",
collection: "agendash-test-collection",
}
}
});

await agenda.start();
return agenda;
}


module.exports = { setupAgenda };
Loading

0 comments on commit d0a91ee

Please sign in to comment.