Ad

Server-Side And Time-Specific Script Execution

I have a list of dates in an array(for now we can say that the dates are sorted). I want to have a script execute when the date matches a date in the array. My issue is figuring how to make this work on its own. I would like to have a server somehow act like an alarm clock that can run a script for a scheduled date and time. If anyone could help with suggestions to make this work I would appreciate it.

set date >>> if (currentDate == set date) >>> run script for the respective data

Please ask if you need clarification.

Ad

Answer

The way to do this with parse is a class with a date attribute. Create one object per date in your array of dates (they needn't be sorted).

Create a scheduled job that upon running, query's the class for the first date equal to the current date. If one is found, do whatever you want to do when an alarm is triggered.

So, something like...

Parse.Cloud.job("checkStatus", function(request, status) {
    var today = new Date();
    today.setHours(0, 0, 0, 0);
    var tomorrow = new Date(today);
    tomorrow.setDate(tomorrow.getDate() + 1);

    var query = new Parse.Query("MyAlarmClass");
    query.greaterThanOrEqualTo('theDateAttribute', today);
    query.lessThan('theDateAttribute', tomorrow);
    return query.first().then(function(anAlarm) {
        if (anAlarm) {
            // do whatever should be done on the alarm
        } else {
           // do nothing
        }
    }).then(function() {
        status.success();
    }, function(error) {
        status.error(JSON.stringify(error));
    });
});

Schedule this to run at least twice per day (or faster than whatever resolution you need on the alarms).

Ad
source: stackoverflow.com
Ad