Prevent serverstop, wait for tasks to finish

Discussion in 'Plugin Development' started by Baba43, Apr 17, 2013.

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

    Baba43

    Hi,

    I want to ensure that my background-tasks gets finished even if the server is going to shutdown. When onDisable is called, all tasks are already disabled by the server, so this method is useless.

    I found the ServerCommand event where I can use setCommand to prevent the /stop command, but I'm sure that this won't prevent other plugins from stopping the server, would it?

    Maybe you guys have another idea how to delay a serverstop/reload.

    Thanks :)
     
  2. Well, you could keep tabs on your background tasks and have a while() with Thread.sleep() with 100ms or so inside your onDisable() to keep the thread stuck there until the tasks finish...

    However, you should really make a timeout for like 30 seconds or so and notify the user, something like "Waiting for background tasks to finish... timeout 30 seconds.".

    EDIT:
    Actually, are you sure your tasks are getting canceled before onDisable() ? Perform a check in there to be sure.

    And if they're still there, trigger them manually and wait for them like said above.

    However, if the tasks are running (code is executing run() method) then they should be interrupted by the server when shutting down I belive.

    By the way, I remember making an exception in my onDisable() and the server was unshutdownable after that =)
     
  3. Offline

    Morthis

    I assume we're talking about an async task then? Since I imagine a sync task would finish it's run method before the CommandExecutor can do anything. I guess if Bukkit shuts down tasks before even calling your plugin's onDisable you could simply schedule the thread using Java rather than through Bukkit, and then blocking the main thread in onDisable until your task finishes. For one of my plugins which needs to make a lot of SQL queries I use a thread pool using Java's ExecutorService and I know I can delay the server shutdown until those tasks are finished without issue.

    It sounds like the issue is the task closing before he has a chance to stop it rather than how to implement a delay. For a delay you could also do a wait and then a notify from the thread you're waiting on, but none of that does any good if the thread is shut down before he has any chance to stop Bukkit from doing so.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    Baba43

    I use an asynchronous task to do database-queries and use a repeating synchronous task to consume their results and get them back to the main thread. If onDisable is called and I hold on byy using thread.sleep some times, the asynchronous task continues but the synchronized task does not. Thats why I think that bukkit has automatically stopped the timer just before onDisable.
     
Thread Status:
Not open for further replies.

Share This Page