Run scheduled task everyday at midnight

Discussion in 'Plugin Development' started by Ne0nx3r0, Jan 2, 2013.

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

    Ne0nx3r0

    How would I go about doing this?

    I considered setting an hourly task and checking if the current hour is 0 using Calendar.HOUR_OF_DAY, but that seems a bit hacky.
     
  2. Offline

    Jogy34

    You could schedule a repeating task with the initial delay the time until midnight then the second delay being one day
     
  3. Offline

    CorrieKay

  4. Offline

    fireblast709

    Code:java
    1. Calendar midnight = Calendar.getInstance();
    2. midnight.set(Calendar.HOUR_OF_DAY, 0);
    3. midnight.set(Calendar.MINUTE, 0);
    4. midnight.set(Calendar.SECOND, 0);
    5. midnight.set(Calendar.MILLISECOND, 1);
    6. midnight.set(Calendar.DAY_OF_YEAR, midnight.get(Calendar.DAY_OF_YEAR) + 1);
    7. long tillMidnight = midnight.getTimeInMillis() - System.currentTimeMillis() - 1;
    8. long ticksTillMidnight = tillMidnight / 50;
     
    hawkfalcon, CorrieKay and Ne0nx3r0 like this.
  5. Offline

    Ne0nx3r0

    CorrieKay Well at least I'm not the only one XD

    So that as the initial delay, then 20*60*60*24 as the repeating time?

    Pretty clever.
     
  6. Offline

    fireblast709

    Yes
    Ty :3
     
    Ne0nx3r0 likes this.
  7. Offline

    CorrieKay

    Clever, yes, but i dont see it working. It works by ticks, which can be affected by lag. The more lag you get, the farther an farther it gets off course.
     
  8. Offline

    Ne0nx3r0

    That's a good point. Hrm. If the server ran for too long it would be pretty far off after a while.
     
  9. Offline

    fireblast709

    You could also schedule a repeating task that takes a portion of the 24 hours, and will recalculate the remaining time when called. Or recalculate the time at each midnight
     
  10. Offline

    CorrieKay

    You'd still have to account for the 24 hours between each firing of the event.

    Lets say you took 1.1 seconds to fire 20 ticks, instead of 1 second to fire 20 ticks. After an hour, youre suddenly six minutes late. After 24 hours, when it should fire, youre suddnly (if my math is correct) almost two and a half hours late.

    You would reach this discrepancy with just .1 second of lag. Thats 20 ticks in the time it would take 22 ticks. Its a little extreme, but its not unrealistic, and creates such a huge discrepancy of time. Two and a half hours is a HUGE amount of time to miss :\
     
  11. Offline

    fireblast709

    In that case you should store the starting time somewhere (I assume you start at midnight to keep things a bit easy) and use a repeating task to check each x amount of seconds (x depends on how precise you want it). Pseudo
    Code:
    main: // ignored
      x = System.currentTimeMillis()
      goto wait
    execute:
      // do stuff
      dif = System.currentTimeMillis() - x - millis in day
      x = System.currentTimeMillis() - dif
    wait:
      time = System.currentTimeMillis()
      if time - x > millis in day
        goto execute
      else
        goto wait
     
  12. Offline

    NemesisMate

    Why are you making the last sentence (tillMidnight / 50), mustn't it be tillMidnight * 0.02 (20/1000)?
     
  13. Offline

    blablubbabc

    1 / 50 = 0.02

    So it's the same.
     
    Ultimate_n00b likes this.
  14. Offline

    NemesisMate

    O.O xD ok, I accept your answer... [facepalm]
     
Thread Status:
Not open for further replies.

Share This Page