Cancel sync repeating task

Discussion in 'Plugin Development' started by Nogtail, Aug 11, 2014.

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

    Nogtail

    I have a plugin which relies on a database connection which is checked every tick and if it is found to be down then the plugin is disabled. When I tested it I found it was spamming the "plugin is disabled" message so after several hours of attempting to trace the issue I found it was because the synch task continues after the plugin is disabled. I then attempted to reload the server and found that the task continues even after a reload, after reloading a second time in a row the task is finally killed off. Is there a way to disable sync tasks? I have tried Bukkit.getScheduler().cancelTasks(this); but nothing appears to happen.

    Thanks :)
     
  2. Give the task an id...
    Code:java
    1. int ID;
    2.  
    3. ID = main.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable(){
    4.  
    5. Bukkit.getScheduler().cancelTask(ID);
     
  3. Offline

    hintss

    it's also good practice to
    Code:java
    1. getServer().getScheduler().cancelTasks(this);

    in your onDisable
     
  4. hintss Does putting (this) instead of the id cancel all tasks? I've always just used the cancelalltasks meathod or whatere it was...
     
  5. xYourFreindx cancelTasks(Plugin) cancels all tasks for a given Plugin. cancelAllTasks() cancels all tasks regardless of plugin. So, if someone was to disable just your plugin, you'd take revenge by cancelling all the other plugins' tasks as well :p

    hintss Why is that good practice? Bukkit automatically does it.
     
    _LB likes this.
  6. AdamQpzm xDD Where have you been all my life?
    (by all my life, I mean three plugin updates ago...)
     
    AdamQpzm likes this.
  7. Offline

    Nogtail

    xYourFreindx hintss AdamQpzm I am using getScheduler.cancelTasks(this) but nothing happens. When I save the tasks ID and use that to cancel the task it works fine but I would rather not use this.
     
  8. Nogtail Never said you should.
     
  9. Offline

    Nogtail

    Do you have any ideas on why it is not cancelling the tasks?
     
  10. Nogtail Well, would depend on where you use it, but why do you need to cancel all tasks for your plugin anyway?
     
  11. Offline

    Nogtail

    AdamQpzm I have tasks which require a connection to a database which like to throw a lot of errors if the database is not connected. When the database looses connection the plugin is disabled but the tasks continue.
     
  12. Offline

    unon1100

    I usually just use BukkitRunnable rather than BukkitScheduler. If you want to save the task to a variable, you can do that, and then BukkitTask#cancel(); to cancel, or if you don't need to disable the task externally, you can easily just do this.cancel(); within the BukkitRunnable.

    Example:

    External Cancel:
    Code:java
    1. BukkitTask task = new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. //code
    5. }
    6. }.runTaskTimer(plug, initDelay, delay);
    7. task.cancel();


    Internal Cancel:
    Code:java
    1. new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. //code
    5. if(terminator) this.cancel();
    6. }
    7. }.runTaskTimer(plug, initDelay, delay);
     
Thread Status:
Not open for further replies.

Share This Page