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

feat: EntryID and cron.nextID renaming #12

Merged
merged 3 commits into from
Nov 6, 2023
Merged
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
29 changes: 15 additions & 14 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ type Cron struct {
chain Chain
stop chan struct{}
add chan *Entry
remove chan EntryID
remove chan ID
snapshot chan chan []Entry
running bool
logger Logger
runningMu sync.Mutex
location *time.Location
parser ScheduleParser
nextID EntryID
next ID
jobWaiter sync.WaitGroup
}

Expand All @@ -43,14 +43,14 @@ type Schedule interface {
Next(time.Time) time.Time
}

// EntryID identifies an entry within a Cron instance
type EntryID int
// ID identifies an entry within a Cron instance
type ID int

// Entry consists of a schedule and the func to execute on that schedule.
type Entry struct {
// ID is the cron-assigned ID of this entry, which may be used to look up a
// snapshot or remove it.
ID EntryID
ID ID

// Schedule on which this job should be run.
Schedule Schedule
Expand Down Expand Up @@ -117,12 +117,13 @@ func New(opts ...Option) *Cron {
add: make(chan *Entry),
stop: make(chan struct{}),
snapshot: make(chan chan []Entry),
remove: make(chan EntryID),
remove: make(chan ID),
running: false,
runningMu: sync.Mutex{},
logger: DefaultLogger,
location: time.Local,
parser: standardParser,
next: 1,
}
for _, opt := range opts {
opt(c)
Expand All @@ -138,14 +139,14 @@ func (f FuncJob) Run() { f() }
// AddFunc adds a func to the Cron to be run on the given schedule.
// The spec is parsed using the time zone of this Cron instance as the default.
// An opaque ID is returned that can be used to later remove it.
func (c *Cron) AddFunc(spec string, cmd func()) (EntryID, error) {
func (c *Cron) AddFunc(spec string, cmd func()) (ID, error) {
return c.AddJob(spec, FuncJob(cmd))
}

// AddJob adds a Job to the Cron to be run on the given schedule.
// The spec is parsed using the time zone of this Cron instance as the default.
// An opaque ID is returned that can be used to later remove it.
func (c *Cron) AddJob(spec string, cmd Job) (EntryID, error) {
func (c *Cron) AddJob(spec string, cmd Job) (ID, error) {
schedule, err := c.parser.Parse(spec)
if err != nil {
return 0, err
Expand All @@ -155,16 +156,16 @@ func (c *Cron) AddJob(spec string, cmd Job) (EntryID, error) {

// Schedule adds a Job to the Cron to be run on the given schedule.
// The job is wrapped with the configured Chain.
func (c *Cron) Schedule(schedule Schedule, cmd Job) EntryID {
func (c *Cron) Schedule(schedule Schedule, cmd Job) ID {
c.runningMu.Lock()
defer c.runningMu.Unlock()
c.nextID++
entry := &Entry{
ID: c.nextID,
ID: c.next,
Schedule: schedule,
WrappedJob: c.chain.Then(cmd),
Job: cmd,
}
c.next++
if !c.running {
c.entries = append(c.entries, entry)
} else {
Expand All @@ -191,7 +192,7 @@ func (c *Cron) Location() *time.Location {
}

// Entry returns a snapshot of the given entry, or nil if it couldn't be found.
func (c *Cron) Entry(id EntryID) Entry {
func (c *Cron) Entry(id ID) Entry {
for _, entry := range c.Entries() {
if id == entry.ID {
return entry
Expand All @@ -201,7 +202,7 @@ func (c *Cron) Entry(id EntryID) Entry {
}

// Remove an entry from being run in the future.
func (c *Cron) Remove(id EntryID) {
func (c *Cron) Remove(id ID) {
c.runningMu.Lock()
defer c.runningMu.Unlock()
if c.running {
Expand Down Expand Up @@ -344,7 +345,7 @@ func (c *Cron) entrySnapshot() []Entry {
return entries
}

func (c *Cron) removeEntry(id EntryID) {
func (c *Cron) removeEntry(id ID) {
var entries []*Entry
for _, e := range c.entries {
if e.ID != id {
Expand Down
Loading