Skip to content

Commit

Permalink
feat: add cron package
Browse files Browse the repository at this point in the history
  • Loading branch information
thuongtruong109 committed Jul 4, 2024
1 parent 4dfda47 commit 88f4652
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
4 changes: 0 additions & 4 deletions cron/README.md

This file was deleted.

49 changes: 49 additions & 0 deletions cron/utilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cron

import "time"

type ICronJob struct {
duration time.Duration
stopAfter time.Duration
callback func()
stopChan chan bool
}

func NewCronJob(duration, stopAfter time.Duration, callback func()) *ICronJob {
return &ICronJob{
duration: duration,
stopAfter: stopAfter,
callback: callback,
stopChan: make(chan bool),
}
}

func (cj *ICronJob) Start() {
go func() {
ticker := time.NewTicker(cj.duration)
stopTimer := time.NewTimer(cj.stopAfter)

defer ticker.Stop()
defer stopTimer.Stop()

for {
select {
case <-ticker.C:
cj.callback()
case <-stopTimer.C:
cj.stopChan <- true
return
}
}
}()
}

func (cj *ICronJob) Wait() {
<-cj.stopChan
}

func Run(duration, stopAfter uint64, callback func()) {
cronJob := NewCronJob(time.Duration(duration)*time.Second, time.Duration(stopAfter)*time.Second, callback)
cronJob.Start()
cronJob.Wait()
}
61 changes: 61 additions & 0 deletions cron/utilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cron

import (
"testing"
"time"
)

func TestNewCronJob(t *testing.T) {
duration := time.Second * 1
stopAfter := time.Second * 5
callback := func() {}

cronJob := NewCronJob(duration, stopAfter, callback)

if cronJob.duration != duration {
t.Errorf("Expected duration %v, but got %v", duration, cronJob.duration)
}
if cronJob.stopAfter != stopAfter {
t.Errorf("Expected stopAfter %v, but got %v", stopAfter, cronJob.stopAfter)
}
if cronJob.callback == nil {
t.Error("Expected callback to be set, but it was nil")
}
if cronJob.stopChan == nil {
t.Error("Expected stopChan to be set, but it was nil")
}
}

func TestCronJobStartAndWait(t *testing.T) {
duration := time.Millisecond * 10
stopAfter := time.Millisecond * 50
callbackCalled := false

callback := func() {
callbackCalled = true
}

cronJob := NewCronJob(duration, stopAfter, callback)
cronJob.Start()
cronJob.Wait()

if !callbackCalled {
t.Error("Expected callback to be called, but it wasn't")
}
}

func TestRun(t *testing.T) {
duration := uint64(1)
stopAfter := uint64(2)
callbackCalled := false

callback := func() {
callbackCalled = true
}

Run(duration, stopAfter, callback)

if !callbackCalled {
t.Error("Expected callback to be called, but it wasn't")
}
}
16 changes: 16 additions & 0 deletions samples/cron/utilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cron

import (
"github.com/thuongtruong109/gouse/cron"
"fmt"
)

func SampleCronRun() {
callback := func() {
fmt.Println("Cron job executed")
}

cron.Run(1, 5, callback)

fmt.Println("Cron job stopped.")
}

0 comments on commit 88f4652

Please sign in to comment.