Replies: 3 comments 8 replies
-
| 
         I'm not sure what you meant by "cronjob", but Hono is a slim framework, almost like a router, so it doesn't have a cron function. However, if you were referring to Cloudflare Triggers, you can write it as follows: export default {
  fetch: app.fetch,
  async scheduled(event, env, ctx) {
    ctx.waitUntil(doSomeTaskOnASchedule())
  }
} | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         I got this working nicely with something like this (in typescript): note that cronTask is just a function called on the cron schedule. You can use whatever works for you. I use the waitUntil method so I can guarantee to finish the cronTask function processing before the cloud function is terminated (all of this is running on Cloudflare workers): export default {
/** this part manages cronjobs */
  scheduled(
    event: ScheduledEvent,
    env: {
      DB?: any;
      SENDGRID_API_KEY: any;
    },
    ctx: ExecutionContext
  ) {
    const delayedProcessing = async () => {
      await cronTask(env);
    };
    ctx.waitUntil(delayedProcessing());
  },
/** this part manages regular REST */
  fetch(request: Request, env: Env, ctx: ExecutionContext) {
    return app.fetch(request, env, ctx);
  },
};in plain JS this should look something like this (js code untested): export default {
  scheduled(event, env, ctx) {
    const delayedProcessing = async () => {
      await cronTask(env)
    }
    ctx.waitUntil(delayedProcessing())
  },
  fetch(request, env, ctx) {
    return app.fetch(request, env, ctx)
  }
} | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Check this out: https://github.com/AustinZhu/kuron I have made a lightweight cron package for Cloudflare Cron Triggers, the APIs are designed to look familiar to Hono users. import { Hono } from 'hono';
import { Cron } from 'kuron';
const app = new Hono<Env>()
  .get('/health', (c) => c.text('OK'));
const cron = new Cron<Env>()
  .schedule('0 0 * * *', async (c) => {
    console.log('Daily midnight job');
  })
  .schedule('0 12 * * *', async (c) => {
    console.log('Daily noon job');
  })
  .schedule('0 0 * * 0', async (c) => {
    console.log('Weekly Sunday job');
  });
export default {
  fetch: app.fetch,
  scheduled: cron.scheduled,
}; | 
  
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How can I create cronjob in Hono guys?
Beta Was this translation helpful? Give feedback.
All reactions