-
Notifications
You must be signed in to change notification settings - Fork 280
Description
There will be times where you have tasks that need to be executed on a fixed schedule like checking peer health or aggregating data and storing it. 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.
import { app, Container, Services } from "@arkecosystem/core-kernel";- The
appimport refers to the application instance which grants access to the container, configurations, system information and more. - The
Containerimport refers to a namespace that contains all of the container-specific entities like binding symbols and interfaces. - The
Servicesimport 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
const cronJob: Services.Schedule.CronJob = app
.get<Services.Schedule.ScheduleService>(Container.Identifiers.ScheduleService)
.cron()Schedule the job to run every minute
cronJob
.everyMinute()
.execute(() => console.log("Hello World"))Schedule the job to run every five minutes
cronJob
.everyFiveMinutes()
.execute(() => console.log("Hello World"))Schedule the job to run every ten minutes
cronJob
.everyTenMinutes()
.execute(() => console.log("Hello World"))Schedule the job to run every fifteen minutes
cronJob
.everyFifteenMinutes()
.execute(() => console.log("Hello World"))Schedule the job to run every thirty minutes
cronJob
.everyThirtyMinutes()
.execute(() => console.log("Hello World"))Schedule the job to run hourly
cronJob
.hourly()
.execute(() => console.log("Hello World"))Schedule the job to run hourly at a given offset in the hour
cronJob
.hourlyAt(minute: string)
.execute(() => console.log("Hello World"))Schedule the job to run daily
cronJob
.daily()
.execute(() => console.log("Hello World"))Schedule the job to run daily at a given time (10:00, 19:30, etc)
cronJob
.dailyAt(hour: string, minute: string)
.execute(() => console.log("Hello World"))Schedule the job to run only on weekdays
cronJob
.weekdays()
.execute(() => console.log("Hello World"))Schedule the job to run only on weekends
cronJob
.weekends()
.execute(() => console.log("Hello World"))Schedule the job to run only on Mondays
cronJob
.mondays()
.execute(() => console.log("Hello World"))Schedule the job to run only on Tuesdays
cronJob
.tuesdays()
.execute(() => console.log("Hello World"))Schedule the job to run only on Wednesdays
cronJob
.wednesdays()
.execute(() => console.log("Hello World"))Schedule the job to run only on Thursdays
cronJob
.thursdays()
.execute(() => console.log("Hello World"))Schedule the job to run only on Fridays
cronJob
.fridays()
.execute(() => console.log("Hello World"))Schedule the job to run only on Saturdays
cronJob
.saturdays()
.execute(() => console.log("Hello World"))Schedule the job to run only on Sundays
cronJob
.sundays()
.execute(() => console.log("Hello World"))Schedule the job to run weekly
cronJob
.weekly()
.execute(() => console.log("Hello World"))Schedule the job to run weekly on a given day and time
cronJob
.weeklyOn(day: string, hour: string, minute: string)
.execute(() => console.log("Hello World"))Schedule the job to run monthly
cronJob
.monthly()
.execute(() => console.log("Hello World"))Schedule the job to run monthly on a given day and time
cronJob
.monthlyOn(day: string, hour: string, minute: string)
.execute(() => console.log("Hello World"))Schedule the job to run quarterly
cronJob
.quarterly()
.execute(() => console.log("Hello World"))Schedule the job to run yearly
cronJob
.yearly();
.execute(() => console.log("Hello World"))Block Job
Get an instance of a BlockJob
const blockJob: Services.Schedule.BlockJob = app
.get<Services.Schedule.ScheduleService>(Container.Identifiers.ScheduleService)
.block()Schedule the job to run every block.
blockJob
.everyBlock()
.execute(() => console.log("Hello World"))Schedule the job to run every five blocks.
blockJob
.everyFiveBlocks()
.execute(() => console.log("Hello World"))Schedule the job to run every ten blocks.
blockJob
.everyTenBlocks()
.execute(() => console.log("Hello World"))Schedule the job to run every fifteen blocks.
blockJob
.everyFifteenBlocks()
.execute(() => console.log("Hello World"))Schedule the job to run every thirty blocks.
blockJob
.everyThirtyBlocks()
.execute(() => console.log("Hello World"))Schedule the job to run every round.
blockJob
.everyRound()
.execute(() => console.log("Hello World"))Integration
Currently, we only have one recurring task which is that we ping some peers every X minutes. It works based on a variable named nextUpdateNetworkStatusScheduled inside the NetworkMonitor class and is based on a fixed schedule so it would be an obvious candidate for the task scheduling.