The big guys in the group are already talking about “Deno is a JavaScript runtime going in the wrong direction “, and I, a Copy raider with years of experience, still can only read the bits and pieces of knowledge, and then share them and get criticized by the big guys. Helpless, with the current Copy siege lion on JavaScript master degree, also only with API call division level. Some people are born Kings; Some people from bronze step by step “to the CEO win white rich beauty to the peak of life “; Some people, who are also bronze, will still be bronze ten years later. “Slow progress means backward”, and they are reduced step by step to “crazy life”. So let’s see how the CronJob in Deno says “Hello World”.

What is a CronJob

A CronJob is a scheduled task. Similar to the Crontab in Linux, it runs a specified task in a specified period of time. For example, Xiao Ming would greet her long-distance girlfriend and send warm messages every night. For example, when “Aluminum friend Rainbow Fart Bot” meets Deno, it sends emails regularly depending on the periodic trigger function provided by Serverless platform. For example, in some service scheduling scripts, such as Schedule: “*/1 * * * *”, maybe that’s the CronJob. Essentially a CronJob is a scheduler that allows applications to schedule jobs to run automatically on a specific date or time. Today, we will integrate CronJob into the Deno application, would you like to have a look?

Install Deno

The previous article basically did not mention the installation of Deno, in China, we use “VScode – the father of Deno” JJC boss for us to provide a mirror service for installation, the address is https://x.deno.js.cn/, simple installation tutorial is as follows:

# install the latest version of the # # using Shell: curl - fsSL https://x.deno.js.cn/install.sh | sh # # use PowerShell: Iwr https://x.deno.js.cn/install.ps1 - useb | iex # to install a particular version # # using Shell: The curl - fsSL v1.0.0 # # https://x.deno.js.cn/install.sh | sh - s using PowerShell: $v = "1.0.0"; Iwr https://x.deno.js.cn/install.ps1 - useb | iex # verified deno installation -- helpCopy the code

deno_cron

Deno currently has three libraries for cron in its official third-party plug-in library, with deno_cron currently 35 stars. Deno_cron is an intelligent Cron job scheduler library used in Deno that allows you to write readable CRon syntax with a great deal of flexibility. Writing cron syntax and operations can be tedious for many developers. This plug-in provides a very developer-friendly API for writing cron syntax for any job planner you need.

Can be introduced by:

import {cron, daily, monthly, biweekly, weekly, hourly, every15Minute, everyMinute, start, stop } from 'https://deno.land/x/deno_cron/cron.ts';
Copy the code

The core contains a large number of blank lines, about 168, which reminds us of the NPM package IS-Promise. This two-line package is being downloaded tens of thousands of times a week. Hopefully, this will never happen again!

We can use Cron to define custom schedules in Deno, such as:

cron('* * * * * *', () => { // run some task }); # of * * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ └ week (0 to 7) on Sunday (0 or 7 is │ │ │ │ └ ─ ─ ─ ─ ─ month (1-12) │ │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ day (1-31) │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ when (0-23) │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ points (0-59) └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ seconds (0-59) - [Optional default 01]Copy the code

What are these asterisks for?

  • The number of seconds occupied by the first asterisk. The value ranges from 0 to 59
  • The second asterisk uses the number of minutes and ranges from 0 to 59
  • The third asterisk is the number of hours used, with a value between 0 and 23
  • The fourth asterisk is the day of the month and has a value between 1 and 31
  • The fifth asterisk is the month of the year and has a value between 1 and 12
  • The sixth asterisk is the day of the week and ranges from 0 to 7

For example, print messages every second and i++ values:

import { cron } from 'https://deno.land/x/deno_cron/cron.ts';

let i = 0;
cron('*/1 * * * * *', () => {
    // run some task
    console.log('This is a same thing', i++)
});
Copy the code

Of course, in addition to setting *, we can call other apis, such as using everyMinute to perform minute-by-minute tasks:

import { everyMinute } from 'https://deno.land/x/deno_cron/cron.ts';

everyMinute(() => {
    console.log(new Date())
})
Copy the code

You can also call start() and stop() to start and stop all cronjobs, respectively:

import { cron, start,stop } from 'https://deno.land/x/deno_cron/cron.ts'; let i = 0; cron('*/1 * * * * *', () => { // run some task console.log('This is a same thing', i++) if (i ! = 5) { start() } else { console.log('Stop! ') stop() } });Copy the code

summary

Did you lose cron in deno? Today’s share is here, I hope you are interested in it can look at the source code, if you have inspired welcome comment area and I discuss, if there are deficiencies, welcome criticism and correction!

Github.com/rbrahul/den…