is it possible to use setTimeout() in firebase cloud functions? - flutter

i am wondering if it is possible to use setTimeout() in cloud functions
let millisecond = (day *24*60*60*1000) + (hour* 60*60*1000) + (min *60*1000) + (second *1000);
setTimeout(() => {
admin.firestore().doc("timer/" + chatroom).set({
"millisecond":millisecond,
"username":username,
"tousername":tousername,
"day":day,
"min":min,
"second":second,
"hour":hour,
}, millisecond);
if not possible is there a way to accomplish this idea ?
also am i billed as much as the function is active?
thank you

Using setTimeout in a Cloud Function is possible, but keep in mind that:
a function can run at most 9 minutes (1st generation) or 60 minutes (2nd generation) (see docs)
you pay for the entire time a function is active this way
So if you need to wait for significant time (I usually use 30s as a threshold for this), consider using another mechanism to delay operations such as scheduling a callback with Cloud Tasks.

Related

How to make a jenkins call to retrieve the job details every 'X' minutes in meteor?

What I'm up to is to get the jenkins job details and store it in mongo DB every "X" minutes. I have to make an HTTP.call(JenkinsURL) which I know how to do. My problem is calling it for specific intervals.
buildDetails=HTTP.call('GET',buildURL);
buildURL has the Jenkins job URL. I found this link which gives an overview of the code for my problem, but I don't know how and where i should place these code to get it working. I tried all possibility.
Is there any method in meteor which can make this possible to run a specific code to be run for every X min??
Is there any method in meteor which can make this possible to run a specific code to be run for every X min??
Yes, there is.
Meteor.setInterval that can be used to do something repetitively every X interval of time.
You can put your HTTP call within it on the server. Eg:
Meteor.startup({function(){
var timerID = Meteor.setInterval(function(){
buildDetails=HTTP.call('GET',buildURL);
// and other things
}, 60000) //60000ms = 1 min
}
});
When you want to stop the timer function, simply call Meteor.clearInterval
Meteor.clearInterval(timerID);

Schedule function execution in swift

I'm developing a simple app in Swift and I need to schedule a function execution every 24 hours. I'm aware of the method:
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
self.functionToCall()
})
that could solve my problem but, is this the right solution for a 24 hours delay?
Thanks
Theoretically, this is possible.
The problem is that your app would have to run in the foreground for 24 hours, which is very unlikely to happen. Unfortunately, you can not run background tasks just like that.
The solution:
Just make it look like the function would execute in the background. Every time the update function is called, simply save the Int(Date().timeIntervalSince1970) to UserDefaults. This works like a timestamp and saves the last time you called your update function. Every time in the viewDidLoad()-function (not sure if it's called the same on Mac apps, but you can imagine what I mean) call:
If let timestamp = UserDefaults.standard.integer(forKey: "yourTimestampKey") {
let currentTimestamp = Date().timeIntervalSince1970
if (currentTimestamp - timestamp) > 86400 { // number of seconds in 24 hours
// the last time your function was updated was at least 24h ago
update()
}
}
That's how you can make it appear like it was updated in the background. I use this all the time in my apps and it works perfectly.
EDIT:
Maybe, just in case the app does indeed run 24 hours in a row, I would set up the upper function that you posted first as well.

Sahi scripting wait or time delay

I was wondering if someone could help me. I want to introduce a 10 minute wait or time delay within my sahi script.
Can someone confirm the correct command to use?
Thanks in advance
Just have a look at the docs, the command you are looking for is _wait
https://sahipro.com/docs/sahi-apis/action-apis.html#Waits
Write _wait(parameter); where parameter is your the time in milli-seconds you want to introduce the delay for.
In your case try _wait(600000);
You can also use the setSpeed(parameter); API , in this case Sahi will wait for the time specified in milli-seconds as a parameter , after each task in the script. But I guess waiting 10 mins after each task is not your requirement.
You can use this method:
_wait($timeout, [$condition])
here $timeout is the duration you want to wait for. And $Condition is something that you can customize based on what condition you want to wait for.
Or you can just use the overloaded method _wait($timeout) if you want to wait for a specific duration.
You can also apply a conditional wait:
_wait(timeOut, _isVisible($element));
This will either wait till "timeOut" milliseconds, or till $element is visible. (whichever condition is satisfied earlier)

setInterval with a variable interval

I would like an api call to be done immediately, then after 1sec, then 2sec, etc in incrementations of 1 sec up to 10.
I tried something like this:
Meteor.startup ->
counter = 1000
Meteor.setInterval (->
Meteor.call "call_url", url, (err, result) ->
...
if counter < 10000
counter += 1000
console.log counter
), counter
While my counter is incremented, the log is done exactly every seconds, which means that the setInterval doesn't keep track of the value.
The only way I see to handle this would be having 9 setTimeout to call the api at different times, then a Meteor.setInterval starting after all the timeouts... Sounds very ugly.
Any suggestions on how to do this in a clean way? It's important for the user to see frequent updates when he just connects to the page, but if he decides to let it open for a while, there is no need to perform that api query so often.
A more general solution (and scalable in the future for CoinsManager) is to use a queuing package that supports scheduling events in the future. I've looked at a bunch of background task management for Meteor, and queue supports scheduling.
Not quite clear what you are asking and there are probably a lot of ways to generate the intervals you want.
In javascript it might look like this:
var doStuff = function(){...};
var intervals = [ 1000, 3000, 6000, 10000, 15000, 21000, 28000, 36000, 45000, 55000 ];
doStuff(); //run immediately
intervals.forEach( function( interval ){
Meteor.setTimeout( doStuff, interval );
});

Schedule Node.js job every five minutes

I'm new to node.js. I need node.js to query a mongodb every five mins, get specific data, then using socket.io, allow subscribed web clients to access this data. I already have the socket.io part set up and of course mongo, I just need to know how to have node.js run every five minutes then post to socket.io.
What's the best solution for this?
Thanks
var minutes = 5, the_interval = minutes * 60 * 1000;
setInterval(function() {
console.log("I am doing my 5 minutes check");
// do your stuff here
}, the_interval);
Save that code as node_regular_job.js and run it :)
You can use this package
var cron = require('node-cron');
cron.schedule('*/5 * * * *', () => {
console.log('running a task 5 minutes');
});
This is how you should do if you had some async tasks to manage:
(function schedule() {
background.asyncStuff().then(function() {
console.log('Process finished, waiting 5 minutes');
setTimeout(function() {
console.log('Going to restart');
schedule();
}, 1000 * 60 * 5);
}).catch(err => console.error('error in scheduler', err));
})();
You cannot guarantee however when it will start, but at least you will not run multiple time the job at the same time, if your job takes more than 5 minutes to execute.
You may still use setInterval for scheduling an async job, but if you do so, you should at least flag the processed tasks as "being processed", so that if the job is going to be scheduled a second time before the previous finishes, your logic may decide to not process the tasks which are still processed.
#alessioalex has the right answer when controlling a job from the code, but others might stumble over here looking for a CLI solution. You can't beat sloth-cli.
Just run, for example, sloth 5 "npm start" to run npm start every 5 minutes.
This project has an example package.json usage.
there are lots of Schedule package that would help you to do this in node.js . Just choose one of them based on your needs
following are list of packages:
Agenda,
Node-schedule,
Node-cron,
Bree,
Cron,
Bull