Skip to content

Commit

Permalink
changed init crons structure for consistency between express-master-c…
Browse files Browse the repository at this point in the history
…ontroller

Signed-off-by: Ijlal Ahmad <[email protected]>
  • Loading branch information
Thre4dripper committed Oct 15, 2024
1 parent e09dd63 commit 73b1390
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src-typescript/config/cronConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ class CronConfig {
/**
* @description Method to initialize the cron jobs
* @param dir - The directory to search for cron jobs
* @param loadCrons - lambda function to load the cron jobs from the directory
*/
static InitCronJobs = async (dir: string) => {
static InitCronJobs = async (dir: string, loadCrons: (pathToCron: string) => void) => {
const entries = await fs.readdir(dir, { withFileTypes: true });

for (const entry of entries) {
const fullPath = path.join(dir, entry.name);

if (entry.isDirectory()) {
await CronConfig.InitCronJobs(fullPath);
await CronConfig.InitCronJobs(fullPath, loadCrons);
} else if (
entry.isFile() &&
(entry.name.endsWith('.cron.ts') || entry.name.endsWith('.cron.js'))
) {
require(fullPath);
loadCrons(fullPath);
}
}
};
Expand Down
7 changes: 6 additions & 1 deletion src-typescript/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ const port = process.env.PORT || 3000;
// End Initialize Socket.IO

// Initialize the cron jobs
await CronConfig.InitCronJobs(path.join(__dirname, 'app/crons'));
await CronConfig.InitCronJobs(path.join(__dirname, 'app/crons'), (pathToCron: string) => {
// configurable import statement to load all the cron jobs before starting server
// This lambda function is called for each cron job file found

require(pathToCron);
});
CronConfig.startCronJobs();

// End Initialize the cron jobs
Expand Down

0 comments on commit 73b1390

Please sign in to comment.