https://github.com/eddort/scheduler
a simple, concurrent task scheduler written in Go
https://github.com/eddort/scheduler
go go-scheduler golang golang-library golang-package
Last synced: about 16 hours ago
JSON representation
a simple, concurrent task scheduler written in Go
- Host: GitHub
- URL: https://github.com/eddort/scheduler
- Owner: eddort
- Created: 2023-04-02T16:36:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-01T20:45:14.000Z (about 3 years ago)
- Last Synced: 2025-02-24T08:13:57.503Z (over 1 year ago)
- Topics: go, go-scheduler, golang, golang-library, golang-package
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Scheduler
A simple, concurrent task scheduler written in Go.
## Features
- Register tasks with unique names, intervals, and deadlines
- Concurrently execute tasks in separate goroutines
- Automatically prevent multiple instances of the same task from running simultaneously
- Gracefully handle task deadlines with context cancellation
## Usage
```go
package main
import (
"errors"
"fmt"
"time"
"github.com/eddort/scheduler"
)
func main() {
counter := 0
registry := scheduler.New()
taskConfig := scheduler.TaskConfig{
Name: "PrintTime",
Interval: 2 * time.Second,
Deadline: 3 * time.Second,
Action: func(payload scheduler.Payload) error {
fmt.Println("Current time:", time.Now())
time.Sleep(1 * time.Second)
if counter%2 == 0 {
return nil
}
return errors.New("what's up")
},
Middlewares: []scheduler.Middleware{func(next scheduler.ActionFunc) scheduler.ActionFunc {
return func(payload scheduler.Payload) error {
counter++
fmt.Println("Before task execution:", payload.Name)
err := next(payload)
if err != nil {
fmt.Println("Task finished with an error:", err)
} else {
fmt.Println("Task finished correctly")
}
return err
}
}},
}
// Register and start the task
registry.RegisterTask(taskConfig)
go func() {
time.Sleep(20 * time.Second)
registry.Stop()
}()
registry.Start()
}
```
## Installation
```bash
go get -u github.com/eddort/scheduler
```
## Testing
We use the [testify](https://github.com/stretchr/testify) library for testing. Install it with:
```bash
go get -u github.com/stretchr/testify
```
Then run tests with:
```bash
go test
```
## License
This project is licensed under the MIT License.