diff --git a/schedule.go b/schedule.go index 202d38e..7074fd5 100644 --- a/schedule.go +++ b/schedule.go @@ -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 +} \ No newline at end of file diff --git a/scheduler.go b/scheduler.go index 29c1acc..dc6dec5 100644 --- a/scheduler.go +++ b/scheduler.go @@ -1,7 +1,6 @@ package sched import ( - "fmt" "sync" ) @@ -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{ @@ -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 @@ -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 ¯\_(ツ)_/¯ @@ -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 @@ -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 +} \ No newline at end of file