-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update tests and standalone agendash with @cawstudios/agenda
- Loading branch information
Vinoothna Kinnera
committed
Sep 21, 2024
1 parent
9252090
commit d0a91ee
Showing
10 changed files
with
319 additions
and
251 deletions.
There are no files selected for viewing
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
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
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(); |
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
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
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
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
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 }; |
Oops, something went wrong.