Page 1 of 1

Scheduled Task getting stopped automatically

Posted: 18 May 2012, 11:46
by Siddhant
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 !

Re: Scheduled Task getting stopped automatically

Posted: 18 May 2012, 20:12
by rjgtav
Hello.
Are you running the latest patch (2.1.0)?
And do you run any code at the USER_LOGIN or USER_DISCONNECT events?

Re: Scheduled Task getting stopped automatically

Posted: 19 May 2012, 08:32
by Lapo
An exception can cause the task to stop.
Make sure to handle runtime exceptions in order to avoid the problem.

Re: Scheduled Task getting stopped automatically

Posted: 19 May 2012, 21:15
by foxboy
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()
}

Re: Scheduled Task getting stopped automatically

Posted: 20 May 2012, 09:46
by Lapo
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

Re: Scheduled Task getting stopped automatically

Posted: 20 May 2012, 13:32
by Siddhant
@Lapo thanks a lot ..... we will execute our code in try catch block now .. i will update about the progress ....

Re: Scheduled Task getting stopped automatically

Posted: 20 May 2012, 19:59
by foxboy
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?

Re: Scheduled Task getting stopped automatically

Posted: 21 May 2012, 07:46
by Lapo
@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.

Re: Scheduled Task getting stopped automatically

Posted: 21 May 2012, 08:18
by foxboy
Thanks Lapo. I think I am gonna try your approach and see what happens.

Re: Scheduled Task getting stopped automatically

Posted: 11 Mar 2019, 12:43
by Prahlad Yogi
Hello

Why live smtartfox server is stop automatically.

Re: Scheduled Task getting stopped automatically

Posted: 12 Mar 2019, 14:46
by Lapo
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