https://github.com/soypat/schedule
Action scheduling using event loops.
https://github.com/soypat/schedule
Last synced: about 2 months ago
JSON representation
Action scheduling using event loops.
- Host: GitHub
- URL: https://github.com/soypat/schedule
- Owner: soypat
- License: bsd-3-clause
- Created: 2023-09-12T00:21:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-08T21:13:36.000Z (over 1 year ago)
- Last Synced: 2025-01-08T12:40:42.561Z (4 months ago)
- Language: Go
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-tinygo - schedule - Event-loop scheduling library for synchronizing actions over long periods of time. (Embedded Systems / General use)
README
# schedule
[](https://pkg.go.dev/github.com/soypat/schedule)
[](https://goreportcard.com/report/github.com/soypat/schedule)
[](https://codecov.io/gh/soypat/schedule)
[](https://github.com/soypat/schedule/actions/workflows/go.yml)
[](https://sourcegraph.com/github.com/soypat/schedule?badge)Action scheduling using event loops.
The basic building unit of schedules is the `Group` interface.
```go
type Group interface {
// Begins sets the start time of the group. It must be called before ScheduleNext.
// It should reset internal state of the Group so that Group can be reused.
Begins(time.Time)
// ScheduleNext returns the next action when `ok` is true
// and returns the action value v.
// When ok=false and next=0 the Group is done.
ScheduleNext(time.Time) (v any, ok bool, next time.Duration, err error)
}
```A more complete interface could be:
```go
type GroupFull interface {
Group
// Start time returns the time the group was started at.
StartTime() time.Time
// Durations returns how long a single iteration lasts.
Duration() time.Duration
// Amount of times group will run. -1 for infinite iterations.
Iterations() int
}
```## Example
The example below demonstrates a group scheduled to add values to
sum over the course of 1.5 seconds.```go
// g is a Group with an integer value type.
var sum int
const resolution = time.Second/6
start := time.Now()
g.Begins(start)
for {
v, ok, next, err := g.ScheduleNext(time.Now())
if err != nil {
panic(err)
}done := !ok && next == 0
if done {
break
} else if !ok {
continue
}
sum += v
fmt.Println("added", v, "to sum", sum)
time.Sleep(resolution)
}
fmt.Println("done!", time.Since(start))
```Outputs:
```
added 20 to sum 20
added 30 to sum 50
added 50 to sum 100
done! 1.5s
```