I'm working on a Iot project. In my case, user can schedule a actuator command to run on specified time.( say on 08/06/2021 at 08.05 AM). This will be stored in mongodb database as a date variable. My requirement is to send an MQTT publish message to actuator at that specified time. Note that, this scheduling is not like interval scheduling. Each schedule will run exactly one time. I also need to edit the scheduled date time.
There is one way to achieve this. Each time user schedule a command, I can run a setTimeout function. But this is not a good practice and editing the schedule is difficult. So please help me to find a optimal solution for this. Thanks in advance.
you may check existing cron like packages
https://www.npmjs.com/package/node-cron
https://www.npmjs.com/package/cron
Related
This question already has answers here:
How would I get a function to run every 24 hours on a server?
(2 answers)
Closed 2 months ago.
What is the best way to handle a function that needs to run every 24 hours at a specific time?
let's say we have an application that pays people for a service that they provide during the day. we already stored the amount of work they did for the day and it's time to pay up.
we want the system to automatically pay (let's assume we toke care of this) them every day at 00:00.
what is the best way to do this?
I'm planning to ask the backend to calculate the remaining time to 00:00. then run our payment function after that time and that function runs itself every 24 hours
A: Is this a good way to handle things? & Is it scaleable?
B: What is the best way to do it in node.js? (I know how to work with the node built-in functions, but I'm not that great with algorithms I'm not sure if using setInterval is ok or not)
ps: other questions suggested using corn jobs that I'm already familiar with, I'm not looking to use any package I'm trying to do it by myself. in fact, I know how to do it myself, I'm just trying to know what is the best way to do it. this is not a project that I want to do for a client. this is a practice.
If you want to execute a function every X time or at a specific time, you should check cron jobs.
You have multiple ways to handle cron jobs. If you want to deploy a "quick" PoC, you can check websites (like easy cron) that permits you to call a URL (and by extension a function) with a cron expression (everyday, every month, etc...).
Or you can use some node packages, you will find on the npm index.
Check utilizing cron, cron is a job scheduler. You have many differents options about date,time etc.
I'm new to cron and actually not sure if node-cron is the right npm package for me.
Basically, I just want to create a scheduled post feature in my app. Where I can set a time to post my content in the future.
So I discovered node-cron and was able to play around with it. I can get it to work, but I noticed this kind of scheduling in node-cron is about setting intervals, and your function is set to repeat again and again. Correct me if I'm wrong.
But let's say I'd like to console.log("Hello World") on Feb, 24, 2023 and run it only once. If I set that on the cron string(not sure what you call it), then I guess I would set it like this:
cron.schedule("* * * 24 Feb *", ()=>{
console.log("Hello World")
})
There is no option to specify a year as I read on the documentation, so where I can I specify the year? My code above will run every second on every Feb, 24 in any year right? Don't mind "the every second run" on my code above, but what I wanted to do is to specify the exact date and year for it to run, within that specific date only.
I've read about node-schedule package where you can pass dates as the schedule, but it seems node-schedule only runs when the script is running?
I have a use case where I have to create a record in the database for users repeatedly on a scheduled basis. Let's say every Monday Weekly/BiWeekly. There are two ways with which i can achieve it.
Using Database Triggers to Create a record on the time. But I don't know how to repeat it. I have to create a trigger for the next schedule when this trigger runs, which i don't think is right approach.
Using Queues to handle the scheduling and executing the repeated jobs. But adding a job for each user is not a good idea I guess. I might be wrong but there is no other way to achieve my goal.
I am confused on what to choose between the two. Let's say i have to do this for 1 million users every week Monday at 9.00 a.m.
Which approach will scale?
I am using nodejs as my backend and using Bull-Queue for the queue and postgres as my Database.
Using Database Triggers to Create a record on the time. But I don't
know how to repeat it. I have to create a trigger for the next
schedule when this trigger runs, which i don't think is right
approach.
- Not a right approach based on the so many factors like - memory, number of requests and code quality.
So I went with the second approach:
Using Queues to handle the scheduling and executing the repeated jobs.
But adding a job for each user is not a good idea I guess. I might be
wrong but there is no other way to achieve my goal.
The situation:
I have a program that records student's payments, (made entirely with HTML, CSS and javascript), I would like it to calculate the amount of students that stopped the lessons, automatically, around the end of the month (by automatically I mean without the user having to press any buttons to get that info).
My question is:
If for example I schedule the function to run on the 29 of every month, and that day the user does not open the program, does the function get executed anyway or not?
The program is an off line program, made to be downloaded once and run always, no need to connect again to the web if the user decides it.
I would do something like:
var thisMoment = new Date(),//creates a date object
currentDay = thisMoment.getDate(); //this would give me the day at that moment.
if(currentDay == 29) { }//code to be executed here
I know I could just try it, but I would have to wait for a day, or rewrite the code to schedule something some minutes ahead, but I would miss all the juicy information you are probably going to share here hahaha.
Any help appreciated! :)
Unless there's some sort of server-side component, your HTML app effectively ceases to exist as soon as the user closes it. Nothing will happen if it's not open.
To do this kind of thing, you will either need a server application that's continuously running, or some sort of cron job that runs an application/script on the server at the desired time. Then, you will need to have your frontend application communicate with this server, most likely through some kind of REST API.
Is it possible on Firebase or Parse to set up something kinda like a cron job?
Is there a way to set up some sort of timed operation that runs over the stored user data?
For example, I'm writing a program that allows people to RSVP for lunch everyday. If you have RSVPed by noon, then you get paired up with somebody else who has also RSVPed. Using JavaScript, the user can submit their RSVP in the browser.
The question is, can Firebase/Parse execute the code to match everyone at 12:00pm every day?
Yes, this can be done with Parse. You'll need to write your matching function as a background job in cloud code, and then you'll need to schedule the task in the dashboard. In terms of the flexibility in scheduling, it's not as flexible as cron but you can definitely run a task at the same time every day, or every x minutes/hours.
Tasks can take 15 mins max to execute before they're killed, so depending on the size of your database or the complexity of your task, you may need to break it up into different tasks or make it resumable.
Just to confirm about Firebase:
As #rickerbh said, it can be done with Parse, but currently there is no way for you to run your code on Firebase's server. There are 2 options for you 2 solve this:
You could use Firebase Queue and run your code in Node.js
You could use a different library such as Microsoft Azure (I still haven't tried this yet, I'm not sure if it provides Job Scheduling for Android)
However, Firebase is working on something called Firebase Trigger, which will solve our problem, however it is still not released with no confirmed release date.