Sign In Register

How can we help you today?

Start a new topic
Answered

Changing a scheduled script

I schedule a script with the scheduler, give the time, params and id.


Now I want to change the time. I know the id of the schedule, but not the params.

How can I do it ?


- Can't delete and create again because I don't have the params

- I found no way to get a scheduled script infos even with its id...

- Could maintain a collection of all my running scripts, but painfull and prone to errors


Thanks !


Best Answer

Eric,


As far as I know this is the only way to go for your case where you cannot reconstitute parameters from existing collections. But it's not that painfull to implement.

Here's something that maybe will fit your need, It as not been tested and compiled:

  

var Scheduler = {
    schedule: function(eventShortCode, taskId, delayInSeconds, parameters){
        var taskData = {
            "id":taskId,
            "eventShortCode":eventShortCode,
            "parameters":parameters,
        }
        var taskData = Spark.runtimeCollection("ScheduledTask").findOne({"id":taskId});
        if (taskData !== null){
            throw "A task with id " + taskId + " is already scheduled";
        }
        
        Spark.getScheduler().inSeconds(eventShortCode, delayInSeconds, taskData, taskId);
    }
    
    reschedule: function(taskId, delayInSeconds){
        var taskData = Spark.runtimeCollection("ScheduledTask").findOne({"id":taskId});
        if (taskData === null){
            throw "Cannot find scheduled task " + taskId;
        }
        Spark.getScheduler().cancel(taskId);
        Spark.getScheduler().inSeconds(currentTask.eventShortCode, delayInSeconds, taskData, taskId);
    }
    
    //call this from your event function to remove the task data from the collection 
    taskComplete: function(taskId){
        Spark.runtimeCollection("ScheduledTask").remove({"id":taskId});
    }
}

schedule usage:
--------------------------
require("Scheduler");
//it must be a unique ID that you can recreate for task rescheduling
//player.getPlayerId() if taks are related to players, if you have multiple tasks for the same player append a suffix
var aUniqueTaskId = Spark.getPlayer().getPlayerId() + "_SEND_POWER_TASK";
Scheduler.schedule("SEND_POWER", aUniqueTaskId, 360, {"energy":3, "level":55, "color": "red"});

reschedule usage:
--------------------------
var aUniqueTaskId = Spark.getPlayer().getPlayerId() + "_SEND_POWER_TASK";
Scheduler.reschedule(aUniqueTaskId, 1800);

  


Hi Eric,


I think a collection of the scheduled scripts is a pretty good way to track these kinds of things.

If you find some aspect painful, let us know and we'll see if there is any way we can improve it.

You should be able to find the params you need here: Sparkscheduler.


Cheers,


Oisin


Hi ,


Sorry I'm not sure to understand.

Do you mean that I have to maintain a runtime collection with all the scripts I schedule ?

Then if I want to change the time, I query the runtime collection to find its parameters, then cancel the scheduled script, then create a new one, then update the runtime collection ?


This seems a little bit painfull and prone to errors.


Would be fine if there was an "update" method on SparkScheduler, or at least a "get" method to retrieve infos.

Once scheduled, we are in the dark...


Answer

Eric,


As far as I know this is the only way to go for your case where you cannot reconstitute parameters from existing collections. But it's not that painfull to implement.

Here's something that maybe will fit your need, It as not been tested and compiled:

  

var Scheduler = {
    schedule: function(eventShortCode, taskId, delayInSeconds, parameters){
        var taskData = {
            "id":taskId,
            "eventShortCode":eventShortCode,
            "parameters":parameters,
        }
        var taskData = Spark.runtimeCollection("ScheduledTask").findOne({"id":taskId});
        if (taskData !== null){
            throw "A task with id " + taskId + " is already scheduled";
        }
        
        Spark.getScheduler().inSeconds(eventShortCode, delayInSeconds, taskData, taskId);
    }
    
    reschedule: function(taskId, delayInSeconds){
        var taskData = Spark.runtimeCollection("ScheduledTask").findOne({"id":taskId});
        if (taskData === null){
            throw "Cannot find scheduled task " + taskId;
        }
        Spark.getScheduler().cancel(taskId);
        Spark.getScheduler().inSeconds(currentTask.eventShortCode, delayInSeconds, taskData, taskId);
    }
    
    //call this from your event function to remove the task data from the collection 
    taskComplete: function(taskId){
        Spark.runtimeCollection("ScheduledTask").remove({"id":taskId});
    }
}

schedule usage:
--------------------------
require("Scheduler");
//it must be a unique ID that you can recreate for task rescheduling
//player.getPlayerId() if taks are related to players, if you have multiple tasks for the same player append a suffix
var aUniqueTaskId = Spark.getPlayer().getPlayerId() + "_SEND_POWER_TASK";
Scheduler.schedule("SEND_POWER", aUniqueTaskId, 360, {"energy":3, "level":55, "color": "red"});

reschedule usage:
--------------------------
var aUniqueTaskId = Spark.getPlayer().getPlayerId() + "_SEND_POWER_TASK";
Scheduler.reschedule(aUniqueTaskId, 1800);

  

oops, I forgot to add the data in the collection, here's the fix

  

var Scheduler = {
    schedule: function(eventShortCode, taskId, delayInSeconds, parameters){
        var taskData = {
            "id":taskId,
            "eventShortCode":eventShortCode,
            "parameters":parameters,
        }
        var coll = Spark.runtimeCollection("ScheduledTask");
        var taskData = coll.findOne({"id":taskId});
        if (taskData !== null){
            throw "A task with id " + taskId + " is already scheduled";
        }
        
        coll.insert(taskData);

        Spark.getScheduler().inSeconds(eventShortCode, delayInSeconds, taskData, taskId);
    },
     
    reschedule: function(taskId, delayInSeconds){
        var taskData = Spark.runtimeCollection("ScheduledTask").findOne({"id":taskId});
        if (taskData === null){
            throw "Cannot find scheduled task " + taskId;
        }
        Spark.getScheduler().cancel(taskId);
        Spark.getScheduler().inSeconds(currentTask.eventShortCode, delayInSeconds, taskData, taskId);
    },
     
    //call this from your event function to remove the task data from the collection 
    taskComplete: function(taskId){
        Spark.runtimeCollection("ScheduledTask").remove({"id":taskId});
    }
}

schedule usage:
--------------------------
require("Scheduler");
//it must be a unique ID that you can recreate for task rescheduling
//player.getPlayerId() if taks are related to players, if you have multiple tasks for the same player append a suffix
var aUniqueTaskId = Spark.getPlayer().getPlayerId() + "_SEND_POWER_TASK";
Scheduler.schedule("SEND_POWER", aUniqueTaskId, 360, {"energy":3, "level":55, "color": "red"});
 
reschedule usage:
--------------------------
var aUniqueTaskId = Spark.getPlayer().getPlayerId() + "_SEND_POWER_TASK";
Scheduler.reschedule(aUniqueTaskId, 1800);

  

Hi Christian,

It makes sense to do this way, I'm going to implement it asap :)

Thanks !

Thanks for jumpin in with that advice Christian!

 

Login to post a comment