Skip to content

Commit

Permalink
add scheduled event handler for running cron jobs (#6)
Browse files Browse the repository at this point in the history
* add scheduled event handler for running cron jobs

* schedule cron jobs to run every 4 hours

* add strict type checking
  • Loading branch information
RitikJaiswal75 authored Jul 26, 2023
1 parent 7fe81f5 commit 5005347
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { env } from '../types/global.types';

export const handleConfig = (env: env) => {
let baseUrl: string;
if (env.CURRENT_ENVIRONMENT) {
if (env.CURRENT_ENVIRONMENT.toLowerCase() === 'production') {
baseUrl = 'https://api.realdevsquad.com';
} else {
baseUrl = 'https://staging-api.realdevsquad.com';
}
} else {
baseUrl = 'https://staging-api.realdevsquad.com';
}
return { baseUrl };
};
8 changes: 8 additions & 0 deletions src/handlers/scheduledEventHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { handleConfig } from '../config/config';
import { env } from '../types/global.types';

export async function ping(env: env) {
const url = handleConfig(env);
const response = await fetch(`${url.baseUrl}/healthcheck`);
return response;
}
12 changes: 10 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { ping } from './handlers/scheduledEventHandler';
import { env } from './types/global.types';

export default {
// ToDo: remove the eslint disabled after proper implementation
// We need to keep all 3 parameters in this format even if they are not used as as cloudflare workers need them to be present So we are disabling eslint rule of no-unused-vars
// for more details read here: https://community.cloudflare.com/t/waituntil-is-not-a-function-when-using-workers-with-modules/375781/4
// eslint-disable-next-line no-unused-vars
async fetch(request: Request, env: env, ctx: ExecutionContext): Promise<Response> {
async scheduled(req: Request, env: env, ctx: ExecutionContext) {
ctx.waitUntil(ping(env));
},
// We need to keep all 3 parameters in this format even if they are not used as as cloudflare workers need them to be present So we are disabling eslint rule of no-unused-vars
// for more details read here: https://community.cloudflare.com/t/waituntil-is-not-a-function-when-using-workers-with-modules/375781/4
// eslint-disable-next-line no-unused-vars
async fetch(req: Request, env: env, ctx: ExecutionContext): Promise<Response> {
return new Response('Hello World!');
},
};
2 changes: 2 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name = "cron-jobs"
main = "src/worker.ts"
compatibility_date = "2023-07-17"
[triggers]
crons = ["0 */4 * * *"]

# # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
Expand Down

0 comments on commit 5005347

Please sign in to comment.