Last updated:
0 purchases
The Go Task Scheduler (GTS) is a lightweight library designed for scheduling and executing goroutines after a specified duration or at a precise time. It also includes a storage mechanism to retain task history and handle task execution during application downtime.
Sqlite3
and NoOp
.System Requirements
SqliteStorage
).Library Requirements
bash
Copy code
go get github.com/rakanalh/scheduler
Installation
bash
Copy code
go get github.com/rakanalh/scheduler
2. Configuration
Step 1: Choose a Storage Option:
Step 2: Initialize the Storage and Scheduler:
Example using SQLite3:
go
Copy code
sqliteStorage := storage.NewSqlite3Storage(storage.Sqlite3Config{DbName: "db.store"}) if err := sqliteStorage.Connect(); err != nil { log.Fatal("Could not connect to the database", err) } if err := sqliteStorage.Initialize(); err != nil { log.Fatal("Could not initialize the database", err) } s := scheduler.New(sqliteStorage)
3. Usage
Schedule Tasks:
Execute a task after a specific duration:
go
Copy code
func MyFunc(arg1, arg2 string) { fmt.Println(arg1, arg2) } taskID := s.RunAfter(5*time.Second, MyFunc, "Hello", "World")
Execute a task at a particular time:
go
Copy code
taskID := s.RunAt(time.Now().Add(24*time.Hour), MyFunc, "Hello", "World")
Execute recurring tasks:
go
Copy code
taskID := s.RunEvery(1*time.Minute, MyFunc, "Hello", "World")
Custom Storage:
Implement the TaskStore
interface if a custom storage solution is needed:
go
Copy code
type TaskStore interface { Store(task *TaskAttributes) error Remove(task *TaskAttributes) error Fetch() ([]TaskAttributes, error) }
Task Attributes Structure:
go
Copy code
type TaskAttributes struct { Hash string Name string LastRun string NextRun string Duration string IsRecurring string Params string }
4. Examples
Check the Examples
folder in the repository for sample implementations:
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.