Scheduled Task getting stopped automatically

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Siddhant
Posts: 13
Joined: 04 Nov 2008, 18:37

Scheduled Task getting stopped automatically

Postby Siddhant » 18 May 2012, 11:46

Hi,
I am facing this very strange problem with Smartfox 2x. I am scheduling a task using

sfs.getTaskScheduler().scheduleAtFixedRate(new TaskRunner(), 0, 1, TimeUnit.SECONDS);

in Zone level extension ( this code is called in init() function in the zone level extension)

but after sometimes when a user logs off , logs on or many different users log on , Scheduled task is getting stopped automatically without giving out any error. There is absolutely no error on the console just that the trace from the schedule task handler stops coming on the console.

We are using just one TaskScheduler for the entire zone , also task is scheduled to be executed always after one second. We have set the thread pool size for the schedulers pool to be 4.

In the scheduled task we are sending messages to the different room extensions by iterating over the rooms and then calling their extensions handleInternalMessage.

Also would like to highlight one thing that sometimes with 4-5 users logged in and with 20 different rooms , taskscheduler goes fine but sometimes even with one user if i log on , log off again and again the scheduler gets stopped.

The problem mentioned is very critical for us and delaying our product to go online in the beta stage.

We have been doing similar things earlier with smartfox 1x but didnt face any issue but on smartfox 2x the scheduler thread is getting stopped for no reason. We have now moved our development platform to smartfox 2x and we are stuck with this issue.



Please help !
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Scheduled Task getting stopped automatically

Postby rjgtav » 18 May 2012, 20:12

Hello.
Are you running the latest patch (2.1.0)?
And do you run any code at the USER_LOGIN or USER_DISCONNECT events?
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Scheduled Task getting stopped automatically

Postby Lapo » 19 May 2012, 08:32

An exception can cause the task to stop.
Make sure to handle runtime exceptions in order to avoid the problem.
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
foxboy
Posts: 110
Joined: 12 May 2011, 02:47
Location: Optimal Solution Pte. Ltd.

Re: Scheduled Task getting stopped automatically

Postby foxboy » 19 May 2012, 21:15

Siddhant wrote:Hi,
I am facing this very strange problem with Smartfox 2x. I am scheduling a task using

sfs.getTaskScheduler().scheduleAtFixedRate(new TaskRunner(), 0, 1, TimeUnit.SECONDS);


I prefer to use the one time scheduler instead of the scheduleFixedRate(). On your init() method, schedule one task and set to your target interval. Then on your TaskRunner(), surround your task with try/catch/finally block and then schedule your next task inside the finally block. This will ensure that a new task will be created even after an exception has occured. Something like this:

Code: Select all

private class TaskRunner implements Runnable {   
   public void run()
        {
         try{
            // The code for your task
         } catch(Exception e) {

         } finally {
            sfs.getTaskScheduler().schedule(new TaskRunner() 1, TimeUnit.SECONDS);
         }
        } // run()
}
"Dream it, I'll code it..."
Lead Developer
Optimal Solution Pte. Ltd.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Scheduled Task getting stopped automatically

Postby Lapo » 20 May 2012, 09:46

No I wouldn't recommend this approach. It has several disadvantages:

1- it keeps recreating objects
2- you can't keep state because the class gets recreated if there's an error.

Adds complexity instead of simplifying things. The mos rational approach is just to catch Exceptions so that the task doesn't get interrupted. And if you need to interrupt it on a specific error you can always handle the exception first, do your cleanup and finally re-throw your exception to stop it.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Siddhant
Posts: 13
Joined: 04 Nov 2008, 18:37

Re: Scheduled Task getting stopped automatically

Postby Siddhant » 20 May 2012, 13:32

@Lapo thanks a lot ..... we will execute our code in try catch block now .. i will update about the progress ....
User avatar
foxboy
Posts: 110
Joined: 12 May 2011, 02:47
Location: Optimal Solution Pte. Ltd.

Re: Scheduled Task getting stopped automatically

Postby foxboy » 20 May 2012, 19:59

Lapo wrote:No I wouldn't recommend this approach. It has several disadvantages:

1- it keeps recreating objects
2- you can't keep state because the class gets recreated if there's an error.

Adds complexity instead of simplifying things. The mos rational approach is just to catch Exceptions so that the task doesn't get interrupted. And if you need to interrupt it on a specific error you can always handle the exception first, do your cleanup and finally re-throw your exception to stop it.

cheers


Hi Lapo, now I am confused :(. I actually used that approach as suggested on another post. Basically, on my implementation, I have attached one task scheduler to each room to periodically scan the game state and perform actions on it. I recreate the object like the above and recreate a new task scheduler after the task is completed successfully. if there is any error, the task will be aborted and no new task scheduler will be created. the reason why i am not doing the fixedrate scheduler is that the task involves database update and the task might run again while im still updating the database. before this approach, i have tried scanning all the rooms in a group using a task scheduler and then perform task based on the game state of each room. the problem is, if one game state requires database update, it will delay the scanning of the other rooms when i need to finish the scanning all the rooms in just 1 second.

since, i have learned now that this is a bad idea. what would be the best approach for this scenario?
"Dream it, I'll code it..."
Lead Developer
Optimal Solution Pte. Ltd.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Scheduled Task getting stopped automatically

Postby Lapo » 21 May 2012, 07:46

@foxboy: did I say it was a bad idea? I don't think so. I just said there's a simpler approach.
the task involves database update and the task might run again while im still updating the database.

Rescheduling doesn't offer any advantage. If the thread is still working on the previous task the new task will have to either use a different thread or wait.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
foxboy
Posts: 110
Joined: 12 May 2011, 02:47
Location: Optimal Solution Pte. Ltd.

Re: Scheduled Task getting stopped automatically

Postby foxboy » 21 May 2012, 08:18

Thanks Lapo. I think I am gonna try your approach and see what happens.
"Dream it, I'll code it..."
Lead Developer
Optimal Solution Pte. Ltd.
Prahlad Yogi
Posts: 1
Joined: 12 Jul 2018, 05:09

Re: Scheduled Task getting stopped automatically

Postby Prahlad Yogi » 11 Mar 2019, 12:43

Hello

Why live smtartfox server is stop automatically.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Scheduled Task getting stopped automatically

Postby Lapo » 12 Mar 2019, 14:46

Prahlad Yogi wrote:Hello

Why live smtartfox server is stop automatically.

Hi,
you should start a new thread, as this one is 6+ years old and it's not even relevant to what your problem seems to be.

Please start a new one and provide as many details as possible. If the server is stopped check the log files and look for errors, if there's any.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Stevenor and 74 guests