Skip to content

Commit

Permalink
feat(tasks): tasks can have IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
ConsoleTVs committed Aug 4, 2024
1 parent a4ce0a3 commit e4fecc2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 433 deletions.
10 changes: 10 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ package yotei

import "context"

// Handler determines an interface of something
// that can be handled.
type Handler interface {
// Handle is the callback to execute the task action.
Handle(context.Context) Action
}

// HandlerFunc is a simple type to quickly transform a func
// into a [Handler].
type HandlerFunc func(context.Context) Action

// Hande runs the action
func (handler HandlerFunc) Handle(ctx context.Context) Action {
return handler(ctx)
}
7 changes: 7 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ func (scheduler *Scheduler) IsRunning() bool {
return scheduler.ctx != nil
}

func (scheduler *Scheduler) FindTaskByID(id string) (*Task, bool) {
scheduler.mutex.Lock()
defer scheduler.mutex.Unlock()

return scheduler.tasks.FindByID(id)
}

// String returns a string representation of a scheduler.
func (scheduler *Scheduler) String() string {
scheduler.mutex.Lock()
Expand Down
33 changes: 26 additions & 7 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ import (
"time"
)

// Handler determines an interface of something
// that can be handled.
type Handler interface {
// Handle is the callback to execute the task action.
Handle(context.Context) Action
}

// Task is the the executionable action in the [Scheduler].
//
// A task must:
// - Be handlable
// - Have a weight
// - Have a duration
// - Be sequential or concurrent
// - Have an id
//
// Use [NewTask] to create a new task.
type Task struct {
Expand All @@ -29,6 +23,7 @@ type Task struct {
duration atomic.Int64
locked atomic.Bool
concurrent atomic.Bool
id atomic.Value
}

// A list of actionable tasks
Expand Down Expand Up @@ -59,6 +54,16 @@ func NewTask(handler Handler) *Task {
return task
}

func (task *Task) Idenfified(id string) *Task {
task.id.Store(id)

return task
}

func (task *Task) ID() string {
return task.id.Load().(string)
}

func (task *Task) Lock() {
task.locked.Store(true)
}
Expand Down Expand Up @@ -102,6 +107,10 @@ func (task *Task) Weight() uint64 {
}

func (task *Task) Handle(ctx context.Context) Action {
if task.handler == nil {
panic("no task handler defined. please ensure the task handler is not nil")
}

return task.handler.Handle(ctx)
}

Expand Down Expand Up @@ -153,3 +162,13 @@ func (tasks Tasks) Locked() Tasks {

return locked
}

func (tasks Tasks) FindByID(id string) (*Task, bool) {
for _, task := range tasks {
if task.ID() == id {
return task, true
}
}

return nil, false
}
260 changes: 0 additions & 260 deletions yotei.go

This file was deleted.

Loading

0 comments on commit e4fecc2

Please sign in to comment.