-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #676 from cj123/schedule-event-fix
Schedule event fix
- Loading branch information
Showing
11 changed files
with
171 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,8 @@ | |
|
||
.pl-4-5 { | ||
padding-left: 2rem!important; | ||
} | ||
} | ||
|
||
.fc-today { | ||
background: #9ec6f5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package when | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
// Resolution is how often each When is checked. | ||
Resolution = time.Second | ||
|
||
timers = make(map[*Timer]bool) | ||
mutex = sync.Mutex{} | ||
once = sync.Once{} | ||
) | ||
|
||
type Timer struct { | ||
fn func() | ||
t time.Time | ||
} | ||
|
||
func newTimer(t time.Time, fn func()) *Timer { | ||
r := &Timer{ | ||
t: t, | ||
fn: fn, | ||
} | ||
|
||
return r | ||
} | ||
|
||
func (t *Timer) Stop() { | ||
mutex.Lock() | ||
delete(timers, t) | ||
mutex.Unlock() | ||
} | ||
|
||
var ErrTimeInPast = errors.New("when: time specified is in the past") | ||
|
||
func When(t time.Time, fn func()) (*Timer, error) { | ||
if t.Before(time.Now()) { | ||
return nil, ErrTimeInPast | ||
} | ||
|
||
once.Do(func() { | ||
go func() { | ||
ticker := time.NewTicker(Resolution) | ||
|
||
for tick := range ticker.C { | ||
var toStop []*Timer | ||
|
||
mutex.Lock() | ||
for timer := range timers { | ||
if tick.Round(Resolution).Equal(timer.t.Round(Resolution)) || tick.Round(Resolution).After(timer.t.Round(Resolution)) { | ||
logrus.Debugf("Starting scheduled event (is now %s)", timer.t) | ||
go timer.fn() | ||
toStop = append(toStop, timer) | ||
} | ||
} | ||
mutex.Unlock() | ||
|
||
for _, timer := range toStop { | ||
timer.Stop() | ||
} | ||
} | ||
}() | ||
}) | ||
|
||
x := newTimer(t, fn) | ||
|
||
mutex.Lock() | ||
timers[x] = true | ||
mutex.Unlock() | ||
|
||
return x, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.