Skip to content

Commit

Permalink
refactor: remove schedule.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
supersonictw committed Apr 7, 2024
1 parent d8375be commit b606fe9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 39 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# App config files
.env
schedule.toml

# Finder config file
.DS_Store
Expand Down
9 changes: 9 additions & 0 deletions app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ const pluginPromises = [
loadDatabase(),
];

// Define schedule tasks
const scheduleTasks = [{
name: "refresh",
config: {
time: "0 6 * * *",
},
}];

// Define router names
const routerNames = [
"root",
Expand All @@ -40,6 +48,7 @@ const displayStatus = (protocolStatus) => {
// Mount application and execute it
invokeApp().
loadPromises(pluginPromises).
loadTasks(scheduleTasks).
loadRoutes(routerNames).
execute().
then(displayStatus);
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"http-status-codes": "^2.2.0",
"node-cache": "^5.1.2",
"node-schedule": "^2.1.1",
"request-ip": "^3.3.0",
"toml": "^3.0.0"
"request-ip": "^3.3.0"
},
"devDependencies": {
"@commitlint/cli": "^17.4.4",
Expand Down
2 changes: 0 additions & 2 deletions schedule.sample.toml

This file was deleted.

22 changes: 19 additions & 3 deletions src/execute.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from "./init/express.mjs";

import {
readScheduleMap,
startScheduleTasks,
} from "./init/schedule.mjs";

Expand Down Expand Up @@ -64,6 +63,7 @@ async function setupHttpsProtocol(app) {
export function invokeApp() {
return {
loadPromises,
loadTasks,
loadRoutes,
execute,
};
Expand All @@ -72,6 +72,9 @@ export function invokeApp() {
// Define preparing promises
const preparingPromises = [];

// Define preparing tasks
const scheduleTasks = [];

/**
* Load promises to be executed before running the application.
* @param {Promise[]} promises the promises to load
Expand All @@ -86,6 +89,20 @@ function loadPromises(promises) {
return invokeApp();
}

/**
* Load tasks to be executed before running the application.
* @param {object[]} tasks the tasks to load
* @return {object} the application invoker
*/
function loadTasks(tasks) {
if (tasks.length < 1) {
return invokeApp();
}

scheduleTasks.push(...tasks);
return invokeApp();
}

/**
* Load routes from specified router names.
* @param {string[]} routerNames the names of the routers to load
Expand Down Expand Up @@ -115,8 +132,7 @@ async function execute() {
await Promise.all(preparingPromises);

// Start schedule tasks
readScheduleMap();
startScheduleTasks();
await startScheduleTasks(scheduleTasks);

// Get enabled protocols
const enabledProtocols = getSplited("ENABLED_PROTOCOLS");
Expand Down
34 changes: 3 additions & 31 deletions src/init/schedule.mjs
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
// node-schedule is a flexible cron-like and not-cron-like job scheduler.

// Import modules
import {
existsSync,
readFileSync,
} from "node:fs";

import {
parse as parseTOML,
} from "toml";

import schedule from "node-schedule";

export const scheduleMap = {};

/**
* Reads the schedule.toml file and returns a parsed map of the contents.
*/
export async function readScheduleMap() {
const mapFilename = new URL("../../schedule.toml", import.meta.url);
if (!existsSync(mapFilename)) {
throw Error("schedule.toml not exists");
}

const mapFileContent = readFileSync(mapFilename, "utf-8");
if (!mapFileContent) {
throw Error("schedule.toml is invalid");
}

const srcMap = parseTOML(mapFileContent);
Object.assign(scheduleMap, srcMap);
}

/**
* Starts the schedule tasks.
* @param {object[]} tasks the tasks to load
*/
export async function startScheduleTasks() {
for (const [methodName, methodConfig] of Object.entries(scheduleMap)) {
export async function startScheduleTasks(tasks) {
for (const {name: methodName, config: methodConfig} of tasks) {
const methodDirectory = new URL("../tasks/", import.meta.url);
const methodFilename = new URL(`${methodName}.mjs`, methodDirectory);

Expand Down

0 comments on commit b606fe9

Please sign in to comment.