Skip to content

Commit

Permalink
feat: add slog as a logger
Browse files Browse the repository at this point in the history
  • Loading branch information
hum committed Jan 15, 2024
1 parent 628db30 commit a335c78
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
18 changes: 5 additions & 13 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"sync"
"time"

"log/slog"

"github.com/google/uuid"
)

// Scheduler is the high-level structure to manage the in-memory tasks. Do not create the structure manually. Always use `scheduled.NewScheduler()`
type Scheduler struct {
tasks map[uuid.UUID]*task
taskLock sync.Mutex
Expand Down Expand Up @@ -63,11 +66,7 @@ func (s *Scheduler) Stop() {
for taskid := range s.tasks {
err := s.RemoveTask(taskid)
if err != nil {
// @TODO: Never print stuff to a console directly
// A) Return as a slice of errors to the caller
// B) Expose a logging interface for the caller to have a control over
// C) Ignore (?)
fmt.Printf("Err: Stop() could not remove task from the scheduler, got err=%s", err)
Logger.Error("could not remove task from the scheduler, got", "err", err)

Check failure on line 69 in scheduler.go

View workflow job for this annotation

GitHub Actions / build

undefined: Logger
continue
}
}
Expand All @@ -90,17 +89,11 @@ func (s *Scheduler) registerTask(t *task, runOnce bool) error {

// execTask is the entrypoint for the execution of the task. It only accepts a task's identifier.
// Tasks are ran in goroutines, which belong to each task respectively.
//
// @TODO: Should exec accept a task pointer to decouple it from the internal array buffer of tasks?
func (s *Scheduler) execTask(task *task, runOnce bool) {
go func() {
time.AfterFunc(time.Until(task.StartTime), func() {
if err := task.ctx.Err(); err != nil {
// @TODO: Never print stuff to a console directly
// A) Return as a slice of errors to the caller
// B) Expose a logging interface for the caller to have a control over
// C) Ignore (?)
fmt.Printf("err: task=%s is cancelled but wanted to be ran\n", task.ID)
Logger.Error("cancelled task cannot execute", slog.String("task", task.ID.String()))

Check failure on line 96 in scheduler.go

View workflow job for this annotation

GitHub Actions / build

undefined: Logger

// Make sure to also stop the tick timer
if task.timer != nil {
Expand All @@ -127,7 +120,6 @@ func (s *Scheduler) execTask(task *task, runOnce bool) {
// Check if the task expired by reaching the maximum
if task.MaxIter > 0 {
if task.MaxIter == task.itercnt {
fmt.Println("task reached max iter")
s.RemoveTask(task.ID)
return
}
Expand Down
6 changes: 0 additions & 6 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,8 @@ func NewTask(opts TaskOpts) *task {
func (t *task) run() {
if err := t.Fn(); err != nil {
if t.ErrFn != nil {
// While it may not be pretty, it works.
// Dereferences the pointer into a function type only when ptr != nil
t.ErrFn(err)
return
}
// @TODO: what is the correct behaviour here?
fmt.Println("warn: task.Fn returned an error without t.ErrFn set")
return
}
}

Expand Down

0 comments on commit a335c78

Please sign in to comment.