How To Run Node-Schedule Task Every 15 Days In Nodejs? - node.js

I'm using Node-Schedule package and I'm having some trouble to define the criteria using the * system.
Does anyone know how can I run this task every day 15 and 30 of a month (15 days interval)
var schedule = require('node-schedule');
var tarefa = schedule.scheduleJob('15-30 * * ', function() {
console.log("TAREFA");
});
One more question, let's say I wanna change this later based on user selected option, how can I get this current task schedule and change this interval later?
Thanks in advance!

0 0 0 1,15 * ? should work (see Quartz Cron expression :Run every 15 days ie twice in a month).
To change the schedule, you can call the rescheduleJob method with the job's name and the new user-specified schedule.
var schedule = require('node-schedule')
schedule.scheduleJob('myJob', '0 0 0 1,15 ? *', function() { console.log('hi') } )
schedule.rescheduleJob('myJob', '0 0 0 1,20 ? *')

Related

Azure Function Cron expression for excluding a defined time

I want to do very basic cron job on Azure Function.
I'm writing functions on Visual Studio and the functions will be dockerized job.
There are two different azure function on my solution.
Runs for every month. (Refreshes whole table).
Runs for every five minutes. (Refhreshes just feature records. Not historical records.)
My expectation is that the functions shouldn't block eachother. So, they shouldn't work at the same time.
My cron expressions for the functions are;
Function1: 0 0 0 1 * * (Runs every month on 1st.)
Function2: 0 */5 * * * * (Runs every five minutes)
I don't want to run function2 on every month like 01/01/2021 00:00:00.
How can I exclude the time from function2?
There is no direct way to do that.
As a workaround, you can add if else code block in Function2. For example, in the if block, you can determine if it's the time like 01/01/2021 00:00:00. if yes, then do nothing. If not, then go to the else block to execute your logic.
Like below:
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
var the_time = DateTime.Now;
//if the date is the first day of a month, and time is 00:00:00
if(the_time.Day==1 && the_time.ToLongTimeString().Contains("00:00:00"))
{
//do nothing, don't write any code here.
}
else
{
//write your code logic here
}
}

Dynamic cron parameters _ Node Js

I have a script that send an email to specific customer but what I'm trying to do is to fire that email in a given time and date .. so the solution is to use cron module like below and changing the parameters with what I want
cron.schedule("* * * * * " , function(){
}
the problem that I want to modify those parameters with varibales which contains a result for a specific calculation! like this below
const X = 234;// this values will change everyday automatically
cron.schedule("X * * * * " , function(){
}
so is it possible to do something like that or is there a better solution that allows me to modify cron parameters
the solution that I tried but nothing is working is below :
const x = 40;// 40 seconds
cron.schedule(`${x} * * * *`, function(){
}
Best Regards,
Many Thanks to num8er ,
the only solution for my problem is
Simply save to table jobs the stuff need to do and put cron script to run every minute which will check jobs table and will run what is scheduled by time. Example table: jobs [id, runAt, method, arguments, done], cron will run and will take jobs which is not done and runAtis less than now, will run method and pass arguments to it and after finishing it will set done=true
that's enough simple to achieve: 1 insert to table, 1 method that will run by cron and get jobs from table and execute

Schedule Repeatable Jobs using bull.js By providing time in repeat rules

I'm currently using bull js to create an api that schedules a job according to inputted time. currently I'm able to do this using crone expression if the time input is in the format of 'YYYY-MM-DD HH:mm". The only problem is that if I want to schedule a job that will run daily I have to write some logic to get time from the inputed time. My question is whether I can specify the repeat rules using a date input as it is done in node-schedule. in short, am looking for the equivalent of the following node-schedule implementation in bull.
var date = new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(y){
console.log(y);
}.bind(null,x));```
Based on the documentation, repeating tasks/jobs are handled using cron syntax:
paymentsQueue.process(function(job) {
// Check payments
});
// Repeat payment job once every day at 3:15 (am)
paymentsQueue.add(paymentsData, { repeat: { cron: "15 3 * * *" } });
Use this cron expression validator to verify your logic is correct.

Node, Mongodb count array in API and reset / start again using cron job

I’m creating a rota where everyday the user changes. I have a database with each row is a different member of staff.
I need count how many rows are in the database and then count them to display a different user each day. When the array gets the end, it needs to go back to the start for example the array is 0 to 4 when it gets to 4 the next one will need to be 0 and not 5.
The example below I have changed the cron job to every 1min for testing.
var mongoose = require('mongoose'),
Staff = mongoose.model('staffmembers');
var cron = require('node-cron');
var next = cron.schedule('*/1 * * * MON-FRI', function() {
Staff.find({}, (err,results) => {
var i = results.length++;
console.log(results[i%results.length].name)
})
});
next.start();
When I run the code below
console.log(results[1].name)
I get the name back I want to do this with the count.

Hangfire Cron Expression failing

I have a task scheduled like this:
RecurringJob.AddOrUpdate("Process inbound external messages", () => ProcessInboundExternalMessages(), Cron.Minutely);
It works.
But I need one to fire ever midnight.
So I tried to create a cron expression:
private const string CronDailyAtMidnight = "0 0 * * *";
And then assign that to my task.
RecurringJob.AddOrUpdate("Deactivate user role allocation on expiry", () => DeactivateUserRoleAllocationOnExpiry(), CronDailyAtMidnight);
However, it never fires. Can anyone see an issue?
The issue was - it uses UTC date. Fixed.

Resources