Cleaning up when server shuts down

Discussion in 'Plugin Development' started by Eyvind, Jan 27, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Eyvind

    Apparently addons aren't disabled when the server is shutdown cleanly, I find that curious but it's neither here nor there. How would I best go about cleaning up after my plugin has been running for a while when the server shuts down?
     
  2. Offline

    DerpinLlama

    There's a name I haven't seen in a while... (Been about a year since I last saw you on Discovery, lol.)

    Anyway, I don't think we have a hook that's called at shutdown apart from plugin disable. What do you mean by 'cleaning up'?
     
  3. Offline

    blaatz0r

    I think the Java garbage collector does all that stuff for you.

    (I assume you're talking about writing your own plugin: ) If you would have any reason to call the onDisable function, you could set all objects to null there..
     
  4. Offline

    Eyvind

    I realize that I don't need to garbage collect, but leaving unsaved data or potentially threads still doing their thing when the server shuts down isn't necessarily good. Imagine if there is a thread doing file operations, it should be told that it needs to stop.

    The server saves the chunks and takes care of itself, is there absolutely no way for a plugin to do that? Most simply, why can't I save my plugin data when the server saves its data? Clean server shutdown is the most obvious time to do that.

    It really seems like it would be best, if all plugins were thrown a disable event. Of course, I don't really mind if it isn't that simple, but there should be a way to do it nevertheless.

    Can't say I recognize your name, but it's a pleasure meeting you again, whoever you are!
     
  5. Offline

    DerpinLlama

    Heh, I was Alex. Decided to use a different name for Bukkit but didn't know what to chose. Naturally, it had to be the most ridiculous one I could find. :D

    Java should - in theory - kill threads all threads when their process dies.
     
  6. Offline

    Eyvind

    The threads will die, but not necessarily in a clean way. However, I'm not using any threads, at least not yet, I was using it mostly as an example.

    The reason I posted this was because I was looking for an easy way to save some data to the disk when the server shuts down so the plugin can pick up from where it left off when the server comes back up.
     
  7. Offline

    DerpinLlama

    You could just save all data by default then only load it into memory when needed.

    Can you be more specific? (i.e. types of data)

    Edit: Also from IRC:
    <DerpinLlama> Is onDisable called on server shutdown?
    <tkelly> nope
     
  8. Offline

    Eyvind

    The data my plugin uses is currently being stored in a HashMap which I serialize to a file for persistency.

    I could serialize it every time it changes, but that would be too many disk writes for any server admin to want to use my plugin. Any sort of periodic save I can think of would be untidy, not optimal, or prone to corruption.

    Ideally, I would only want to save under one of three conditions:
    1) Someone manually tells the plugin to save
    2) The world is saved
    3) The server shuts down (and the world is saved)

    If the latter two are not possible with Bukkit, then I feel that would be a very necessary addition.
     
  9. Offline

    tkelly

    The best strategy would be to have your plugin be prepared for a shutdown at any given moment (if reading files, open and close the streams relatively close together, don't just leave them open, etc)

    But certain things might just require knowing when the server stops. Right now if server admins use "stop", there's not a way to detect that, but there is a redmine ticket open for this @ http://redmine.bukkit.org/issues/234
     
  10. Offline

    Eyvind

    I put all my stream closes in finally clauses, which solves any problems the vast majority of the time.

    That will be near-perfect. Only thing missing then would be an event thrown when the world is saved, since if the server crashes plugin data would essentially be rolled back to the last time the server was started. There's a good reason for periodic world saves! While plugin saves perhaps aren't as important as world saves, if plugin saves happened simultaneously, server admins will have more effortless control over the process. I'd say that would be a good thing.
     
  11. Offline

    tkelly

    It's been pushed to the main (Craft)Bukkit repos.

    As for the periodic save stuff, that would require a different system where plugins have an onSave() method or something like that. If you drafted it up, and made a ticket for it, that would be the best plan.
     
  12. Offline

    eisental

    Great! thanks for finally adding it [​IMG]
     
  13. Offline

    M1sT3rM4n

    What is the command portion you type to activate the trash collector?
     
  14. Offline

    Eyvind

    I don't know if this is what you had in mind, but: http://redmine.bukkit.org/issues/315

    The garbage collector runs automatically, I don't think there is ever many good reasons to call on it yourself. I believe the JVM tries to pick the best possible time to do it, either in a sustained resource consumption low or just prior to a resource consumption high -- those are the two primary points I remember reading, somewhere. With all the JVM will be doing that you don't see much of, especially if you are coding a plugin where you don't even have any control over what the majority of the software architecture is doing, I strongly doubt there is any reason to garbage collect yourself.

    That said, even if you do call the garbage collector, the specification states that it is no guarantee that the garbage collection will happen at that point, further reducing the reasons to try it.
     
  15. Offline

    M1sT3rM4n

    I remembered seeing someone giving the command out to someone else when he provided instructions as to how to start the run.bat file for Bukkit. I just thought it was an interesting read.
     
  16. Offline

    eisental

    Code:
    System.gc();
    I think it might be useful when you need to have stable timing and you use a lot of memory. The garbage collector can sometime cause a small 'hickup' in the middle of important processes when you let it run automatically.
     
  17. Offline

    Eyvind

    As long as you don't rely on it, there's never a guarantee that anything will happen.
     
  18. Offline

    NathanWolf

    Thank you very much, I was looking for exactly this!

    I'm making a generalized persistence plugin, and I wanted to work with an in-memory cache and only save on demand or on server shutdown. I thought onDisable was the way to go, but it didn't seem to be working.

    I'll go update! :)
     
  19. Offline

    tkelly

    I think it was broken/removed (by accident) in a recent update >.>

    I'll work on readding it sometime...
     
  20. Offline

    NathanWolf

    Hm... I'm not positive, but it seems like the disablePlugins() call might've gotten lost in the recent mc-dev refactor? Is anyone else experiencing this, or is my code pull messed up somehow?
    --- merged: Feb 1, 2011 4:34 AM ---
    Ah :) You beat me to it.

    As long as I can count on it long-term, it's fine by me. I can force a save manually with a console command, so it's not a big deal for now.
     
Thread Status:
Not open for further replies.

Share This Page