Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetSchedule/GetSchedules #4

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,13 @@ func negativeToZero(nextRunDuration time.Duration) time.Duration {
}
return nextRunDuration
}

// State Returns the current State of the Schedule
func (s *Schedule) State() State {
return s.state
}

// ID Returns the Schedule ID
func (s *Schedule) ID() string {
return s.id
}
46 changes: 42 additions & 4 deletions scheduler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sched

import (
"fmt"
"sync"
)

Expand All @@ -13,6 +12,27 @@ type Scheduler struct {
mx sync.RWMutex
}

//ErrorScheduleNotFound Error When we can't find a Schedule
type ErrorScheduleNotFound struct {
Message string
}

func (e ErrorScheduleNotFound) Error() string {
return e.Message
}

//ErrorScheduleNotFound Error When we can't find a Schedule
type ErrorScheduleExists struct {
Message string
}

func (e ErrorScheduleExists) Error() string {
return e.Message
}




//NewScheduler Creates new Scheduler, opt Options are applied to *every* schedule added and created by this scheduler.
func NewScheduler(opts ...Option) *Scheduler {
return &Scheduler{
Expand All @@ -27,7 +47,7 @@ func (s *Scheduler) Add(id string, timer Timer, job func(), extraOpts ...Option)
defer s.mx.Unlock()

if _, ok := s.schedules[id]; ok {
return fmt.Errorf("job with this id already exists")
return ErrorScheduleExists{"job with this id already exists"}
}

// Create schedule
Expand All @@ -47,7 +67,7 @@ func (s *Scheduler) Start(id string) error {
// Find Schedule by id
schedule, found := s.schedules[id]
if !found {
return fmt.Errorf("schdule with this id does not exit")
return ErrorScheduleExists{"job with this id already exists"}
}

// Start it ¯\_(ツ)_/¯
Expand All @@ -71,7 +91,7 @@ func (s *Scheduler) Stop(id string) error {
defer s.mx.Unlock()
schedule, found := s.schedules[id]
if !found {
return fmt.Errorf("schdule with this id does not exit")
return ErrorScheduleExists{"job with this id already exists"}
}
schedule.Stop()
return nil
Expand All @@ -91,3 +111,21 @@ func (s *Scheduler) StopAll() {
}
wg.Wait()
}

//GetSchedule Returns a Schedule by ID from the Scheduler
func (s *Scheduler) GetSchedule(id string) (*Schedule, error) {
s.mx.Lock()
defer s.mx.Unlock()
j, ok := s.schedules[id];
if !ok {
return nil, ErrorScheduleNotFound{"Schedule Not Found"}
}
return j, nil
}

//GetAllSchedules Returns all Schedule's in the Scheduler
func (s *Scheduler) GetAllSchedules() (map[string]*Schedule, error) {
s.mx.Lock()
defer s.mx.Unlock()
return s.schedules, nil
}