Skip to content

Commit

Permalink
add cron to dcron and splite e2e test cases from other test cases. (l…
Browse files Browse the repository at this point in the history
…ibi#81)

* Deps: updated github.com/go-redis/redis/v8 to github.com/redis/go-redis/v9 (libi#73)

* add robfig/cron to dcron (libi#75)

* add NodeID function into dcron

* add cron lib

* fix warning

* logger removal : phares1

* update

* update

* update

* Add comments, add split the E2E test cases and other test cases. (libi#80)

* add example app to readme

* add NodeID function into dcron

* add cron lib

* fix warning

* logger removal : phares1

* update

* update

* update

* update

* update

* update

* update

* update test workflow

* revert 1.22

* update etcd driver

* update

* fix get nodes

* update

* update for fix TAT

* Revert "update etcd driver"

This reverts commit a21ebf7.

* Revert "deps: updated go-redis to v9 (libi#79)"

This reverts commit 0b85b24.

* update

* update

* refact etcd

* Revert "refact etcd"

This reverts commit 049bed1.

* Revert "update"

This reverts commit 9c71fd6.

* update

* refactor etcddriver

* fix error

* update

* Revert "update"

This reverts commit 6cfcfe6.

* Revert "fix error"

This reverts commit 99b2d82.

* Revert "refactor etcddriver"

This reverts commit a576ac3.

* update

* update

* remove comments, and fix

* add comments

* merge e2e test and normal test

* move e2etest to origin path

* add timeout to avoid pipeline timeout

---------

Co-authored-by: Ava Ackerman <withrjp@gmail.com>
Co-authored-by: libi <7769922+libi@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 3, 2024
1 parent 143a944 commit 171374b
Show file tree
Hide file tree
Showing 35 changed files with 3,341 additions and 166 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@ jobs:
name: Test on go ${{ matrix.go_version }}
strategy:
matrix:
go_version: ["1.19", "1.20", "1.21"]
go_version: [
"1.19",
"1.20",
"1.21",
]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Redis Server in GitHub Actions
uses: supercharge/redis-github-action@1.4.0
uses: supercharge/redis-github-action@1.7.0
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}

- name: Test
run: go test -coverprofile=coverage.txt -covermode=atomic -v ./...
run: go test -v -timeout 30m -coverprofile=coverage.txt -covermode=atomic ./...

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
.idea/*
bin/
bin/
coverage.txt
coverage.html
21 changes: 21 additions & 0 deletions cron/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (C) 2012 Rob Figueiredo
All Rights Reserved.

MIT LICENSE

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions cron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# cron

fork from [robfig/cron](github.com/robfig/cron)
97 changes: 97 additions & 0 deletions cron/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cron

import (
"fmt"
"runtime"
"sync"
"time"

"github.com/libi/dcron/dlog"
)

// JobWrapper decorates the given Job with some behavior.
type JobWrapper func(Job) Job

// Chain is a sequence of JobWrappers that decorates submitted jobs with
// cross-cutting behaviors like logging or synchronization.
type Chain struct {
wrappers []JobWrapper
}

// NewChain returns a Chain consisting of the given JobWrappers.
func NewChain(c ...JobWrapper) Chain {
return Chain{c}
}

// Then decorates the given job with all JobWrappers in the chain.
//
// This:
//
// NewChain(m1, m2, m3).Then(job)
//
// is equivalent to:
//
// m1(m2(m3(job)))
func (c Chain) Then(j Job) Job {
for i := range c.wrappers {
j = c.wrappers[len(c.wrappers)-i-1](j)
}
return j
}

// Recover panics in wrapped jobs and log them with the provided logger.
func Recover(logger dlog.Logger) JobWrapper {
return func(j Job) Job {
return FuncJob(func() {
defer func() {
if r := recover(); r != nil {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}
logger.Errorf("panic: stack %v\n%v\n", err, string(buf))
}
}()
j.Run()
})
}
}

// DelayIfStillRunning serializes jobs, delaying subsequent runs until the
// previous one is complete. Jobs running after a delay of more than a minute
// have the delay logged at Info.
func DelayIfStillRunning(logger dlog.Logger) JobWrapper {
return func(j Job) Job {
var mu sync.Mutex
return FuncJob(func() {
start := time.Now()
mu.Lock()
defer mu.Unlock()
if dur := time.Since(start); dur > time.Minute {
logger.Infof("delay duration=%v", dur)
}
j.Run()
})
}
}

// SkipIfStillRunning skips an invocation of the Job if a previous invocation is
// still running. It logs skips to the given logger at Info level.
func SkipIfStillRunning(logger dlog.Logger) JobWrapper {
return func(j Job) Job {
var ch = make(chan struct{}, 1)
ch <- struct{}{}
return FuncJob(func() {
select {
case v := <-ch:
defer func() { ch <- v }()
j.Run()
default:
logger.Infof("skip")
}
})
}
}
Loading

0 comments on commit 171374b

Please sign in to comment.