Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Task Schedules #3557

Closed
faustbrian opened this issue Mar 2, 2020 · 1 comment
Closed

Integrate Task Schedules #3557

faustbrian opened this issue Mar 2, 2020 · 1 comment

Comments

@faustbrian
Copy link
Contributor

faustbrian commented Mar 2, 2020

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 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

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.

@faustbrian faustbrian added this to the 3.0.0 milestone Mar 2, 2020
@faustbrian
Copy link
Contributor Author

Closing this as no one picked it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant