From 88f4652bd9d42181b3db12a779ab8eac594bfd45 Mon Sep 17 00:00:00 2001 From: truongtop1 Date: Fri, 5 Jul 2024 00:46:26 +0700 Subject: [PATCH] feat: add cron package --- cron/README.md | 4 --- cron/utilities.go | 49 +++++++++++++++++++++++++++++++ cron/utilities_test.go | 61 +++++++++++++++++++++++++++++++++++++++ samples/cron/utilities.go | 16 ++++++++++ 4 files changed, 126 insertions(+), 4 deletions(-) delete mode 100644 cron/README.md create mode 100644 cron/utilities.go create mode 100644 cron/utilities_test.go create mode 100644 samples/cron/utilities.go diff --git a/cron/README.md b/cron/README.md deleted file mode 100644 index 3feca1a..0000000 --- a/cron/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Cron module - -- It is a cron module that provides some useful tools or action scripts for the application -- Currently, it's also in maintenance mode. New features will be added soon diff --git a/cron/utilities.go b/cron/utilities.go new file mode 100644 index 0000000..5f50af3 --- /dev/null +++ b/cron/utilities.go @@ -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() +} \ No newline at end of file diff --git a/cron/utilities_test.go b/cron/utilities_test.go new file mode 100644 index 0000000..7d414d3 --- /dev/null +++ b/cron/utilities_test.go @@ -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") + } +} diff --git a/samples/cron/utilities.go b/samples/cron/utilities.go new file mode 100644 index 0000000..700af1c --- /dev/null +++ b/samples/cron/utilities.go @@ -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.") +} \ No newline at end of file