Scheduler isn't accurate?

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

beatmapper
Posts: 20
Joined: 26 Dec 2007, 07:48

Scheduler isn't accurate?

Post by beatmapper »

howdy,

iv'e been playing with the Scheduler you provided, and i have some questions:

a. isn't it accurate? why is it that when i perform a task every second, and i trace that call every "tick", it doesn't look like it traces every second clock like. i mean, it looks as if it has many glitches in miliseconds - compared to a setInterval with a trace that looks on the trace very accurate.
b. assuming it IS accurate, can't i run tasks in less the a second interval? i tried passing .25 to as the "seconds" parameter, but it didn't look like it's working.
beatmapper
Posts: 20
Joined: 26 Dec 2007, 07:48

more

Post by beatmapper »

and now that i think of it, could it be that the schedualer is connected to the machine clock? if it is, then it suffers from time sync issues just like the machine clock. (when your clock is trying to make up for "lost" time in order to sync with the right time) and therefor cannot be trusted as a source for accurate timed tasks.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

The scheduler has a default "resolution" which is by default set to .750 milliseconds. You can change it to a lower value when instantiating the Scheduler object by passing a lower value to the constructor, like 200 milliseconds.
Lapo
--
gotoAndPlay()
...addicted to flash games
darnpunk
Posts: 229
Joined: 22 Jun 2007, 02:58
Location: SG

Post by darnpunk »

Hi,

I noticed the scheduler is not "cleared" when I use the destroy() method as said in the tutorials. It is also returning errors.

Code: Select all

function destroy()
{
	// when I call this function it returns an error 
// Can't find method it.gotoandplay.smartfoxserver.util.scheduling.Scheduler.destroy()
	scheduler.destroy()
}
However it does not return an error if I insert the scheduler object as a param like this:

Code: Select all

function destroy()
{
	scheduler.destroy(scheduler)
}
Even though the error does not repeat when I did the above, the interval does not seem to clear. I noticed that everytime I reloaded an extension with a scheduler, the active threads keep on increasing. One observation is through a countdown timer.

At normal startup, it will countdown properly every second 10...9...8...7... and so on. However after reloading, it will countdown 10...9...7...6...4. I believe it's cos of the scheduler not cleared properly? I followed every step in the scheduler tutorial. Am I missing something or anyone else experiencing this too?

Regards,
darnpunk
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Thanks for additional details. We'll test it asap and report what we have found
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

darnpunk:
SFS 1.6 comes with a scheduler example written in Actionscript.
It's called schedulerTest.as and it's found in the sfsExtensions/ folder.

We tried it repeatedly restarting the server and didn't notice any problems.
NOTE:
The destroy() method takes an object as 1st parameter. In this specific case it's not used, so you can safely pass a null.
In other words:

Code: Select all

schedulerInstance.destroy(null)
HTH
Lapo
--
gotoAndPlay()
...addicted to flash games
darnpunk
Posts: 229
Joined: 22 Jun 2007, 02:58
Location: SG

Post by darnpunk »

Hi Lapo,

I just tried again with the example you've mentioned but still it's the same. Firstly, whenever I reload the schedulerTest.as extension, I notice an increase in the active threads. Each reload adds a new active thread.

I am assuming this is due to an interval not cleared as I used to have this problem sometime ago. It was solved when I cleared the interval properly. Correct me if I am wrong :)

Secondly, the traces after reloading the extension looks like this (i set the loop to true instead of the default false):

[schedulerTest.as]: Executing task: have breakfast
[schedulerTest.as]: Executing task: work a bit
[schedulerTest.as]: Start at: 14:00, End at: 17:00
[schedulerTest.as]: Executing task: have breakfast
[schedulerTest.as]: Executing task: work a bit
[schedulerTest.as]: Start at: 14:00, End at: 17:00
[schedulerTest.as]: Executing task: have breakfast
[schedulerTest.as]: Executing task: have breakfast // notice the repetitions
[schedulerTest.as]: Executing task: work a bit
[schedulerTest.as]: Start at: 14:00, End at: 17:00
[schedulerTest.as]: Executing task: work a bit
[schedulerTest.as]: Start at: 14:00, End at: 17:00
[schedulerTest.as]: Executing task: have breakfast
[schedulerTest.as]: Executing task: have breakfast // same here
[schedulerTest.as]: Executing task: work a bit
[schedulerTest.as]: Start at: 14:00, End at: 17:00

The only changes I made to the original script was to set the loop to true for all tasks and passing in the scheduler object into the destroy function - scheduler.destroy(scheduler). Somehow passing in a null does not work.

I am using SFS 1.6 out of the box and only added schedulerTest.as into simpleChat zone for testing. Restarting the server helps but it happens again right after I reload the extension. Any ideas?

Regards,
darnpunk
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Hi,
I've spotted a problem in the example code that prevents the Scheduler to be destroyed correctly.

As you can see the scheduler variable is declared in the global scope at line 14

Code: Select all

// Global variable
var scheduler = null
The error is in the init() method, line 36

Code: Select all

var scheduler = new _scheduler.Scheduler()
It should be:

Code: Select all

scheduler = new _scheduler.Scheduler()
because the "var" keyword makes it a local variable, so the global scheduler variable will stay null and when the destroy() is invoked it will fire a NullPointerException and won't shut down the Scheduler as well.

As regards the thread count increasing upon multiple restarts I don't think it's related with the Scheduler. We'll look into that asap.
Lapo
--
gotoAndPlay()
...addicted to flash games
darnpunk
Posts: 229
Joined: 22 Jun 2007, 02:58
Location: SG

Post by darnpunk »

Hi Lapo,

Yes the scheduler is destroyed properly now. I'll be looking forward for the reason on the increasing number of active threads. Anyway, is the Scheduler class a Singleton?

Regards,
darnpunk
tobypb
Posts: 48
Joined: 15 Jun 2007, 20:50

Post by tobypb »

Is it possible to pass the scheduler task times of less than one second. It seems strange that the scheduler is constructed with a time that is in milliseconds but when tasks are created they are created on the basis of seconds..
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

The scheduler is not done for "high-frequency" tasks ... it is more for timed events, like move timers, bomb timers etc...
Since it's single threaded and it can handle many tasks at once, if you need very fast events it might become inaccurate.

Maybe, as a future improvement, we could add the ability to use faster event in conjunction with a pool of worker threads
Lapo
--
gotoAndPlay()
...addicted to flash games
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Post by jamieyg3 »

tobypb wrote:Is it possible to pass the scheduler task times of less than one second. It seems strange that the scheduler is constructed with a time that is in milliseconds but when tasks are created they are created on the basis of seconds..
I would also like a scheduler that is not based on seconds, like I want to have a task go every 1.5 seconds for example.

What would be the best way to achieve this? So far I have found that I can do this:

scheduler = new Scheduler(1500);
scheduler.addScheduledTask(loopingTaks, 0, true, handler);

but if I want one task to be 1.5 seconds and another to be 2.5 seconds I would have to make 2 schedulers for them.

Is there a better way to do this?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I am sorry, there's no way to use fractions of a second.
As I mentioned the Scheduler is done for tasks that recur in seconds, so it is not precise to the millisecond.

The typical use case of a Scheduler are: game timers, game events, time-based checks (for example empty rooms), time-based auto-saves etc...

All these examples are usually require several seconds or minutes between each call so they can all be handled by the same thread and they don't require time precision in the order of milliseconds.

If you need a swiss-clock type precision you can start your own threads/timers
Lapo
--
gotoAndPlay()
...addicted to flash games
falcan
Posts: 44
Joined: 04 Mar 2009, 12:55

Bump

Post by falcan »

Hey guys, bumping this up because this scheduler not wanting to quit caused me some headache, I somehow corrected it myself and now while searching for something else I see this thread - you've still got the lines there though,eeek

var scheduler = null

combined with

var scheduler = new _scheduler.Scheduler()

Both in docs and in the as example, please finally correct it, you will save nerves of more people hopefully :) Not everyone reads the whole forum before he starts to work on examples...Thanks. ;)
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Sorry what are you referring to?
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply