Today I Learned - Rocky Kev

TIL JS Cron and process.platform

POSTED ON:

TAGS:

I was reading this article: How to use Node.js to backup your personal files (and learn some webdev skills along the way)

Today I learned about Javascript's Cron feature.

Cron is a tool that allows you to execute something on a schedule. This is typically done using the cron syntax. We allow you to execute a function whenever your scheduled job triggers. We also allow you to execute a job external to the javascript process using child_process. Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format. Although the Cron patterns supported here extend on the standard Unix format to support seconds digits, leaving it off will default to 0 and match the Unix behavior.

As well as using process.platform and switching between robocopy if you have win32, or rsync if you have anything else.

import { platform } from 'node:process';

console.log(`This platform is ${platform}`);

Currently possible values are:

via NodeJS docs

require("dotenv").config();
const CronJob = require("cron").CronJob;
const Rsync = require("rsync");

// The value of process.platform will be:
// Windows: win32
// Mac: darwin
// Ubuntu: linux
const syncProgram = process.platform === "win32" ? "robocopy" : "rsync";

// Equivalent to writing `rsync -a example-source/ example-destination/` on terminal
rsync = new Rsync()
.executable(syncProgram)
// The -a flag means "archive" to say we are copying the full directory not just a file
.flags("a")
// Reads from the `.env` file in the project directory
.source(process.env.SOURCE_DIR)
.destination(process.env.DESTINATION_DIR);

const job = new CronJob(
// Run this function once every minute
// To learn more about this cron string visit the below link
// https://crontab.guru/#*_*_*_*_*
process.env.CRON_STRING,
() => {
rsync.execute((error, code, cmd) => {
let result;
if (error) {
// List of rsync status codes
// https://stackoverflow.com/a/20738063
result = `Code ${code} ${error?.message}`;
} else {
result = "Backup complete";
}

const currentDate = new Date().toISOString();
// Write log to the console, or will be redirected to a
// nohup.out file if using nohup
process.stdout.write(`${currentDate}: ${result}\n`);
});
},
null,
true,
// Replace with your time zone
// https://gist.github.com/diogocapela/12c6617fc87607d11fd62d2a4f42b02a
"America/Toronto"
);

// Begin the cronjob
job.start();

Notice the changes above. I did a check against process.platform and if it returns win32 we set the executable copy program to robocopy instead of rsync which will allow it to run on Windows.


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL keywords in package.json

Today I learned what keywords are in a package.json file! It's a collection of keywords about a module. Keywords can help identify a package, related modules and software, and concepts.

TIL functional async/await

PLACEHOLDER