-
Notifications
You must be signed in to change notification settings - Fork 899
feat: send native system notification on scheduled workspace shutdown #1414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
125584c
8313a52
67516c7
55a1de2
e830baf
4f39908
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package notify | ||
|
||
import ( | ||
"sort" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Notifier calls a Condition at most once for each count in countdown. | ||
type Notifier struct { | ||
sync.Mutex | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
condition Condition | ||
notifiedAt map[time.Duration]bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to move this into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could move into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok that makes sense. I guess it's mostly a question of how the package is to be used and what the user (might) expect to happen. I imagine this will not be a problem for |
||
countdown []time.Duration | ||
} | ||
|
||
// Condition is a function that gets executed with a certain time. | ||
type Condition func(now time.Time) (deadline time.Time, callback func()) | ||
|
||
// New returns a Notifier that calls cond once every time it polls. | ||
// - Condition is a function that returns the deadline and a callback. | ||
// It should return the deadline for the notification, as well as a | ||
// callback function to execute once the time to the deadline is | ||
// less than one of the notify attempts. If deadline is the zero | ||
// time, callback will not be executed. | ||
// - Callback is executed once for every time the difference between deadline | ||
// and the current time is less than an element of countdown. | ||
// - To enforce a minimum interval between consecutive callbacks, truncate | ||
// the returned deadline to the minimum interval. | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// - Duplicate values are removed from countdown, and it is sorted in | ||
// descending order. | ||
func New(cond Condition, countdown ...time.Duration) *Notifier { | ||
// Ensure countdown is sorted in descending order and contains no duplicates. | ||
sort.Slice(unique(countdown), func(i, j int) bool { | ||
return countdown[i] < countdown[j] | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
n := &Notifier{ | ||
countdown: countdown, | ||
condition: cond, | ||
notifiedAt: make(map[time.Duration]bool), | ||
} | ||
|
||
return n | ||
} | ||
|
||
// Poll polls once immediately, and then once for every value from ticker. | ||
func (n *Notifier) Poll(ticker <-chan time.Time) { | ||
// poll once immediately | ||
n.pollOnce(time.Now()) | ||
for t := range ticker { | ||
n.pollOnce(t) | ||
} | ||
} | ||
|
||
func (n *Notifier) pollOnce(tick time.Time) { | ||
n.Lock() | ||
defer n.Unlock() | ||
|
||
deadline, callback := n.condition(tick) | ||
if deadline.IsZero() { | ||
return | ||
} | ||
|
||
timeRemaining := deadline.Sub(tick) | ||
for _, tock := range n.countdown { | ||
if timeRemaining <= tock && !n.notifiedAt[tock] { | ||
callback() | ||
n.notifiedAt[tock] = true | ||
} | ||
} | ||
} | ||
|
||
func unique(ds []time.Duration) []time.Duration { | ||
m := make(map[time.Duration]bool) | ||
for _, d := range ds { | ||
m[d] = true | ||
} | ||
ks := make([]time.Duration, 0) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for k := range m { | ||
ks = append(ks, k) | ||
} | ||
return ks | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package notify_test | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/coder/coder/coderd/autobuild/notify" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/atomic" | ||
"go.uber.org/goleak" | ||
) | ||
|
||
func TestNotifier(t *testing.T) { | ||
t.Parallel() | ||
|
||
now := time.Now() | ||
|
||
testCases := []struct { | ||
Name string | ||
Countdown []time.Duration | ||
Ticks []time.Time | ||
ConditionDeadline time.Time | ||
NumConditions int64 | ||
NumCallbacks int64 | ||
}{ | ||
{ | ||
Name: "zero deadline", | ||
Countdown: durations(), | ||
Ticks: ticks(now, 0), | ||
ConditionDeadline: time.Time{}, | ||
NumConditions: 1, | ||
NumCallbacks: 0, | ||
}, | ||
{ | ||
Name: "no calls", | ||
Countdown: durations(), | ||
Ticks: ticks(now, 0), | ||
ConditionDeadline: now, | ||
NumConditions: 1, | ||
NumCallbacks: 0, | ||
}, | ||
{ | ||
Name: "exactly one call", | ||
Countdown: durations(time.Second), | ||
Ticks: ticks(now, 2), | ||
ConditionDeadline: now.Add(2 * time.Second), | ||
NumConditions: 2, | ||
NumCallbacks: 1, | ||
}, | ||
{ | ||
Name: "two calls", | ||
Countdown: durations(4*time.Second, 2*time.Second), | ||
Ticks: ticks(now, 5), | ||
ConditionDeadline: now.Add(5 * time.Second), | ||
NumConditions: 6, | ||
NumCallbacks: 2, | ||
}, | ||
{ | ||
Name: "wrong order should not matter", | ||
Countdown: durations(2*time.Second, 4*time.Second), | ||
Ticks: ticks(now, 5), | ||
ConditionDeadline: now.Add(5 * time.Second), | ||
NumConditions: 6, | ||
NumCallbacks: 2, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
ch := make(chan time.Time) | ||
numConditions := atomic.NewInt64(0) | ||
numCalls := atomic.NewInt64(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool use of |
||
cond := func(time.Time) (time.Time, func()) { | ||
numConditions.Inc() | ||
return testCase.ConditionDeadline, func() { | ||
numCalls.Inc() | ||
} | ||
} | ||
var wg sync.WaitGroup | ||
go func() { | ||
n := notify.New(cond, testCase.Countdown...) | ||
n.Poll(ch) | ||
wg.Done() | ||
}() | ||
wg.Add(1) | ||
for _, tick := range testCase.Ticks { | ||
ch <- tick | ||
} | ||
close(ch) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
wg.Wait() | ||
require.Equal(t, testCase.NumCallbacks, numCalls.Load()) | ||
}) | ||
} | ||
} | ||
|
||
func durations(ds ...time.Duration) []time.Duration { | ||
return ds | ||
} | ||
|
||
func ticks(t time.Time, n int) []time.Time { | ||
ts := make([]time.Time, n+1) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ts = append(ts, t) | ||
for i := 0; i < n; i++ { | ||
ts = append(ts, t.Add(time.Duration(n)*time.Second)) | ||
} | ||
return ts | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
goleak.VerifyTestMain(m) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.