Can i use cron expression to start and end job - javascript

i tried this package cron.
const CronJob = require('cron').CronJob;
console.log('Before job instantiation');
const job = new CronJob('0 0 10-12,18-23 * * 0-6', function() {
upload //
});
console.log('After job instantiation');
job.start();
i need to upload all days in a week, in between 10-12 am and 6-11pm. so i need to start upload at 10 am and pause it in 12am. and again resume my upload at 6pm and pause at 11pm.
but this cron fires every one hour between 10-12 am and 6-10pm, but i need to sense only at 10 , 12 , 6 , 10 not in between hours.
how to do this ?

Your pattern for hours specifies two ranges "10-12,18-23", so it is doing what you told it to.
If you want to accomplish your goal, you should use "10,12,18,22' instead. Unless your goal really intended for 11 as the last hour then you should use 23 instead of 22.

Related

Encode a unique string every 5 second using date

I am working on a encryption project, and I basically want to get a different string every 5 seconds regardless from the platform it's being called from.
I can get a unique string every second, and this will give the same thing on any platform
return btoa(new Date().getSeconds().toString() + 'secret');
Every minute
return btoa(new Date().getMinutes().toString() + 'secret');
But, I want to do something similar to this but every 5 seconds, so that the other platform will have enough time (5 seconds) as 1 second is too short, and 1 minute is too long.
Any ideas?
This is not something you can find using Date, but can easily be done by using some math.
const date = new Date();
const time = date.getSeconds() - (date.getSeconds() % 5);
This will give you the seconds in this minute of a multiple of 5.
So every 5 seconds it goes from 0 to 5 to 10 and so on.

How to schedule node-cron job every new month?

I am using node-cron package for scheduling node-cron jobs. I want to schedule an node-cron job which will run every new month.
for example:
My node-cron job should run at 1 September 2020 after that it should run at 1 October 2020 and and so on..!
Please help me out for the above issue.
Thanks in advance.
I've tested accepted answer's code and noticed that there is something off.
cron.schedule(* * 1 * *) will make code run every first day of the month, and every hour and every minute. This means if it is first day of month, code will run once every minute.
To correct this issue (and actually run once a month, not multiple times in one day) we change: cron.schedule(* * 1 * *) to: cron.schedule(0 0 1 * *) so code runs every first day, at 00:00.
Following this tutorial I believe you just have to do:
const cron = require("node-cron");
cron.schedule("* * 1 * *", function() {
// Do something
});
where:
cron.schedule("* * * 1 * *", function() { // Do something });
OR
cron.schedule("* * 1 * *", function() { // Do something });
NOTE : remember that the first * of the six * is optional

Node-scheduler how to set recursive condition

I want to run a background script every day at midnight after every 10 minutes.
It should run on 00:10, 00:20, and so on
What I have tried is.
schedule.scheduleJob("1 0-23 * * *", async () => {
})
What I want is to find a way to start this job at midnight and should be run after each 10 minutes
In your case schedule must be: */10 * * * * – run every 10th minute.
I'm not sure what you mean by starting at midnight. When it should finish then?
Update based on comment:
If you want to run it during the interval of 12 PM to 10 AM you'll need to specify the hour parameter. And it'll look like this: */10 0-10 * * * - every 10th minute past every hour from 0 through 10
start at 00:00:00
then at 00:10:00
then at 00:20:00
...
finish at 10:00:00
Hint: you can use crontab.guru to play with Cron formats.

Correctly treating DST shift with momentJS

I've came across something that I thought it was right, but now taking a closer look something is clearly wrong.
I'm on a project of a pill reminder app where someone can set notifications to remind him/her of taking pills in the correct time. There're medicines which a person can take for the rest of his life. And in that case I don't set alerts for years, I set for 3 months max and, when he takes one and mark it as done, I set another alert for 3 months later starting on that date/time.
This app will be released only in Brazil and we have Daylight Saving Time here. When it shifts to DST time the clocks must be adjusted to -1 hour after midnight, when going off DST it gains 1 hour.
For this project I'm using Firebase, Ionic 2, the LocalNotification Plugin and Moment JS.
I have to make a story of the user because other user can see if he's taking it correctly, so I use Moment JS to manipulate the datetime and save the notification and create a node for that user in firebase with UNIX time.
LET'S FINALLY GO TO THE PROBLEM.
When saving a date I check if this date is DST, if it is I add +3 hours to it, if it's not I add +2. I need to add this because when I wrap the isoString in the Moment() function it gives me -3 hours or -2 hours (Haven't searched for this, but I think Moment uses USA time).
This works fine if I'm saving dates inside DST times if I'm in DST time, if in some case I'm not on DST and save a notification for a DST time day it saves with +2 hours.
AN EXAMPLE
The DST time will shift to in DST on October 15. If I need to save 30 notifications, one per day everyday as 12AM, starting at October 1 up to October 30. From day 1 to day 15 the dates will be right, from day 16 to 30 they'll be with +2 hours.
Here's the basic code I use:
// THIS'LL SET MY DATEPICKER TO THE DATE/HOUR I'M IN.
minDate: any = Moment().isDST ? Moment().subtract(3, 'h').toDate().toISOString() : Moment().subtract(2, 'h').toDate().toISOString();
// THIS'LL CONVERT THE SELECTED DATE TO A UNIX TIME IN WICH I'LL USE TO SAVE THE NOTIFICATION AND THE MEDICATION DATA ON FIREBASE
unixConverted = Moment(this.minDate).isDST ? Moment(this.minDate).add(3, 'h').unix() : Moment(this.minDate).add(2, 'h').unix();
What is strange is that using Moment().unix() alone it give me the right time I'm in, if I use Moment(this.minDate).unix() it gives me -2 or -3 hours of the hour I selected.
So if it's in DST (in which I've set my clock to -1 hour) I add 3, if not I add 2.
So how is the proper way to manipulate this DST shift?
Is there a better way to do this than using DST?
Am I right using this logic or is this much more complex than what I think?
Ok so i've found a better way without using .isDST() method.
Simple use Moment().add(Moment().utcOffset(), 'm'), this'll get the current moment time and add the offset in minutes.
The .add() and .subract() methods makes Moment return the UTC time.
The utcOffset() returns a positive or negative number of minutes (representing hours) from UTC, like -60 or 180. So i'll get the correct respecting the time shift.
Worked like a charm for me.

How can I set up a 24 hours operation with iMacros?

How can I set up a 24 hours a day, 7 days a week (non-stop) operation with iMacros?
I Found that code but i can't adapt it my macro. i don't know use visual basic but i setup vbs 2008 today. How can i use that code on my imacros code? Anybody can tell it step by step on vbs. Thanks
'Sample code B
Set iim1= CreateObject ("imacros")
For m = 1 to 5000
iret = iim1.iimInit()
For n = 1 to 1000
iret = iim1.iimPlay ("macro1")
Next
iret = iim1.iimExit()
Next
Source: http://wiki.imacros.net/Web_Testing#Q:_How_can_I_set_up_a_24_hours_a_day.2C_7_days_a_week_.28non-stop.29_operation_with_iMacros.3F
I don't know about VB solution but JS solution is following.
Place SET !ERRORIGNORE YES command in macro and SET !TIMEOUT_MACRO 120 (you can change the time) inside the macro. Then when you call the macro in JS you do it like this.
iimPlay(macro, 110)
Make sure that the time is less then macro timeout by few seconds. That way macro will restart after each time and script can work a lot of time.
//this starts firefox
start /d"C:\Program Files\Mozilla Firefox" firefox.exe /AUTO
//this kills firefox process
pskill firefox.exe
//this command is one version of wait 10 seconds. You can change number 10 into some other for script to wait more then 10 seconds
ping -n 10 127.0.0.1
On this link you have examples on how to run iMacros file from bookmarks. So you have couple of examples and you can use them.
http://wiki.imacros.net/iMacros_for_Firefox#Command_Line_Support

Categories

Resources