Scheduled Events
Handle cron triggers in Cloudflare Workers.
Vela supports Cloudflare Workers scheduled events (cron triggers) through the @Scheduled() decorator and the CloudflareApplication.scheduled() handler.
Setup
Define a cron schedule in wrangler.toml:
[triggers]
crons = ["*/5 * * * *"]Defining Scheduled Tasks
Use the @Scheduled() decorator on a method to register it as a cron handler:
import { Injectable } from '@velajs/vela';
import { Scheduled } from '@velajs/cloudflare';
@Injectable()
class CleanupService {
@Scheduled('*/5 * * * *')
async cleanup(event: ScheduledEvent) {
console.log('Running cleanup at', event.scheduledTime);
// perform periodic work
}
}Register the service in your module:
import { Module } from '@velajs/vela';
@Module({
providers: [CleanupService],
})
class AppModule {}Application Handler
The CloudflareApplication automatically exposes a .scheduled() handler. When you export the application as the default, Workers invokes it on each cron trigger:
import { CloudflareFactory } from '@velajs/cloudflare';
import { AppModule } from './app.module';
const app = CloudflareFactory.create(AppModule);
export default app;The app.scheduled(event, env, ctx) method is called by the Workers runtime and delegates to every method decorated with @Scheduled() whose cron expression matches the trigger.
Hyperdrive
Use Cloudflare Hyperdrive for accelerated database connections in your Vela application.
Testing
Unit and integration testing for Vela applications.
Last updated on