how to save room data when server shutdown

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

Moderators: Lapo, Bax

atoneday
Posts: 52
Joined: 18 Jun 2012, 08:44

how to save room data when server shutdown

Postby atoneday » 09 Jun 2020, 11:07

I use addShutdownHook for every room extention to save some status in the room. But when i try to shutdown the sever, some shutdown hooks execute and some do not. How to ensure every room save their data?

Code: Select all

public class BaseRoomExtension extends SFSExtension {
   @Override
   public void init() {
      trace(getClass().getName() + " Extension started@" + getGameRoom().getName());
      
      Runtime.getRuntime().addShutdownHook(new Thread() {
         public void run() {
            log("in ShutdownHook@"+ getGameRoom().getName() );
         }
      });
   }
}
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: how to save room data when server shutdown

Postby Lapo » 09 Jun 2020, 11:25

Hi,
there isn't a guarantee that shutdown hooks will all be executed when the JVM dies, and adding dozens of them will make it even less likely, because there's too many.

I would probably suggest to add one single shutdown hook that takes care of all Rooms. Even in this case, however, keep in mind that it may or may not work completely. Shutdown hooks are meant to execute quickly, if they take too much time to complete they might get interrupted.

A good strategy to save Room states is to mix the use of a shutdown hook with time-based saves. This should give you a "good enough" system to keep your data safe.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
atoneday
Posts: 52
Joined: 18 Jun 2012, 08:44

Re: how to save room data when server shutdown

Postby atoneday » 10 Jun 2020, 01:11

Thank you very much. I will add time-based saves.
One single shutdown hook means shutdown hook in zone extension, right?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: how to save room data when server shutdown

Postby Lapo » 10 Jun 2020, 07:15

Yes, you can add the shutdown hook in the init method of your Extension.

However, keep in mind that if your Extension is redeployed multiple times it might create a memory leak, because the previous hook still exists and it's pointing to a Thread object from the previous Extension.

To avoid this I would also add a call to Runtime.removeShutdownHook() in the destroy method of your Zone Extension.
Example:

Code: Select all

@Override
public void destroy()
{
   super.destroy();
   Runtime.removeShutdownHook(hook);
}


cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Baidu [Spider] and 95 guests