I have created crone job for every minute
import * as schedule from "node-schedule"
schedule.scheduleJob('* * * * *', async () => {
console.log("running every minute")
});
the above working perfectly for every minute. similarly I have task which has to execute for every hour. I tried like below
schedule.scheduleJob('0 0 */1 * *', async () => {
console.log("running every minute")
});
But unfortunately its not working every hour as expected. Is there any thing I'm missing
i think you want "0 */1 * * *". Also you can use this great website to make sure your crontab is correct: https://crontab.guru/
based on node-schedule the third * is hour
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
for every one hour just try
schedule.scheduleJob('0 0 */1 * * *', async () => {
console.log("running every minute")
});
For every Hour it should be :
schedule.scheduleJob('0 0 */1 * * *', async () => {
console.log("running every hour")
});
Related
How can I print Cypress base Url in the Git bash CLI console while running the Cypress test ? Could someone please advise ?
When running headless, the browser logs don't show in the terminal, but those from the Cypress node process (aka cy.task()) do show up.
I assume the baseUrl is changing during the test, here is an example that does that and logs the current value using a task.
The configured value is http://localhost:3000, the test changes it to http://localhost:3001.
cypress.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
console.log('Printing baseUrl - during setup', config.baseUrl)
on('task', {
logBaseUrl(baseUrl) {
console.log('Printing baseUrl - from task', baseUrl)
return null
}
})
},
baseUrl: 'http://localhost:3000'
},
})
test
it('logs baseUrl to the terminal in run mode', () => {
console.log('Printing baseUrl - directly from test', Cypress.config('baseUrl')) // no show
Cypress.config('baseUrl', 'http://localhost:3001')
cy.task('logBaseUrl', Cypress.config('baseUrl'))
})
terminal
Printing baseUrl - during setup http://localhost:3000
====================================================================================================
Running: log-baseurl.cy.js (1 of 1)
Printing baseUrl - from task http://localhost:3001
√ logs baseUrl to the terminal in run mode (73ms)
1 passing (115ms)
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 1 │
│ Passing: 1 │
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 0 │
│ Video: true │
│ Duration: 0 seconds │
│ Spec Ran: log-baseurl.cy.js │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
====================================================================================================
(Run Finished)
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ √ log-baseurl.cy.js 108ms 1 1 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
√ All specs passed! 108ms 1 1 - - -
Done in 18.49s.
You can use a nodejs command console.log(Cypress.config().baseUrl).
That does not require Git Bash though, only nodejs installed on Your Windows.
Or you can add in your tests cy.config().baseUrl.
I'm using #nestjs/schedule and cron jobs in nestjs.
I need execute cron jobs limited ways, For example three times.
How can I do that?
Tnx
┌────────────── second (optional)
│ ┌──────────── minute
│ │ ┌────────── hour
│ │ │ ┌──────── day of month
│ │ │ │ ┌────── month
│ │ │ │ │ ┌──── day of week
│ │ │ │ │ │
│ │ │ │ │ │
* * * * * *
Suppose you want your corn job run at 10:45 you simply replace the steric sign from hours and minutes position as shown above indications.
cron.schedule('45 10 * * *', () => {
console.log('Running a job');
},
{
scheduled: true,
timezone: "Asia/Karachi"
});
Or suppose you want to run every day at specific hours
cron.schedule('0 10,11,12 * * *', () => {
console.log('Running a job');
},
{
scheduled: true,
timezone: "Asia/Karachi"
});
You can create your job dynamically and use the annotation timeout to finish it.
The timeout annotation needs time to finish your task, so, you need to calculate the time set to execute your job and multiply for the quantity of execution you want.
Here is an example of executing every 2 seconds and only three times. So, the final time to finish the job will be 2 * 3 * 1000 (or 6 seconds).
Pay attention that I had to add 1 second in timeout annotation, or the interruption would be executed on the third process and will execute only twice.
After that, you can delete the job or stop it. In my example, I decide to delete it.
private static time = 2;
private static quantity = 3;
private static limit = TaskService.time * TaskService.quantity;
private nameTask = '### TESTE 3 Times ###';
addCronJon(name: string, seconds: string, limit?: number) {
const job = new CronJob(`*/${seconds} * * * * *`, () => {
this.logger.warn(`time (${seconds} for job ${name} to run!`);
});
this.schedulerRegistry.addCronJob(name, job);
job.start();
this.logger.warn(
`job ${name} added for each minute at ${seconds} seconds!`,
);
}
threeTimes() {
this.addCronJon(
this.nameTask,
TaskService.time.toString(),
TaskService.quantity,
);
}
#Timeout(TaskService.limit * 1000 + 1000)
threeTimesTimeout() {
this.schedulerRegistry.deleteCronJob(this.nameTask);
this.logger.warn(`job ${this.nameTask} deleted!`);
}
The complete code you can see here .
The nestjs page about cron job you can access here
I am trying to run the following Postman Script via Newman to write the response to file:
//verify http response code
pm.test("Report Generated", function () {
pm.response.to.have.status(200);
});
var fs = require('fs');
var outputFilename = 'C:/Users/archit.goyal/Downloads/spaceReport.csv';
fs.writeFileSync(outputFilename, pm.response.text());
The request gives a response but getting the following error when writing to file:
1? TypeError in test-script
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 20 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 20 │ 1 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 2 │ 0 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 1m 48.3s │
├───────────────────────────────────────────────┤
│ total data received: 1.24MB (approx) │
├───────────────────────────────────────────────┤
│ average response time: 5.3s │
└───────────────────────────────────────────────┘
# failure detail
1. TypeError fs.writeFileSync is not a function
at test-script
inside "3i_BMS_Amortization_Schedule / GetReport"
Please help
Postman itself cannot execute scripts like that. To save your responses of all the api requests, you can create a nodeJS server which will call the api through newman and then save the response to a local file. Here is an example -
var fs = require('fs'),
newman = require('newman'),
allResponse=[],
outputFilename = 'C:/Users/archit.goyal/Downloads/spaceReport.csv';
newman.run({
collection: '//your_collection_name.json',
iterationCount : 1
})
.on('request', function (err, args) {
if (!err) {
//console.log(args); // --> args contain ALL the data newman provides to this script.
var responseBody = args.response.stream,
response = responseBody.toString();
allResponse.push(JSON.parse(response));
}
})
.on('done', function (err, summary) {
fs.writeFile(outputFilename,"");
for(var i =0;i<allResponse.length;i++)
{
fs.appendFileSync(outputFilename,JSON.stringify(allResponse[i],null,5));
}
});
Note that, the above code will save only the responses. Other data like request , or URl can be extracted in a similar manner. To run this, install newman in the same directory as the script, then run the script using -
node file_name.js
I want to execute the message daily at 10'o clock 5 minutes 25 seconds but I tried the program I got an error:
ERROR OCCURED:
throw patterns[5] + ' is a invalid expression for week day';
^
is a invalid expression for week day
Code:
var cron = require('node-cron');
cron.schedule('25 05 10 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 January,February,March,April,May,June,July,August,September,October,November,December Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday ', function(){
console.log('Time for breakfast');
});
The cron syntax is as described in the node-cron library page:
┌────────────── second (optional)
│ ┌──────────── minute
│ │ ┌────────── hour
│ │ │ ┌──────── day of month
│ │ │ │ ┌────── month
│ │ │ │ │ ┌──── day of week
│ │ │ │ │ │
│ │ │ │ │ │
* * * * * *
So, for executing the task every day at 10:05:25, try
25 5 10 * * *
cron.schedule('25 5 10 * * *', function() {
console.log('Time for breakfast');
});
how to set a variable automatically zero for each midnight in node js is it possible to use node-schedule-npm ? if so please help me how to do it
initially var count= 0; when action is performed, it will get incremented throughout the day, for next day it should be automatically set to zero.
You can use node-cron from npm.
$ npm install --save node-cron
Here is an example code
var cron = require('node-cron');
cron.schedule('* * * * *', function(){
console.log('running a task every minute');
});
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
you can try this
var cron = require('node-schedule');
cron.scheduleJob('00 00 12 * * 1-7',function setVotesToZero(req, res, next) {
db.users.find().toArray(function(err, vote) {
if (err) return next(err);
db.users.update({}, {
$set: {
count : 0
},
}, function(err, result) {
if (err) return next(err);
res.send({
status: 'success',
});
});
})
})