Hey there, hope you are fine
I need to create like a âcronâ run every 30 seconds or minute⌠so i check the Cloud function as Job.
The purpose of the job is check if the time elapsed and the duration set in specific streamings has to stop⌠so if stream duration is 30 minutes and elapsed time is 31 minutes, a column should be updated with âfinishedDateâ and then that streams goes down⌠additional to check if job is running well i added a column name âattemptToFinishBiddingâ to understand if at least that record was consider.
I have a couple of questions:
- is this the better approach to do achieve the purpose?
- how can i test besides going to âjobsâ panel and click ârun nowâ
- how can i debbug this to understand if the code is wrong?
- how can i run it every 30 seconds (i read about an approach with setInterval)
- there is a consideration about timezone?? the createdAt field using for calculation is created by new Date() in javascript
Moralis.Cloud.job("finishStreams", async (request) => {
function diff_minutes(dt2, dt1) {
var diff = (dt2.getTime() - dt1.getTime()) / 1000;
diff /= 60;
return Math.abs(Math.round(diff));
}
// params: passed in the job call
// headers: from the request that triggered the job
// log: the Moralis Server logger passed in the request
// message: a function to update the status message of the job object
const { params, headers, log, message } = request;
const query = new Moralis.Query("ArtWork");
query.equalTo('dateFinishBidding', null);
const results = await query.find();
results.map(art => {
let diff = diff_minutes(new Date(), artObj.get('createdAt'));
let duration = parseInt(artObj.get('duration'));
if (duration < diff) {
art.set('dateFinishBidding', new Date());
art.save();
} else {
let attemps = art.get('attemptToFinishBidding') ? art.get('attemptToFinishBidding') : 0;
art.set('attemptToFinishBidding', attemps++);
art.save();
}
});
message("I just started");
return (request);
});