diff --git a/src/config/config.ts b/src/config/config.ts new file mode 100644 index 0000000..48446f4 --- /dev/null +++ b/src/config/config.ts @@ -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 }; +}; diff --git a/src/handlers/scheduledEventHandler.ts b/src/handlers/scheduledEventHandler.ts new file mode 100644 index 0000000..290112e --- /dev/null +++ b/src/handlers/scheduledEventHandler.ts @@ -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; +} diff --git a/src/worker.ts b/src/worker.ts index 1792e5d..e90e029 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -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 { + 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 { return new Response('Hello World!'); }, }; diff --git a/wrangler.toml b/wrangler.toml index fad2c39..c6455e0 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -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]]