program a task with node.js... cron or setInterval? - javascript

Maybe it seems an odd question... but I don´t know exactly how to program a task... o rather, I don´t know exactly wich one is the best solution. I just finish a web app and it needs everyweeks, every monday, execute a "special" piece of code, that it can´t be invoked from anywhere else.
The cron solution looks much better, but the piece of code would be isolated, like if this code and the rest of the application were two diferents programs. And the worst handicap of the cron solution (even tough I suposse it can be solved), when I call the javascript file from the cli node cron.js it makes good its job, but it never ends... this is normal?? or it depends about the code?? Becouse I don´t want to execute one instance, and the next week, with the first instance still running, run the second, and everyweeks the same problem.
The "setInteval" solution could be setting the time for a day, and testing if it is monday (on monday makes the action)... or maybe setting the time for a week (604800000 miliseconds)... This solution hasn´t these problems, especially the last, but it consumes a lot of resources (I think...no?).
What is the best solution?? Any different?? And if the cron is the best... how to stop the task? (uuuuuhhhh maybe other programed task with cron -a shell- one minute after to kill the node task... uuummm a little botched job).
Thank you very much.

Related

Another possibilities than wait?

I am trying to automate a website where the testing environment is reduced and the page loads nearly two minutes(120000).
I don't want to use cy.wait(120000) or cy.pause() command.
Could anyone help by giving more suggestions to solve the issue even the testing environment gets slower than this.
I have tried should,intercept,etc..,Other than the normal ones can someone suggest me some ideas.It wil be more helpful if you post your answers with commands.
Advance Thanks for making a try to solve this.
cy.wait is not a good practice in most situations because it just hangs your program to wait till the end of the timeout.
I think you're looking for a solution to wait for an element appears in HTML, so I'd suggest you to check this part of the document
In your case, it should be like below
cy.get('.selector', { timeout: 120000 }).should('be.visible')
Even though the timeout is longer but whenever your .selector appears on the HTML, it will pass.
Note that, the default timeout is only 4 seconds, so if you want to have more timeout, you can modify it to longer (like 2 mins for your case).
Why do we need to have timeout? Well, basically, we don't want the program hangs forever, so that's why we need to have timeout.

How to make a node.js app run permanently programatically

Im building a node.js script/app/process (new to node and its definitions) that should run at a certain hour (from a conf file) every day.
Im using the Node Schedule package to schedule the main function to run at that appointed time everyday. But the thing is this package does an "in-process scheduling", which means my script must keep running in the background for the scheduling to work.
So basically my code should look like this at the end:
global consts initializing and dependencies (requires)
job scheduling
defining all functions necessary
logic to make script run permanently.
This node.js app runs inside a docker on an ec2 machine in AWS.
How can I accomplish the task of running it permanently? I need a programatic solution, something (a package, a design pattern) I can embed inside my code.
Might be important to note that inside my code I have only a few lines of "requires" and const initializations, and then 1 main function that invokes everything else, if it helps somehow. Also, because im using that Node Scheduler I mentioned, restarting the script when it ends (for example through docker if its possible) is a bad idea, because the scheduling job would be lost and the script itself does nothing except initializations and scheduling the main function to the desired time, so it would restart possibly every few milliseconds, which I guess is a bad practice.
Well, someone in the comment section pointed me to my mistake... I saw the following paragraph on node-scheduler and didn't even test if it works without tryin to find a way to "run forever":
"Note that Node Schedule is designed for in-process scheduling, i.e. scheduled jobs will only fire as long as your script is running, and the schedule will disappear when execution completes. If you need to schedule jobs that will persist even when your script isn't running, consider using actual cron."
But apparently they meant I should keep the process running, effectively means the scheduler will terminate if I close the terminal. Here is a topic with a question and answer about how to prevent that from happening, if someone needs a reference:
How to make a node.js application run permanently?
Thanks to all who tried to help :)

Easy way to refresh php function with js

I have a PHP function that checks for the occurence of a string. What I want is to have it checked continously at a certain interval(eg: 5 seconds). From what I've gathered, PHP would be lesst than optimal for this 'refresh' function so looking for a way to do it with JS.
Thanks in advance and sorry for the silly question!
I don't think PHP would be less than optimal, but either way, I don't know of an "Easy" way. Cron jobs would typically be perfect for what you need, but they do not run sub-minute (although I have seen interesting workarounds).
Node has a lot of packages for "cron" like functionality, but these take the same parameters, so no sub-minute.
You could utilize a setInterval() callback to accomplish this easily, but it would require you to a) keep a web client open 24/7 and b) monitor the web client to ensure it never freezes, fails, shuts down, etc.

Why is EventMachine so much slower than Node?

In my specific case, at least. Not trying to make general statements here.
I've got this web crawler that I wrote in Node.js. I'd love to use Ruby instead, so I re-wrote it in EventMachine. Since the original was in CoffeeScript, it was actually surprisingly easy, and the code is very much the same, except that in EventMachine I can actually trap and recover from exceptions (since I'm using fibers).
The problem is that tests that run in under 20 seconds on the Node.js code take up to and over 5 minutes on EventMachine. When I watch the connection count it almost looks like they are not even running in parallel (they queue up into the hundreds, then very slowly work their way down), though logging shows that the code points are hit in parallel.
I realize that without code you can't really know what exactly is going on, but I was just wondering if there is some kind of underlying difference and I should give up, or if they really should be able to run about as fast (a small slowdown is fine) and I should keep trying to figure out what the issue is.
I did the following, but it didn't really seem to have any effect:
puts "Running with ulimit: " + EM.set_descriptor_table_size(60000).to_s
EM.set_effective_user('nobody')
EM.kqueue
Oh, and I'm very sure that I don't have any blocking calls in EventMachine. I've combed through every line about 10 times looking for anything that could be blocking. All my network calls are EM::HttpRequest.
The problem is that tests that run in under 20 seconds on the Node.js code take up to and over 5 minutes on EventMachine. When I watch the connection count it almost looks like they are not even running in parallel (they queue up into the hundreds, then very slowly work their way down), though logging shows that the code points are hit in parallel.
If they're not running in parallel then it's not asynchronous. So you're blocking.
Basically you need to figure out what blocking IO call you've made in the standard Ruby library and remove that and replace it with an EventMachine non blocking IO call.
Your code may not have any blocking calls but are you using 3rd party code that is not your own or not from EM ? They may block. Even something as simple as a debug print / log can block.
All my network calls are EM::HttpRequest.
What about file IO, what about TCP ? What about anything else that can block. What about 3rd party libraries.
We really need to see some code here. Either to identify a bottle neck in your code or a blocking call.
node.js should not be more than an order of magnitude faster then EM.

Pausing JavaScript execution for animation of code

I'm having an interesting but difficult problem with my JavaScript code. Basically, I'm trying to create an animation of a simple algorithm using JavaScript (a sorting algorithm, if you're wondering) for educational reasons. I've already got all of the animation code written (using RaphaelJS), and it all works fine. The trouble is getting the animations of the algorithm to happen at the right time. JavaScript doesn't really have any way to "pause" execution, so I can't really step through the algorithm slowly, playing animations at each step. It's obviously much more valuable from an educational perspective if the algorithm proceeds at a pace the student can comprehend.
There are two ways to solve this problem that I can think of, and both suck. The first is to use some crazy setTimeout() code. This would probably be very difficult -- lots of strange code transformations would be needed to associate the different parts of the algorithm with correct timeouts. I've already tried to do this a little bit, and it gets very complicated for a non-trivial algorithm.
The second is to busy-wait. This would probably also work. The problem is that busy waiting is a pretty bad idea in JavaScript code -- I actually crashed Firefox when I was testing out this alternative. Basically, I'm wondering if there's another solution here that I'm overlooking. Bonus points if the solution is client-side only, since the amount of freedom I have to serve stuff other than static html and javascript on the school server is questionable.
It's obviously much more valuable from an educational perspective if the algorithm proceeds at a pace the student can comprehend.
What about using links/buttons e.g. < > to allow the user to step through the code by clicking prev/next?
Maybe the only viable workaround and may make more sense for the end user.
Maybe this is too simple to do what you want it to do, but what if you put the segment of code that you want to run between stops into a function, and call that function with the push/click of a button on the page: onclick="runAnimationPiece()"
Your code would need to hold some state to know where it left off from the last time the function ran.
And if you need to run different functions for different pieces of the animation, say, for instance, the first piece should be generated by foo1() and second piece by foo2(), you can create a counter and a function which calls the appropriate animation function (foo1() or foo2(), etc.) based on the counter.

Categories

Resources