If you're writing a Node app, you might have tasks you want to reoccur periodically. For example, run a cleaning task every Sunday night. Or check for updated weather conditions every day at 4 pm.

A quick walkthrough on how to do that 🧵 Image
There are some ways to solve this.

You could for example use setInterval() to repeat every X seconds. (Please don't do this 🚫)

Or you could a piece of your app being called with the UNIX-native cron, which you need to set up in every machine you're running this on.

(1/8)
There is a better way ➡️ With the npm module node-cron (npmjs.com/package/node-c…).

As the name suggests, node-cron is based on cron which is widely used in the UNIX world do schedule tasks.

(2/8)
It consists of a so-called crontab file. Each line in this file represents an instruction what and when to execute and looks like this:

0 12 * * * /home/marc/task.sh

(3/8)
The syntax looks complicated but is not. The 5 numbers or * represent a piece of the time it should be executed.

Important is that the values are separated by a space. Also, note that the hours are indicated with the 24-hour system.

(4/8) Image
Each field can be:
- A number (e.g. 8)
- A list of numbers (e.g. 3,9,20)
- A range of numbers (e.g. 7-13)
- An asterisk (*), which means the task should run in any instance of the field

Additionally, it can have a "step" operator (/). This can be used to skip some values

(5/8)
30 10 25 12 * echo "merry christmas 🎄"

👆 means: "Run echo "merry christmas 🎄" every December 25th at 10:30am"

We can use the same syntax as `cron` uses, but within our Node.js application.

(6/8)
You first need to install node-cron 👇

npm install --save node-cron

And then schedule a job with cron.schedule

(7/8) const cron = require('node-...
There you have it. The thing that needs most getting used to is the syntax. Once you got that down, scheduling tasks in Node.js takes little effort 💪

(8/8)
If you liked this thread, make sure to give me a follow in order to be notified about more of this 😃

Also, the complete article is also available in blog format:
marc.dev/blog/node-cron

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Marc Backes

Marc Backes Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @themarcba

6 Aug
What is WEB SCRAPING? 🤷‍♂️

To answer this question, I created a small web scraper for Amazon items.

This is a thread that explains step by step how it works 🧵👇

(find the complete code at the end) Image
1. What is web scraping?

A web scraper is a program that scans a website, and reads information from it, rather than using a public API.

2. Why would you use web scraping?

It can be used to retrieve data from a website when no public API is available.

(1/13)
3. Finding out more about the website

Assuming we want to create a web scraper that retrieves information from an item availability.

(2/13) Image
Read 15 tweets
2 Aug
What is a senior developer *REALLY*?

There is a misconception in our industry that the senior developer title is earned by age or time in the company.

I disagree with that approach. Find out what I think a senior developer really is.

🧵👇
1. What a senior developer is NOT ☝️

👉 People that know everything about a programming language
👉 Know all the answers
👉 The absolute truth

(1/12)
2. Problem-solving 💡

👉 Make sure not to introduce unnecessary sources of errors
👉 Create as little friction with the existing system as possible
👉 Think of the bigger picture
👉 Have expandability/reusability in mind
👉 Make decisions about potential trade-offs

(2/12)
Read 14 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(