Schedule

There will be times where you have tasks that need to be executed on a fixed schedule. Setting up crontab or keeping track of intervals and timeouts can be a pain.

Core makes running recurring tasks easy by providing a task scheduling manager that allows tasks to be scheduled based on times or block events.

Prerequisites

Before we start, we need to establish what a few recurring variables and imports in this document refer to when they are used.

1import { app, Container, Services } from "@arkecosystem/core-kernel";
  • The app import refers to the application instance which grants access to the container, configurations, system information and more.
  • The Container import refers to a namespace that contains all of the container specific entities like binding symbols and interfaces.
  • The Services import refers to a namespace that contains all of the core services. This generally will only be needed for type hints as Core is responsible for service creation and maintenance.

Cron Job

Get an instance of a CronJob

1const cronJob: Services.Schedule.CronJob = app
2 .get<Services.Schedule.ScheduleService>(Container.Identifiers.ScheduleService)
3 .cron()

Schedule the job to run every minute

1cronJob
2 .everyMinute()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every five minutes

1cronJob
2 .everyFiveMinutes()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every ten minutes

1cronJob
2 .everyTenMinutes()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every fifteen minutes

1cronJob
2 .everyFifteenMinutes()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every thirty minutes

1cronJob
2 .everyThirtyMinutes()
3 .execute(() => console.log("Hello World"))

Schedule the job to run hourly

1cronJob
2 .hourly()
3 .execute(() => console.log("Hello World"))

Schedule the job to run hourly at a given offset in the hour

1cronJob
2 .hourlyAt(minute: string)
3 .execute(() => console.log("Hello World"))

Schedule the job to run daily

1cronJob
2 .daily()
3 .execute(() => console.log("Hello World"))

Schedule the job to run daily at a given time (10:00, 19:30, etc)

1cronJob
2 .dailyAt(hour: string, minute: string)
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on weekdays

1cronJob
2 .weekdays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on weekends

1cronJob
2 .weekends()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on Mondays

1cronJob
2 .mondays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on Tuesdays

1cronJob
2 .tuesdays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on Wednesdays

1cronJob
2 .wednesdays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on Thursdays

1cronJob
2 .thursdays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on Fridays

1cronJob
2 .fridays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on Saturdays

1cronJob
2 .saturdays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run only on Sundays

1cronJob
2 .sundays()
3 .execute(() => console.log("Hello World"))

Schedule the job to run weekly

1cronJob
2 .weekly()
3 .execute(() => console.log("Hello World"))

Schedule the job to run weekly on a given day and time

1cronJob
2 .weeklyOn(day: string, hour: string, minute: string)
3 .execute(() => console.log("Hello World"))

Schedule the job to run monthly

1cronJob
2 .monthly()
3 .execute(() => console.log("Hello World"))

Schedule the job to run monthly on a given day and time

1cronJob
2 .monthlyOn(day: string, hour: string, minute: string)
3 .execute(() => console.log("Hello World"))

Schedule the job to run quarterly

1cronJob
2 .quarterly()
3 .execute(() => console.log("Hello World"))

Schedule the job to run yearly

1cronJob
2 .yearly();
3 .execute(() => console.log("Hello World"))

Block Job

Get an instance of a BlockJob

1const blockJob: Services.Schedule.BlockJob = app
2 .get<Services.Schedule.ScheduleService>(Container.Identifiers.ScheduleService)
3 .block()

Schedule the job to run every block

1blockJob
2 .everyBlock()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every five blocks

1blockJob
2 .everyFiveBlocks()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every ten blocks

1blockJob
2 .everyTenBlocks()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every fifteen blocks

1blockJob
2 .everyFifteenBlocks()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every thirty blocks

1blockJob
2 .everyThirtyBlocks()
3 .execute(() => console.log("Hello World"))

Schedule the job to run every round

1blockJob
2 .everyRound()
3 .execute(() => console.log("Hello World"))
Last updated 3 years ago
Edit Page
Share: