()
| 101 | } |
| 102 | |
| 103 | func cron() { |
| 104 | if len(timerManager.createdTimer) > 0 { |
| 105 | timerManager.muCreatedTimer.Lock() |
| 106 | for _, t := range timerManager.createdTimer { |
| 107 | timerManager.timers[t.id] = t |
| 108 | } |
| 109 | timerManager.createdTimer = timerManager.createdTimer[:0] |
| 110 | timerManager.muCreatedTimer.Unlock() |
| 111 | } |
| 112 | |
| 113 | if len(timerManager.timers) < 1 { |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | now := time.Now() |
| 118 | unn := now.UnixNano() |
| 119 | for id, t := range timerManager.timers { |
| 120 | if t.counter == infinite || t.counter > 0 { |
| 121 | // condition timer |
| 122 | if t.condition != nil { |
| 123 | if t.condition.Check(now) { |
| 124 | safecall(id, t.fn) |
| 125 | } |
| 126 | continue |
| 127 | } |
| 128 | |
| 129 | // execute job |
| 130 | if t.createAt+t.elapse <= unn { |
| 131 | safecall(id, t.fn) |
| 132 | t.elapse += int64(t.interval) |
| 133 | |
| 134 | // update timer counter |
| 135 | if t.counter != infinite && t.counter > 0 { |
| 136 | t.counter-- |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if t.counter == 0 { |
| 142 | timerManager.muClosingTimer.Lock() |
| 143 | timerManager.closingTimer = append(timerManager.closingTimer, t.id) |
| 144 | timerManager.muClosingTimer.Unlock() |
| 145 | continue |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if len(timerManager.closingTimer) > 0 { |
| 150 | timerManager.muClosingTimer.Lock() |
| 151 | for _, id := range timerManager.closingTimer { |
| 152 | delete(timerManager.timers, id) |
| 153 | } |
| 154 | timerManager.closingTimer = timerManager.closingTimer[:0] |
| 155 | timerManager.muClosingTimer.Unlock() |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // NewTimer returns a new Timer containing a function that will be called |
| 160 | // with a period specified by the duration argument. It adjusts the intervals |
searching dependent graphs…