OnScheduledEvent improvement

OnScheduledEvent is run whenever a schedule is created, deleted, updated or run. This makes it so that you have to create conditions that check that the scheduled event was triggered because of the scheduled time or a CRUD method. I believe the payload should include this information to make it easier for devs to distinguish between each event type.

3 Likes

Hi @osanchez,

The event, OnScheduledEvent will only run the scheduled time comes up again. This will not run whenever the schedule is created, updated, or deleted.

So, the payload does not need any differences as it is always run when the set scheduled time comes again.

Do you mean any other behavior or a feedback on its current behavior?

We have the following registered event with callback function that runs a sync when this event is triggered. There is another server function that is called when our custom iparams page is saved. this server function creates, updates, or deletes the schedule. When we were saving the configuration page settings the onScheduledEvent was being triggered. As you mention, it should only be triggered when the scheduled time is reached, but for us this does not seem to be the case. please let me know if you would like to see more source code.

events: [
      { event: "onScheduledEvent", callback: "onScheduledEventHandler"}
  ]

@osanchez I understand the issue that you report.

When the iparams are updated, the onAppInstall event will get triggered. Could you share the source code of that function to understand what has been done on the callback function?
Please remove any business logic written there and just share the code snippet on how the schedule event is created/updated/deleted.

Here are the events

  events: [
      { event: "onScheduledEvent", callback: "onScheduledEventHandler"},
      { event: "onAppInstall", callback: "onAppInstalledCallback"}
  ]

OnAppInstall

    onAppInstalledCallback: function(payload) {
        console.log("Logging arguments from onAppInstallevent: " + JSON.stringify(payload));

        $db.set('D42-Settings', {.....}).then(
            function() {
                console.log('Production environment flag set')
            },
            function(error) {
                console.log('Failed to set production environment flag')
                console.log(error)
            }
        )
        renderData();
  },

Here is the scheduled event

onScheduledEventHandler: function(payload) {
    console.log("Logging arguments from onScheduledEvent: " +  JSON.stringify(payload));

    if("name" in payload.data) {
        if(payload.data.name === "sync") {
            console.log('Device42 scheduled sync event')
            //check if the current event was triggered due to a scheduled event
            let scheduled_time = payload.iparams.scheduleTime
            let event_trigger_time = new Date(payload.timestamp)

            //time conversion
            let hours = militaryTimeHour(event_trigger_time.getUTCHours());
            let minutes = event_trigger_time.getMinutes();
            event_trigger_time = hours + ':' + minutes

            console.log("scheduled sync time: " + scheduled_time)
            console.log("current event time: " + event_trigger_time)

            //if a sync was not kicked off then the event was likely a CRUD operation on the sync schedule
            if(scheduled_time === event_trigger_time) {
                console.log('starting the scheduled sync')
                let obj = this;
                try {
                    //sync occurs here
                } catch (e) {
                    console.log(e);
                }
            }
        }
    }
    renderData();
  },

triggered on iparams save
Schedule Change

  scheduleChanged: function(args) {
    var scheduleEnable = args.scheduleEnable;
    var scheduleTime = args.scheduleTime;

    $schedule.fetch({
      name: "sync"
    })
    .then(function() {
      if(scheduleEnable)
      {
        $schedule.update({
          name: "sync",
          data: {name: "sync"},
          schedule_at: formatScheduleTime(scheduleTime),
          repeat: {
            time_unit: "days",
            frequency: 1
          }
        })
        .then(function() {
            renderData(null,  {"message": "update schedule successfully."});
        }, function(err) {
            renderData(null,  err);
        });
      } else {
        $schedule.delete({
          name: "sync"
        })
        .then(function() {
            renderData(null,  {"message": "removed schedule"});
        }, function(err) {
            renderData(null,  err);
        });  
      }
    }, function() {
        if (scheduleEnable) {
            $schedule.create({
                name: "sync",
                data: {name: "sync"},
                schedule_at: formatScheduleTime(scheduleTime),
                repeat: {
                    time_unit: "days",
                    frequency: 1
                }
            })
                .then(function () {
                    renderData(null, {"message": "update schedule successfully"});
                }, function (err) {
                    renderData(null, err);
                });
        } else {
            return renderData(null, {"message": "schedule was not updated"});
        }
    })
  },