[SOLVED] Moralis job like cron

Hey there, hope you are fine :slight_smile:

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:

  1. is this the better approach to do achieve the purpose?
  2. how can i test besides going to “jobs” panel and click “run now”
  3. how can i debbug this to understand if the code is wrong?
  4. how can i run it every 30 seconds (i read about an approach with setInterval)
  5. 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);
});
  1. how can i test besides going to “jobs” panel and click “run now”
  2. how can i debbug this to understand if the code is wrong?

You can add some logging and check your server dashboard for the updated classes/columns.

  1. how can i run it every 30 seconds (i read about an approach with setInterval)

You can schedule jobs to run from your server dashboard (Jobs > Scheduled Jobs) - the minimum interval is 1 minute. For a shorter interval, an alternative would be to use a cloud function that starts a job and then you can call this cloud function repeatedly e.g. with setInterval.

  1. there is a consideration about timezone?? the createdAt field using for calculation is created by new Date() in javascript

I’m not sure if timezone matters if you’re only comparing against the existing datetime like createdAt.

Thanks, i didn’t find how to schedule besides daily, but i saw that uncheck daily, the interval shows to 1 min :slight_smile: