Skip to content

Commit

Permalink
feat: add convenient way to execute a func
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenzou committed Sep 28, 2024
1 parent 19e0dbd commit 9415df5
Show file tree
Hide file tree
Showing 10 changed files with 925 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: stable
stable: true
cache: true

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: stable
stable: true
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# options for analysis running
run:
go: '1.18'
go: '1.21'
# default concurrency is a available CPU number
concurrency: 4

Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exclude: '.*'
repos:
- repo: 'https://github.com/golangci/golangci-lint'
rev: v1.54.1
rev: v1.61.0
hooks:
- id: golangci-lint
entry: golangci-lint run -vvv
Expand Down
30 changes: 24 additions & 6 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ func (c CallableFunc[T]) Call(ctx context.Context) (T, error) {
}

type Executor interface {
// Execute execute a task in background
// Will return ErrShutdown if shutdown already
// Will return ErrRejectedExecution if task out of cap
// Execute execute a task in background.
// Will return ErrShutdown if shutdown already.
// Will return ErrRejectedExecution if task out of cap.
Execute(Runnable) error

// ExecuteFunc execute a func in background.
// Will return ErrShutdown if shutdown already.
// Will return ErrRejectedExecution if task out of cap.
ExecuteFunc(fn func(ctx context.Context)) error

// Shutdown shutdown the executor
// Will wait the queued task to be finish
Shutdown(ctx context.Context) error
Expand All @@ -58,8 +63,11 @@ type Executor interface {
type ExecutorService[T any] interface {
Executor

// Submit execute a task with result async, and can get the task result via get
// Submit execute a task with result async, and can get the task result via get.
Submit(callable Callable[T]) (Future[T], error)

// SubmitFunc execute a func with result async, and can get the task result via get.
SubmitFunc(fn func(ctx context.Context) (T, error)) (Future[T], error)
}

type CRONRule struct {
Expand All @@ -72,12 +80,22 @@ type CRONRule struct {

type ScheduledExecutor interface {
Executor
// Schedule run a one time task after delay duration

// Schedule run a one time task after delay duration.
Schedule(r Runnable, delay time.Duration) (CancelFunc, error)

// ScheduleAtFixRate schedule a periodic task in fixed rate
// ScheduleFunc run a one time func after delay duration.
ScheduleFunc(fn func(ctx context.Context), delay time.Duration) (CancelFunc, error)

// ScheduleAtFixRate schedule a periodic task in fixed rate from now.
ScheduleAtFixRate(r Runnable, period time.Duration) (CancelFunc, error)

// ScheduleFuncAtFixRate schedule a periodic func in fixed rate from now.
ScheduleFuncAtFixRate(fn func(ctx context.Context), delay time.Duration) (CancelFunc, error)

// ScheduleAtCronRate schedule at periodic cron task
ScheduleAtCronRate(r Runnable, rule CRONRule) (CancelFunc, error)

// ScheduleFuncAtCronRate schedule at periodic cron func.
ScheduleFuncAtCronRate(fn func(ctx context.Context), rule CRONRule) (CancelFunc, error)
}
20 changes: 13 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
module github.com/zhenzou/executors

go 1.21
go 1.23.0

require (
github.com/aptible/supercronic v0.2.26
github.com/dubbogo/timer v0.0.0-20210901143602-5dacc5984f43
github.com/panjf2000/ants/v2 v2.8.1
github.com/stretchr/testify v1.8.1
github.com/aptible/supercronic v0.2.32
github.com/dubbogo/timer v0.1.0
github.com/panjf2000/ants/v2 v2.10.0
github.com/stretchr/testify v1.9.0
github.com/zyedidia/generic v1.2.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dubbogo/gost v1.14.0 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/fasthash v1.0.3 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/exp v0.0.0-20220218215828-6cf2b201936e // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9415df5

Please sign in to comment.