Dieses Blog durchsuchen

Mittwoch, 17. August 2016

node.js: install simple sheduler as a rebootsafe service on ubuntu 16.04

There are many situations, in whioch you want to process a serverside workerscript to process queued tasks. Here is a simple solution to setup a nodejs cron script wich runs as a systemd service on ubuntu.

In this sample we are logging a text every second to the console, the filesystem and to the system d status log.


Prerequisits

Installed node.js, npm


Create your project

create a folder "/var/www/html/sheduler"

Init a npm project

create a package.json with following content

{
"name": "sheduler",
"version": "1.0.0",
"description": "a simple sheduler",
"main": "index.js",
"dependencies": {
"chai": "^3.5.0",
"mocha": "^3.0.2",
"node-cron": "^1.1.1",
"winston": "^2.2.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"cron",
"node"
],
"author": "pboethig",
"license": "MIT",
"bugs": {
"url": "https://github.com/pboethig/node-cron/issues"
},
"homepage": "https://github.com/pboethig/node-cron#readme"
}
view raw package.json hosted with ❤ by GitHub
open a terminal and run
$ npm install

Install the sheduler

We want to use node-cron for our sheduler.  Node-cron allows us to trigger a script every second. That is ideal, if you want to build a worker wich runs every second. install follwing script 

var winston = require('winston');
var logger = new (winston.Logger)({
transports:[
new winston.transports.File({
level: 'info',
filename: '/var/www/html/sheduler/all-logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level:'debug',
handleExceptions:true,
json: false,
colorize:true
})
],
exitOnError:false
});
module.exports = logger;
var cron = require('cron');
var cronJob = cron.job("* * * * * *", function()
{
logger.info("test1");
logger.debug("test2");
});
cronJob.start();
view raw index.js hosted with ❤ by GitHub

This simple script creates a logger to log to the filesystem and to console every second. The logfile path is set to /var/www/html/sheduler/all-log.log


Install sheduler as a systemd service


To make our sheduler reboot save we now add the script as a systemd service.

add a file:
/etc/systemd/system/nodecron.service

paste following script in this service unit

[Unit]
Description=nodecron service
After=network.target
[Service]
User=root
ExecStart=/usr/bin/node /var/www/html/sheduler/index.js
[Install]
WantedBy=multi-user.target
This adds a servicedesfinition with the information, what script to execute.

Test your systemd-service

At last we reload our service units by typing:
$ systemctl --system daemon-reload

Enable your service
$ systemctl enable nodecron

 Start your service
$ systemctl start nodecron


Get status
$ systemctl status nodecron 

 If everything works you will get a status like that:

This tells us, that our logger logs every second a info and a debugmessage.
As you can see, our node.js script gets executed successfully. The service is tunning even, if you reboot the machine.

At the end you will find a logfile with your 2 loggings under:
/var/www/html/sheduler/all-logs.log

Disable your service like that
$ systemctl disable nodecron



Keine Kommentare:

Kommentar veröffentlichen