Runs script repeatedly to api test - node.js

I currently have an api test script using supertest + mocha + grunt.
I would like to run this script every 1 minute. How is it possible

You can use linux cron job or node module schedular
https://github.com/node-schedule/node-schedule
TO run your script once in a minute.

Related

Running a pytest fixture only once when running tests in parallel

I'm having some real difficulty in running a pytest fixture within a conftest file only once when the tests are run in parallel via a shell script. Contents of the shell script are below:
#!/usr/bin/env bash
pytest -k 'mobile' --os iphone my_tests/ &
pytest -k 'mobile' --os ipad my_tests/
wait
The pytest fixture creates resources for the mobiles tests to use before running the tests:
#pytest.fixture(scope='session', autouse=True)
def create_resources():
// Do stuff to create the resources
yield
// Do stuff to remove the resources
When running each on its own it works perfectly. Creates the resources, runs the tests and finally removes the resources it created. When running in parallel (with the shell script) both try to run the create_resources fixture at the same time.
Does anyone know of a way I can run the create_resource fixture just once? If so then is it possible for the second device to wait until the resources are created before all devices run the tests?

Add a job to linux cron list programmatically with Nodejs

I have a job with Nodejs that I want to do it each 30 minutes to scan the Database and Update Products Data in An Ecommerce API with my Nodejs Program, note that the Nodejs Program is serving an REST API (Backend) for a react js web application
So I searched for that and I found that I can do that with Nodejs Cron Library like "node-schedule" but I know that will be more interesting to do it with Linux Cron
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
Is there any library that can let me add Cron jobs to Linux using Nodejs Or would I do it with "fs" only? so I will open the cron job file and add my command?
The command crontab which is part of Vixie Cron allows you to create, edit and delete per-user cron entries.
Or if you are running as the root user, which you should not be doing, you can drop cron files into /etc/cron.d
This is not always supported, and if you're running in a Docker type container environment it is doubtful that you have any cron at all. In that environment you'd want your running Nodejs to handle scheduled jobs for you. Or use some other kind of distributed scheduled work system.
You can put your cron job to a nodejs script. Then adding to the crontab can be done with cronbee module, via API:
import { cronbee } from 'cronbee'
await cronbee.ensure({
taskName: 'do smth',
taskRun: `node my-script`,
cron: '42 * * * *'
})
or you can ensure the cron job via CLI, if the module is installed globally or from npm scripts:
$ cronbee ensure mytasks.json

How to containerize a NodeJS app with cron jobs using Docker?

I have a NodeJS app consisting of a REST API and an overnight maintenance (cron) job. Currently running on Debian Linux.
What is the best practice do Dockerize it?
I can use the official "node" Docker image, however that doesn't contain a crontab.
I can use the official "alpine" Docker image (and install NodeJS in it) however I lose the possibility of upgrading NodeJS with the easy of pulling a new version of the official image.
What is the best way to achieve this?
Solution #1
Use a node cron package. I added some code example from a real project. Every hour I scrape a website. You can set time and place your recurring task inside cron.schedule. You can use it a separate project or combine with your api code.
import { Scraper } from './controller/scraper.js';
import cron from 'node-cron'
cron.schedule('0 0 */1 * * *', () => {
console.log('running a task every hour');
const scraper = new Scraper()
const data = scraper.getData()
});
Solution #2
Docker official website doesn't advice running process managers with containers. But you can use if you need. I use pm2 as a process manager. It can be used for running an app for some intervals and it can restart your app if it crashes.
Links that will help you:
https://pm2.keymetrics.io/
https://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
My use case:
Restart app if it crashes.
Run app.js file for each hour.
Use official nodejs docker image and build docker image with your code.
You need to run two containers, one for Rest API and other for cron job.
For cron job you dont need conrtab inside the docker image but schedule the image run on the host machine like below:
This will ensure to run docker image at required interval.
crontab -e
* */2 * * * docker run -it app/cron-image:v1

Run PM2 cron only on a scheduled time

I run one of my node.js command using the following command:
pm2 start sample.js --cron "0 1 * * *" -- SAMP
But the problem is this program run twice. First one is when I execute the command and send one is at 1:00 AM(Which we want).
So my query is how can set the cron so that this program runs at only once (At 1:00 AM only).
My suggestion to you is to use crontab for this task. This is exactly what cron was designed for, and not what pm2 was designed for.
As the comment above states, the --cron option only specifies when the app should be restarted, --cron cannot be used to schedule the running of an app. In the cases where we need to run a node app on a specific time table and don't need all of the fancy pm2 capabilities of auto-restarting and clustering, we simply use crontab.

How to run a nodejs script every second

I need to run my nodejs script for every second ,Similar to PHP cron jobs. I have tried some nodejs cron libraries like https://github.com/ncb000gt/node-cron but the issue was first run should be manual i:e I have to run the file with cron script for first time manually.
But in php cron jobs, they run by the server so if the apache server running script will automatically start and even if the script return an error for a cycle then script will run again from the beginning from the next cycle
So is there any way to achieve this in nodejs ?
You have two options:
using Node as a daemon, with something like Supervisord to run your node-cron script. This alternative is wasteful on resources such as RAM because Node and Supervisord are running all the time.
using the system's crontab, you can run your script like calling Node on the command line, such as * * * * node /path/to/your/script.js. This alternative is highly efficient but lacks some control, like being able to log the output in case of an error, although you could just pipe the output to a file: node script.js > logfile

Resources