-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_example_test.go
More file actions
41 lines (33 loc) · 834 Bytes
/
runner_example_test.go
File metadata and controls
41 lines (33 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package runner_test
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/hemantjadon/runner"
)
type job struct {
}
func (j job) Run(ctx context.Context) {
select {
case <-ctx.Done():
time.Sleep(10 * time.Second)
}
}
func ExampleRunner() {
// Create a runner wrapping the job you want to execute.
jobRunner := runner.Runner{Runnable: job{}}
// Run the runner in a go routine.
go func() {
if err := jobRunner.Run(); err != nil && !errors.Is(err, runner.ErrClosed) {
_, _ = fmt.Fprintf(os.Stderr, "error running runner: %v\n", err)
}
}()
// Wait till the job is to be stopped.
time.Sleep(5 * time.Second)
// Then Close the runner, which will block till the runner completely stops.
if err := jobRunner.Close(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error stopping runner: %v\n", err)
}
}