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 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
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.
How i can call the function 24 hours after the first call in node.js?
Or 24 hours after the execution of a specific request to this node.js server?
And execute it with the same parameters
(function must sent email to user)
I think this is a much bigger question that might be dependent on the type of architecture you have. The simple answer is to investigate some sort of scheduler, or cron task, however the better solution might be to distribute these systems and setup some sort of messaging system live Rabbit MQ.
When the first function runs, you can send a message via rabbit to the system that handles emails and run this much later, thereby reducing the load on the original system, or protecting it if it goes down.
It's a simple case of a javascript that continuously asks "are there yet?" Like a four year old on a car drive.. But, much like parents, if you do this too often or, with too many kids at once, the server will buckle under pressure..
How do you solve the issue of having a webpage that looks for new content in the order of every 5 seconds and that allows for a larger number of visitors?
stackoverflow does it some way, don't know how though.
The more standard way would indeed be the javascript that looks for new content every few seconds.
A more advanced way would use a push-like technique, by using Comet techniques (long-polling and such). There's a lot of interesting stuff under that link.
I'm still waiting for a good opportunity to use it myself...
Oh, and here's a link from stackoverflow about it:
Is there some way to PUSH data from web server to browser?
In Java I used Ajax library (DWR) using Comet technology - I think you should search for library in PHP using it.
The idea is that server is sending one very long Http response and when it has something to send to the client it ends it and send new response with updated data.
Using it client doens't have to ping server every x seconds to get new data - I think it could help you.
You could make the poll time variable depending on the number of clients. Using your metaphor, the kid asks "Are we there yet?" and the driver responds "No, but maybe in an hour". Thankfully, Javascript isn't a stubborn kid so you can be sure he won't bug you until then.
You could consider polling every 5 seconds to start with, but after a while start to increase the poll interval time - perhaps up to some upper limit (1 minute, 5 minute - whatever seems optimal for your usage). The increase doesn't have to be linear.
A more sophisticated spin (which could incorporate monzee's suggestion to vary by number of clients), would be to allow the server to dictate the interval before next poll. The server could then increase the intervale over time, and you can even change the algorithm on the fly, or in response to network load.
You could take a look at the 'Twisted' framework in python. It's event-driven network programming framework that might satisfy what you are looking for. It can be used to push messages from the server.
Perhaps you can send a query to a real simple script, that doesn't need to make a real db-query, but only uses a simple timestamp to tell if there is anything new.
And then, if the answer is true, you can do a real query, where the server has to do real work !-)
I would have a single instance calling the DB and if a newer timestamp exists, put that new timestamp in a application variable. Then let all sessions check against that application variable. Or something like that. That way only one innstance are calling the sql-server and the number of clients does'nt matter.
I havent tried this and its just the first idéa on the top of the head but I think that cashe the timestamp and let the clients check the cashe is a way to do it, and how to implement the cashe (sql-server-cashe, application variable and so on) I dont know whats best.
Regarding how SO does it, note that it doesn't check for new answers continuously, only when you're typing into the "Your Answer" box.
The key then, is to first do a computationally cheap operation to weed out common "no update needed" cases (e.g., entering a new answer or checking a timestamp) before initiating a more expensive process to actually retrieve any changes.
Alternately, depending on your application, you may be able to resolve this by optimizing your change-publishing mechanism. For example, perhaps it might be feasible for changes (or summaries of them) to be put onto an RSS feed and have clients watch the feed instead of the real application. We can assume that this would be fairly efficient, as it's exactly the sort of thing RSS is designed and optimized for, plus it would have the additional benefit of making your application much more interoperable with the rest of the world at little or no cost to you.
I believe the approach shd be based on a combination of server-side sockets and client-side ajax/comet. Like:
Assume a chat application with several logged on users, and that each of them is listening via a slow-load AJAX call to the server-side listener script.
Whatever browser gets the just-entered data submits it to the server with an ajax call to a writer script. That server updates the database (or storage system) and posts a sockets write to noted listener script. The latter then gets the fresh data and posts it back to the client browser.
Now I haven't yet written this, and right now I dunno whether/how the browser limit of two concurrent connections screws up the above logic.
Will appreciate hearing fm anyone with thoughts here.
AS