-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
255 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/gorhill/cronexpr" | ||
"time" | ||
) | ||
|
||
type CronTrigger struct { | ||
expression *cronexpr.Expression | ||
} | ||
|
||
func (t CronTrigger) GetNextFireTime(task Task) *time.Time { | ||
var nextFireTime time.Time | ||
nextFireTimes := t.expression.NextN(time.Now(), 2) | ||
if task.LastFireTime != nil && task.LastFireTime.Equal(nextFireTimes[0]) { | ||
// this can happen if the task executes faster than the tick rate, in which case, schedule the task for the next scheduled time | ||
nextFireTime = nextFireTimes[1] | ||
} else { | ||
nextFireTime = nextFireTimes[0] | ||
} | ||
return &nextFireTime | ||
} | ||
|
||
func (t CronTrigger) IsRecurring() bool { | ||
return true | ||
} | ||
|
||
func NewCronTrigger(cronExpression string) (*CronTrigger, error) { | ||
expression, err := cronexpr.Parse(cronExpression) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &CronTrigger{expression: expression}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
package pkg | ||
|
||
import "time" | ||
import ( | ||
"time" | ||
) | ||
|
||
type TriggerInterface interface { | ||
GetNextFireTime() *time.Time | ||
} | ||
type ExecuteOnceTrigger struct { | ||
FireAt time.Time | ||
FireAt time.Time `json:"fire_at"` | ||
} | ||
|
||
func (t ExecuteOnceTrigger) GetNextFireTime() *time.Time { | ||
func (t ExecuteOnceTrigger) GetNextFireTime(task Task) *time.Time { | ||
return &t.FireAt | ||
} | ||
|
||
func (t ExecuteOnceTrigger) IsRecurring() bool { | ||
return false | ||
} | ||
|
||
func NewExecuteOnceTrigger(fireAt time.Time) *ExecuteOnceTrigger { | ||
return &ExecuteOnceTrigger{FireAt: fireAt} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package pkg | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type TriggerInterface interface { | ||
GetNextFireTime(task Task) *time.Time | ||
IsRecurring() bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.