Skip to content

Commit

Permalink
correctly exit jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Oct 20, 2024
1 parent afcfacd commit 9847c64
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions apps/jobs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,29 @@ const runJob = async (job: string) => {
logger.error(`Error running job ${job}: ${error.message}`, error);
}
};
const main = () => {
const main = async () => {
const parsedValues = parseJobArgs();
const jobSchedulePairs = parseJobSchedulePairs(parsedValues);

logger.info(
`Starting jobs: ${jobSchedulePairs.map((pair) => pair.job).join(", ")}`,
);

if (parsedValues.runOnce) {
const jobPromises = jobSchedulePairs.map(({ job }) => {
if (job == null) return Promise.resolve();
logger.info(`Running job ${job} once`);
return runJob(job);
});

await Promise.all(jobPromises);
logger.info("All jobs completed. Exiting.");
process.exit(0);
}

for (const { job, schedule } of jobSchedulePairs) {
if (job == null) continue;

if (parsedValues.runOnce) {
logger.info(`Running job ${job} once`);
runJob(job);
continue;
}
const cronJob = new CronJob(schedule, () => runJob(job));

cronJob.start();
Expand Down

0 comments on commit 9847c64

Please sign in to comment.