Project

General

Profile

Return remaining time of timer

Added by David Fredriksson over 2 years ago


A lot of times we update an ongoing timer.
I use this code to do so:
<pre>

function updateOngoingNonRepetiveJob(jobName, updatedTimeInSeconds, callbackFunctionForJob)
{
    var restart = false;
    try
    {
        Controller.cancelJob(jobName);
        restart = true;

    }
    catch (error)
    {
        print("Couldn't stop a job, possibly because there where no such job active!");
        print(error);
    }
    if(restart)
        print("Restart job " + jobName + "!");
    else
        print("Starts job " + jobName + " initially!");

    Controller.startJob(jobName,updatedTimeInSeconds,callbackFunctionForJob); 
}

</pre>

However, a lot of complicated code would be a lot easier if a function like this also returned number of remaining ms of the timer.


Replies (9)

RE: Return remaining time of timer - Added by Torbjorn Carlqvist Admin over 2 years ago

Hi David

Controller.startJob(...) always checks if a job with this name is already running. If that is the case the startJob function will first cancel the existing job before starting a new job with that name. So basically you can call Controller.startJob any time in order to RESET.
Does that solve your problem?

RE: Return remaining time of timer - Added by David Fredriksson over 2 years ago

Hi Torbjörn!

Not really actually.
So there are two parts to this:
1. If Controller.startJob(...) always checks if a job with this name is already running without generating any error well that's great because than I can skip the try-catch. However..
2. What I really want is to get the remaining number of milliseconds, as a return statement, when the timer is updated or 0 if the timer is not running.

Way would anyone want that?
Well, a lot of time we are dealing with reccuring events, where the time between samples may vary from batch to batch but shouldn't vary that much within the same batch.
Now a dynamic watchdog would (start high) adjust it's timeout due to what the remaining time was (with some margin) when the job was forwarded in time.

RE: Return remaining time of timer - Added by Torbjorn Carlqvist Admin over 2 years ago

I see..

I think better up is to respond with a JSON object telling a little bit more about what startJob actually performs when called.
Then IF a job was canceled the response could tell what whould have bean the next scheduled event of that canceled job if it would have bean allowed to continue running.

Example when calling this statement and then calling it again in approx 5sec.

Controller.startJob('Job2',10,'myCallbackFunction');

Could result in:

{
   "newJob":
   {
      "nextFireEpoc":1661937046214,
      "nextFire":"Wed Aug 31 11:10:46 CEST 2022"},
   "canceledJob":
   {
       "code":"",
       "callback":"myCallbackFunction",
       "Job2_trg":
       {
          "nextFireEpoc":1661937050467,
          "msLeftUntilNextFire":5251,
          "nextFire":"Wed Aug 31 11:10:50 CEST 2022",
          "state":"NORMAL" 
       }
    }
}

The msLeftUntilNextFire might give you what you requesting?

RE: Return remaining time of timer - Added by David Fredriksson over 2 years ago

Well that should do the trick!
I can make a wrapper to select what I need in a specific case.

Just one question:
If we are postponing the fire, shouldn't the "newJob" occure after the canceled job or am I missunderstanding the example?

RE: Return remaining time of timer - Added by Torbjorn Carlqvist Admin over 2 years ago

Good!

I created the response above by hand so it was a typo. Well spotted and important notice of course.

Here is a real response from a prototype I just made:

First call to Controller.startJob('Job3',10,'myCallbackFunction'); returned:

{
    "newJob": {
        "nextFireEpoc": 1661946784979,
        "nextFire": "Wed Aug 31 13:53:04 CEST 2022" 
    }
}

Second call to Controller.startJob('Job3',10,'myCallbackFunction'); a few seconds later:

{
    "newJob": {
        "nextFireEpoc": 1661946788842,
        "nextFire": "Wed Aug 31 13:53:08 CEST 2022" 
    },
    "canceledJob": {
        "Job3_trg": {
            "msLeftUntilNextFire": 6136,
            "nextFireEpoc": 1661946784979,
            "nextFire": "Wed Aug 31 13:53:04 CEST 2022",
            "state": "NORMAL" 
        },
        "code": "",
        "callback": "myCallbackFunction" 
    }
}

RE: Return remaining time of timer - Added by David Fredriksson over 2 years ago

Ok, nice!

In what release will this be in?

So, in order to get the remaining time, untill the canceled fire, the wrapper will have to make a new timestamp and subtract it from the canceled job nextfire after pharsing the data right?

Also, what is Epoc ?

RE: Return remaining time of timer - Added by Torbjorn Carlqvist Admin over 2 years ago

First, I will create a new issue for this where I put you as watcher so you can follow the progress. The release version for this is not yet determined.

msLeftUntilNextFire parameter gives you that directly, in milliseconds. This is the supposed time left from now to the upcoming event for that job in the moment you canceled it.
Epoc means you get a convinient way of getting a javascript Date variable without the need of formatting the "nextFire" string.

(Note, EPOC is the number of seconds since 1 January 1970. And in this case already *1000 to get it in millisecs.)

Eg.

var nextFireEpoc = 1661946784979; //From the respone above
mydate = new Date(nextFireEpoc); //Convert to JS Date obj
print(mydate.toLocaleString()); //Print the text version
print(mydate.getHours()); //Or get pice by pice

RE: Return remaining time of timer - Added by David Fredriksson over 2 years ago

Tested this startjob in v3.0.14¶

function testRepeat()
{
    var nu = new Date;
   print("testRepeat() anropades " + nu);

}

function printTimerString(timerReturnString)
{
    var obj =  JSON.parse(timerReturnString);

    print("\nnewJob");
    print("nextFireEpoc: " + obj.newJob.nextFireEpoc);
    print("name: " + obj.newJob.name);
    print("nextFire: " + obj.newJob.nextFire);

    try
    {
        print("\ncanceledJob");
        print("nextFireEpoc: " + obj.canceledJob.code);
        print("name: " + obj.canceledJob.name);
        print("nextFireEpoc: " + obj.canceledJob.callback);

        print("msLeftUntilNextFire: " + obj.canceledJob.testJob_trg.msLeftUntilNextFire);
        print("nextFireEpoc: " + obj.canceledJob.testJob_trg.nextFireEpoc);
        print("nextFire: " + obj.canceledJob.testJob_trg.nextFire);

    }
    catch
    {
        print("Nothing was canceled!");
    }
}

//Start job
//Eg. executes function myCallbackFunction() after 10 seconds
var retString =  Controller.startJob('testJob',10,'testRepeat');

printTimerString(retString);

The result when the time was alowed to timout was:


newJob
nextFireEpoc: 1662129377355
name: testJob
nextFire: Fri Sep 02 16:36:17 CEST 2022

canceledJob
Nothing was canceled!

And if the same job was called before the fire the result was:


newJob
nextFireEpoc: 1662129381187
name: testJob
nextFire: Fri Sep 02 16:36:21 CEST 2022

canceledJob
nextFireEpoc: 
name: testJob
nextFireEpoc: testRepeat
msLeftUntilNextFire: 6168
nextFireEpoc: 1662129377355
nextFire: Fri Sep 02 16:36:17 CEST 2022

So a function that returns remaning time may be:

function getRemaingTimerFromJson(timerReturnString)
{
    var obj =  JSON.parse(timerReturnString);

    var returnstatement = null;

    try
    {
       returnstatement = (obj.canceledJob.testJob_trg.msLeftUntilNextFire);
    }
    catch(error)
    {
      returnstatement = null;
    }
    return (returnstatement);
}

    (1-9/9)