Scheduled tasks let your program do things automatically at set times. This helps you run jobs like backups or reminders without needing to start them manually.
Scheduled tasks (cron) in Deno
Introduction
Syntax
Deno
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts"; const cron = new Cron("* * * * *", () => { // code to run every minute }); cron.start();
The string "* * * * *" is a cron expression that sets when the task runs.
Use cron.start() to begin the scheduled task.
Examples
Deno
const cron = new Cron("0 8 * * *", () => { console.log("Good morning! Time to start work."); }); cron.start();
Deno
const cron = new Cron("*/15 * * * *", () => { console.log("Checking status every 15 minutes."); }); cron.start();
Deno
const cron = new Cron("0 0 * * 0", () => { console.log("Weekly report generated."); }); cron.start();
Sample Program
This program prints a message every minute. After 3 minutes, it stops the cron job and prints a stop message.
Deno
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts"; const cron = new Cron("*/1 * * * *", () => { console.log("Task runs every minute."); }); cron.start(); // Keep the program running setTimeout(() => { cron.stop(); console.log("Cron stopped after 3 minutes."); }, 180000);
Important Notes
Make sure your program keeps running to allow scheduled tasks to execute.
You can stop a cron job anytime with cron.stop().
Cron expressions have 5 parts: minute, hour, day of month, month, day of week.
Summary
Scheduled tasks automate running code at set times.
Deno uses the croner library to create cron jobs.
Start tasks with cron.start() and stop with cron.stop().
Practice
1. What is the main purpose of using scheduled tasks (cron) in Deno?
easy
Solution
Step 1: Understand scheduled tasks
Scheduled tasks run code automatically without manual intervention.Step 2: Identify cron job purpose in Deno
Deno's cron jobs automate running code at set times or intervals.Final Answer:
To run code automatically at specific times or intervals -> Option DQuick Check:
Scheduled tasks = automatic timed code run [OK]
Hint: Scheduled tasks run code on a timer, not manually [OK]
Common Mistakes:
- Confusing scheduled tasks with manual triggers
- Thinking cron is for debugging
- Assuming cron speeds up compilation
2. Which of the following is the correct way to import the croner library in a Deno script?
easy
Solution
Step 1: Check Deno import style
Deno uses ES module imports with URLs, not CommonJS require.Step 2: Identify correct croner import
The official croner import uses a URL with version and named import.Final Answer:
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts"; -> Option CQuick Check:
Deno imports use URLs and named imports [OK]
Hint: Deno imports use full URLs, not short names or require [OK]
Common Mistakes:
- Using require() which is Node.js style
- Importing from 'cron' instead of 'croner'
- Using wildcard import without URL
3. What will the following Deno croner code output when run?
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";
const cron = new Cron("* * * * *", () => {
console.log("Hello every minute");
});
cron.start();
medium
Solution
Step 1: Understand the cron pattern "* * * * *"
This pattern means the task runs every minute.Step 2: Analyze the callback behavior
The callback logs "Hello every minute" each time the cron triggers.Final Answer:
Prints "Hello every minute" every minute -> Option BQuick Check:
Cron pattern "* * * * *" = every minute [OK]
Hint: Five stars in cron means every minute [OK]
Common Mistakes:
- Thinking it runs every second
- Expecting immediate output without waiting
- Confusing cron syntax with other formats
4. Identify the error in this Deno croner code snippet:
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";
const cron = new Cron("60 * * * *", () => {
console.log("This runs every hour");
});
cron.start();
medium
Solution
Step 1: Check the cron pattern validity
Minutes field must be between 0 and 59; "60" is invalid.Step 2: Confirm other code parts
cron.start() does not require await; callback can be sync; hourly tasks are supported.Final Answer:
The cron pattern "60 * * * *" is invalid because minutes must be 0-59 -> Option AQuick Check:
Minutes range = 0-59, 60 is invalid [OK]
Hint: Minutes in cron must be 0-59, not 60 or above [OK]
Common Mistakes:
- Using invalid minute value 60
- Thinking cron.start() needs await
- Assuming callback must be async
5. You want to schedule a Deno task to run every weekday at 9:30 AM using croner. Which cron pattern is correct?
hard
Solution
Step 1: Understand cron fields order
Cron fields are: minute, hour, day of month, month, day of week.Step 2: Match 9:30 AM on weekdays
Minute = 30, hour = 9, day of week = 1-5 (Monday to Friday).Final Answer:
"30 9 * * 1-5" -> Option AQuick Check:
Minute=30, Hour=9, Weekdays=1-5 [OK]
Hint: Minute first, hour second, weekdays 1-5 for Mon-Fri [OK]
Common Mistakes:
- Swapping hour and minute fields
- Using weekend days 6,7 instead of weekdays
- Setting minute or hour to zero incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
