Skip to content

Commit

Permalink
refactor: improve module path
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Nov 26, 2023
1 parent f1aa044 commit 85343cd
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 157 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Scheduler
[![Go Reference](https://pkg.go.dev/badge/github.com/Ja7ad/Scheduler.svg)](https://pkg.go.dev/github.com/Ja7ad/Scheduler)
[![Go Reference](https://pkg.go.dev/badge/github.com/Ja7ad/scheduler.svg)](https://pkg.go.dev/github.com/Ja7ad/scheduler)

[Scheduler](https://pkg.go.dev/github.com/Ja7ad/Scheduler) package is a zero-dependency scheduling library for Go
[Scheduler](https://pkg.go.dev/github.com/Ja7ad/scheduler) package is a zero-dependency scheduling library for Go

# Install
```console
go get -u github.com/Ja7ad/Scheduler
go get -u github.com/Ja7ad/scheduler
```

# Features
Expand All @@ -23,7 +23,7 @@ package main

import (
"fmt"
"github.com/Ja7ad/Scheduler"
"github.com/Ja7ad/scheduler"
)

var (
Expand Down
14 changes: 6 additions & 8 deletions _example/multiple/jobs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ package main

import (
"fmt"
"github.com/Ja7ad/Scheduler"
)

var (
Sched = Scheduler.NewScheduler()
"github.com/Ja7ad/scheduler"
)

func main() {
if err := Sched.Every(5).Second().Do(Greeting); err != nil {
sched := scheduler.New()

if err := sched.Every(5).Second().Do(Greeting); err != nil {
panic(err)
}
if err := Sched.Every(7).Second().Do(Name, "Javad"); err != nil {
if err := sched.Every(7).Second().Do(Name, "Javad"); err != nil {
panic(err)
}
<-Sched.Start()
<-sched.Start()
}

func Greeting() {
Expand Down
19 changes: 9 additions & 10 deletions _example/multiple/scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package main

import (
"fmt"
"github.com/Ja7ad/Scheduler"
)

var (
Sched1 = Scheduler.NewScheduler()
Sched2 = Scheduler.NewScheduler()
"github.com/Ja7ad/scheduler"
)

func main() {
Expand All @@ -16,17 +11,21 @@ func main() {
}

func sched1() {
if err := Sched1.Every(5).Second().Do(Greeting); err != nil {
sched := scheduler.New()

if err := sched.Every(5).Second().Do(Greeting); err != nil {
panic(err)
}
<-Sched1.Start()
<-sched.Start()
}

func sched2() {
if err := Sched2.Every(10).Second().Do(Name, "Javad"); err != nil {
sched := scheduler.New()

if err := sched.Every(10).Second().Do(Name, "Javad"); err != nil {
panic(err)
}
<-Sched2.Start()
<-sched.Start()
}

func Greeting() {
Expand Down
12 changes: 5 additions & 7 deletions _example/sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package main

import (
"fmt"
"github.com/Ja7ad/Scheduler"
)

var (
Sched = Scheduler.NewScheduler()
"github.com/Ja7ad/scheduler"
)

func main() {
if err := Sched.Every(5).Second().Do(Greeting); err != nil {
sched := scheduler.New()

if err := sched.Every(5).Second().Do(Greeting); err != nil {
panic(err)
}

<-Sched.Start()
<-sched.Start()
}

func Greeting() {
Expand Down
4 changes: 2 additions & 2 deletions schedErrors/errors.go → errs/errors.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package schedErrors
package errs

import "errors"

var (
ERROR_TIME_FORMAT = errors.New("schedErrors in time format")
ERROR_TIME_FORMAT = errors.New("errs in time format")
ERROR_PARAMETER = errors.New("no parameters are adapted")
ERROR_NOT_A_FUNCTION = errors.New("in the job queue, only functions can be scheduled")
ERROR_JOB_PREIOD = errors.New("no job period specified")
Expand Down
17 changes: 17 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package scheduler

import "fmt"

func ExampleNew() {
greetingFunc := func() {
fmt.Println("Hello, World!")
}

sched := New()

if err := sched.Every(5).Second().Do(greetingFunc); err != nil {
panic(err)
}

<-sched.Start()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Ja7ad/Scheduler
module github.com/Ja7ad/scheduler

go 1.17

Expand Down
8 changes: 4 additions & 4 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package helper
import (
"crypto/sha256"
"fmt"
"github.com/Ja7ad/Scheduler/schedErrors"
"github.com/Ja7ad/scheduler/errs"
"reflect"
"runtime"
"strconv"
Expand All @@ -27,7 +27,7 @@ func GetFunctionName(functionName interface{}) string {
func CallJobFuncWithParams(jobFunc interface{}, params []interface{}) ([]reflect.Value, error) {
f := reflect.ValueOf(jobFunc)
if len(params) != f.Type().NumIn() {
return nil, schedErrors.ERROR_PARAMETER
return nil, errs.ERROR_PARAMETER
}
in := make([]reflect.Value, len(params))
for k, param := range params {
Expand All @@ -40,7 +40,7 @@ func CallJobFuncWithParams(jobFunc interface{}, params []interface{}) ([]reflect
func TimeFormat(time string) (h, m, s int, err error) {
timeSplit := strings.Split(time, ":")
if len(timeSplit) < 2 || len(timeSplit) > 3 {
return 0, 0, 0, schedErrors.ERROR_TIME_FORMAT
return 0, 0, 0, errs.ERROR_TIME_FORMAT
}

if h, err = strconv.Atoi(timeSplit[0]); err != nil {
Expand All @@ -58,7 +58,7 @@ func TimeFormat(time string) (h, m, s int, err error) {
}

if h < 0 || h > 23 || m < 0 || m > 59 || s < 0 || s > 59 {
return 0, 0, 0, schedErrors.ERROR_TIME_FORMAT
return 0, 0, 0, errs.ERROR_TIME_FORMAT
}

return h, m, s, nil
Expand Down
52 changes: 26 additions & 26 deletions job/job.go → job.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package job
package scheduler

import (
"fmt"
"github.com/Ja7ad/Scheduler/global"
"github.com/Ja7ad/Scheduler/helper"
"github.com/Ja7ad/Scheduler/schedErrors"
"github.com/Ja7ad/scheduler/errs"
"github.com/Ja7ad/scheduler/helper"
"github.com/Ja7ad/scheduler/types"
"log"
"reflect"
"time"
)

var (
JobLocker Locker
jobLocker Locker
)

type Locker interface {
Expand All @@ -25,7 +25,7 @@ type Job struct {
Functions map[string]interface{}
FuncParams map[string][]interface{}
Interval uint64
JobUnit global.TimeUnit
JobUnit types.TimeUnit
Tags []string
AtTime time.Duration
TimeLocation *time.Location
Expand All @@ -40,7 +40,7 @@ type Job struct {
func NewJob(interval uint64) *Job {
return &Job{
Interval: interval,
TimeLocation: global.TimeZone,
TimeLocation: types.TimeZone,
LastRun: time.Unix(0, 0),
NextRun: time.Unix(0, 0),
FirstWeekDay: time.Sunday,
Expand All @@ -53,16 +53,16 @@ func NewJob(interval uint64) *Job {
// Run the job and reschedule it
func (j *Job) Run() ([]reflect.Value, error) {
if j.LockJob {
if JobLocker == nil {
return nil, fmt.Errorf("%v %v", schedErrors.ERROR_TRY_LOCK_JOB, j.JobFunction)
if jobLocker == nil {
return nil, fmt.Errorf("%v %v", errs.ERROR_TRY_LOCK_JOB, j.JobFunction)
}

hashedKey := helper.GetFunctionHashedKey(j.JobFunction)
if _, err := JobLocker.Lock(hashedKey); err != nil {
return nil, fmt.Errorf("%v %v", schedErrors.ERROR_TRY_LOCK_JOB, j.JobFunction)
if _, err := jobLocker.Lock(hashedKey); err != nil {
return nil, fmt.Errorf("%v %v", errs.ERROR_TRY_LOCK_JOB, j.JobFunction)
}

defer JobLocker.Unlock(hashedKey)
defer jobLocker.Unlock(hashedKey)
}
result, err := helper.CallJobFuncWithParams(j.Functions[j.JobFunction], j.FuncParams[j.JobFunction])
if err != nil {
Expand All @@ -80,7 +80,7 @@ func (j *Job) Do(jobFunction interface{}, params ...interface{}) error {

jobType := reflect.TypeOf(jobFunction)
if jobType.Kind() != reflect.Func {
return schedErrors.ERROR_NOT_A_FUNCTION
return errs.ERROR_NOT_A_FUNCTION
}

funcName := helper.GetFunctionName(jobFunction)
Expand All @@ -103,7 +103,7 @@ func (j *Job) DoJobSafely(jobFunction interface{}, params ...interface{}) error
recoveryFunction := func() {
defer func() {
if r := recover(); r != nil {
log.Printf("Internal Panic: %v", r)
log.Printf("internal panic happen: %v", r)
}
}()
_, _ = helper.CallJobFuncWithParams(jobFunction, params)
Expand All @@ -112,12 +112,13 @@ func (j *Job) DoJobSafely(jobFunction interface{}, params ...interface{}) error
}

// At schedules the job to run at the given time
//
// s.Every(1).Day().At("20:30:01").Do(task)
// s.Every(1).Monday().At("20:30:01").Do(task)
func (j *Job) At(t string) *Job {
h, m, s, err := helper.TimeFormat(t)
if err != nil {
j.JobError = schedErrors.ERROR_TIME_FORMAT
j.JobError = errs.ERROR_TIME_FORMAT
return j
}
j.AtTime = time.Duration(h)*time.Hour + time.Duration(m)*time.Minute + time.Duration(s)*time.Second
Expand All @@ -137,20 +138,19 @@ func (j *Job) NextJobRun() error {
}

switch j.JobUnit {
case global.Seconds, global.Minutes, global.Hours:
case types.Seconds, types.Minutes, types.Hours:
j.NextRun = j.LastRun.Add(periodDuration)
case global.Days:
case types.Days:
j.NextRun = j.RoundToMidNight(j.LastRun)
j.NextRun = j.NextRun.Add(j.AtTime)
case global.Weeks:
case types.Weeks:
j.NextRun = j.RoundToMidNight(j.LastRun)
dayDiff := int(j.FirstWeekDay)
dayDiff -= int(j.NextRun.Weekday())
if dayDiff != 0 {
j.NextRun = j.NextRun.Add(time.Duration(dayDiff) * 24 * time.Hour)
}
j.NextRun = j.NextRun.Add(j.AtTime)
// TODO: Add support for months
}

// next possible schedule advance
Expand All @@ -166,20 +166,20 @@ func (j *Job) PeriodDuration() (time.Duration, error) {
var periodDuration time.Duration

switch j.JobUnit {
case global.Seconds:
case types.Seconds:
periodDuration = interval * time.Second
case global.Minutes:
case types.Minutes:
periodDuration = interval * time.Minute
case global.Hours:
case types.Hours:
periodDuration = interval * time.Hour
case global.Days:
case types.Days:
periodDuration = interval * time.Hour * 24
case global.Weeks:
case types.Weeks:
periodDuration = interval * time.Hour * 24 * 7
case global.Months:
case types.Months:
periodDuration = interval * time.Hour * 24 * 30
default:
return 0, schedErrors.ERROR_JOB_PREIOD
return 0, errs.ERROR_JOB_PREIOD
}
return periodDuration, nil
}
6 changes: 3 additions & 3 deletions job/job.partial.go → job.partial.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package job
package scheduler

import (
"fmt"
"github.com/Ja7ad/Scheduler/global"
"github.com/Ja7ad/scheduler/types"
"time"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func (j *Job) From(t *time.Time) *Job {
}

// SetJobUnit sets the JobUnit
func (j *Job) SetJobUnit(unit global.TimeUnit) *Job {
func (j *Job) SetJobUnit(unit types.TimeUnit) *Job {
j.JobUnit = unit
return j
}
Expand Down
Loading

0 comments on commit 85343cd

Please sign in to comment.