Skip to content

Commit

Permalink
Merge pull request #4 from goinsane/develop
Browse files Browse the repository at this point in the history
v0.3.2
  • Loading branch information
orkunkaraduman authored Dec 31, 2023
2 parents 6f55cb0 + 759a216 commit 2b7ded1
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions redcron.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (

// RedCron registers and runs cron jobs.
type RedCron struct {
cfg Config
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
crons map[string]cronProperties
cronsMu sync.Mutex
stopping int32
cfg Config
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
crons map[string]cronProperties
cronsMu sync.Mutex
stopped int32
}

// New creates a new RedCron struct.
Expand All @@ -37,6 +37,7 @@ func New(cfg Config) (c *RedCron) {
}

// Register registers a new cron job by the given parameters. It returns the underlying RedCron.
// If the underlying RedCron is stopped, registered cron jobs won't be triggered.
func (c *RedCron) Register(name string, repeatSec int, offsetSec int, f func(context.Context)) *RedCron {
if name == "" {
panic(errors.New("name must be non-empty"))
Expand All @@ -61,31 +62,32 @@ func (c *RedCron) Register(name string, repeatSec int, offsetSec int, f func(con

// Stop stops triggering cron jobs and waits for all jobs are finished.
// When ctx has been done, all contexts of jobs are cancelled.
// If ctx is nil, all contexts of jobs are cancelled immediately.
func (c *RedCron) Stop(ctx context.Context) {
if !atomic.CompareAndSwapInt32(&c.stopping, 0, 1) {
return
}
atomic.CompareAndSwapInt32(&c.stopped, 0, 1)

stopped := make(chan struct{})
done := make(chan struct{})
go func() {
c.wg.Wait()
close(stopped)
close(done)
}()

select {
case <-ctx.Done():
case <-stopped:
if ctx != nil {
select {
case <-ctx.Done():
case <-done:
}
}

c.cancel()
<-stopped
<-done
}

func (c *RedCron) run(cp cronProperties, f func(context.Context)) {
c.wg.Add(1)
defer c.wg.Done()

for c.ctx.Err() == nil && c.stopping == 0 {
for c.ctx.Err() == nil && c.stopped == 0 {
var tm time.Time
select {
case <-c.ctx.Done():
Expand Down

0 comments on commit 2b7ded1

Please sign in to comment.