From c725359528c9845e00f4399796699d7fd76325a4 Mon Sep 17 00:00:00 2001 From: googs1025 Date: Sat, 6 Jan 2024 11:42:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=A0=91=E7=8A=B6?= =?UTF-8?q?=E6=A8=A1=E7=89=88=E4=BB=BB=E5=8A=A1=E5=BC=95=E6=93=8E=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 4 +- .gitignore | 2 +- README-zh.md | 4 +- README.md | 4 +- cron-task-mode/cronfunc/cron_test.go | 2 +- event-mode/broadcaster/broadcaseter.go | 2 +- event-mode/broadcaster/watcher.go | 2 +- event-mode/event_broadcaster.go | 4 +- event-mode/event_broadcaster_test.go | 12 +- exit-gracefully-mode/exit_gracefully1_test.go | 8 +- forever-mode/forever_test.go | 6 +- forever-mode/forever_time_test.go | 6 +- go.mod | 4 +- go.sum | 4 +- gorountine-other-mode/group/group_test.go | 8 +- .../app/controllermanager.go | 4 +- .../app/my_controller.go | 4 +- .../app/options/options.go | 2 +- .../controller-manager.go | 2 +- .../controller/mycontroller1/controller.go | 2 +- .../controller/mycontroller2/controller.go | 2 +- kube-controller-mode/controller.go | 2 +- kubelet-podworker-mode/kubelet.go | 2 +- kubelet-podworker-mode/kubelet/kubelet.go | 2 +- .../kubelet/kubelet_test.go | 6 +- kubelet-podworker-mode/kubelet/pod.go | 2 +- kubelet-podworker-mode/kubelet/worker.go | 2 +- .../scheduler-with-plugins/demo/plugin.go | 4 +- .../scheduler-with-plugins/demo/pod.go | 2 +- .../interface/interface.go | 2 +- .../scheduler-with-plugins/queue.go | 2 +- .../scheduler-with-plugins/scheduler.go | 6 +- .../scheduler_with_plugin_test.go | 8 +- task-job-mode/common/constants/task.go | 2 +- task-job-mode/plugins/demo/demo.go | 4 +- task-job-mode/plugins/interface.go | 4 +- task-job-mode/pool.go | 2 +- task-job-mode/queue.go | 2 +- task-job-mode/worker.go | 6 +- task-job-mode/worker_test.go | 2 +- tree-template-engine/manager.go | 39 +++++++ tree-template-engine/model/task.go | 18 +++ tree-template-engine/task.yaml | 51 +++++++++ tree-template-engine/template_engine.go | 105 ++++++++++++++++++ tree-template-engine/template_engine_test.go | 14 +++ workqueue-mode/delay_queue_test.go | 4 +- 46 files changed, 305 insertions(+), 76 deletions(-) create mode 100644 tree-template-engine/manager.go create mode 100644 tree-template-engine/model/task.go create mode 100644 tree-template-engine/task.yaml create mode 100644 tree-template-engine/template_engine.go create mode 100644 tree-template-engine/template_engine_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1dc4a2f..df1f69c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,5 +25,5 @@ jobs: with: go-version: ${{ env.GO_VERSION }} - - name: Run unit test - run: go test -v ./... \ No newline at end of file + - name: Run unit tree-template-engine + run: go tree-template-engine -v ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5e527ac..a59cb7d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ bin testbin/* -# Test binary, build with `go test -c` +# Test binary, build with `go tree-template-engine -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE diff --git a/README-zh.md b/README-zh.md index a913d07..129f78c 100644 --- a/README-zh.md +++ b/README-zh.md @@ -48,4 +48,6 @@ 22. event模式 [代码示例](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/event-mode) -23. worker-job模式 [代码示例](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/worker-job-mode) \ No newline at end of file +23. worker-job模式 [代码示例](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/worker-job-mode) + +24. tree-template-engine模式 [代码示例](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/tree-template-engine) \ No newline at end of file diff --git a/README.md b/README.md index 02710bd..bf62887 100644 --- a/README.md +++ b/README.md @@ -49,4 +49,6 @@ Concurrency modes that I usually learn (the mode classification is mainly based 22. event mode [code example](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/event-mode) -23. worker-job mode [code example](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/worker-job-mode) \ No newline at end of file +23. worker-job mode [code example](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/worker-job-mode) + +24. tree-template-engine mode [code example](https://github.com/StudyPlace-io/Golang-Concurrency-Pattern-Demo/tree/main/tree-template-engine) \ No newline at end of file diff --git a/cron-task-mode/cronfunc/cron_test.go b/cron-task-mode/cronfunc/cron_test.go index bf49983..2a753ab 100644 --- a/cron-task-mode/cronfunc/cron_test.go +++ b/cron-task-mode/cronfunc/cron_test.go @@ -24,7 +24,7 @@ func CronTry() { }() CronUntil(func() { time.Sleep(100 * time.Millisecond) - log.Println("test") + log.Println("tree-template-engine") }, 100*time.Millisecond, ch) log.Println("main exit") } diff --git a/event-mode/broadcaster/broadcaseter.go b/event-mode/broadcaster/broadcaseter.go index 0f4b300..52ac239 100644 --- a/event-mode/broadcaster/broadcaseter.go +++ b/event-mode/broadcaster/broadcaseter.go @@ -1,7 +1,7 @@ package broadcaster import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/event-mode/event" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/event-mode/event" "sync" ) diff --git a/event-mode/broadcaster/watcher.go b/event-mode/broadcaster/watcher.go index 729a6de..46c7f4a 100644 --- a/event-mode/broadcaster/watcher.go +++ b/event-mode/broadcaster/watcher.go @@ -1,7 +1,7 @@ package broadcaster import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/event-mode/event" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/event-mode/event" ) // Interface 接口 watcher 实现 diff --git a/event-mode/event_broadcaster.go b/event-mode/event_broadcaster.go index 0412af0..bc7fd6e 100644 --- a/event-mode/event_broadcaster.go +++ b/event-mode/event_broadcaster.go @@ -2,8 +2,8 @@ package event_broadcaster import ( "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/event-mode/broadcaster" - "github.com/practice/Golang-Concurrency-Pattern-Demo/event-mode/event" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/event-mode/broadcaster" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/event-mode/event" "time" ) diff --git a/event-mode/event_broadcaster_test.go b/event-mode/event_broadcaster_test.go index 4696642..828452f 100644 --- a/event-mode/event_broadcaster_test.go +++ b/event-mode/event_broadcaster_test.go @@ -1,7 +1,7 @@ package event_broadcaster import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/event-mode/event" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/event-mode/event" "testing" "time" ) @@ -13,16 +13,16 @@ func TestEventBroadcaster(t *testing.T) { go func() { time.Sleep(time.Second * 3) - eventBroadcast.Event(event.Warning, "test", "other-goroutine") + eventBroadcast.Event(event.Warning, "tree-template-engine", "other-goroutine") time.Sleep(time.Second * 3) - eventBroadcast.EventBySource(event.Normal, "test", "other-goroutine", "api-server") + eventBroadcast.EventBySource(event.Normal, "tree-template-engine", "other-goroutine", "api-server") time.Sleep(time.Second * 3) - eventBroadcast.Event(event.Normal, "test", "other-goroutine") + eventBroadcast.Event(event.Normal, "tree-template-engine", "other-goroutine") }() time.Sleep(time.Second * 3) - eventBroadcast.Event(event.Normal, "test", "main-goroutine") - eventBroadcast.EventBySource(event.Normal, "test", "main-goroutine", "api-server") + eventBroadcast.Event(event.Normal, "tree-template-engine", "main-goroutine") + eventBroadcast.EventBySource(event.Normal, "tree-template-engine", "main-goroutine", "api-server") <-time.After(time.Second * 20) eventBroadcast.Stop() diff --git a/exit-gracefully-mode/exit_gracefully1_test.go b/exit-gracefully-mode/exit_gracefully1_test.go index 579ca03..9a84793 100644 --- a/exit-gracefully-mode/exit_gracefully1_test.go +++ b/exit-gracefully-mode/exit_gracefully1_test.go @@ -4,8 +4,8 @@ import ( "context" "flag" "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/exit-gracefully-mode/signals" "github.com/spf13/pflag" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/exit-gracefully-mode/signals" "k8s.io/klog/v2" "log" "net/http" @@ -28,7 +28,7 @@ func NewOptions() *Options { } const ( - DefaultTest = "test" + DefaultTest = "tree-template-engine" DefaultPort = 8080 DefaultHealthPort = 9999 ) @@ -37,7 +37,7 @@ const ( func (o *Options) AddFlags(flags *pflag.FlagSet) { flags.IntVar(&o.Port, "port", DefaultPort, "xxx") flags.IntVar(&o.Port, "healthPort", DefaultHealthPort, "xxx") - flags.StringVar(&o.test, "test", DefaultTest, "xxx") + flags.StringVar(&o.test, "tree-template-engine", DefaultTest, "xxx") flags.IntVar(&o.test2, "test2", 1000, "xxx") flags.BoolVar(&o.test1, "test1", false, "xxx") @@ -56,7 +56,7 @@ func (o *Options) addKlogFlags(flags *pflag.FlagSet) { } func test(w http.ResponseWriter, req *http.Request) { - fmt.Println("test server") + fmt.Println("tree-template-engine server") } // Run 执行 diff --git a/forever-mode/forever_test.go b/forever-mode/forever_test.go index 111e24a..2071649 100644 --- a/forever-mode/forever_test.go +++ b/forever-mode/forever_test.go @@ -10,7 +10,7 @@ import ( func TestRunForever(t *testing.T) { go func() { RunForever(time.Second*5, func() error { - fmt.Println("test") + fmt.Println("tree-template-engine") return nil }) }() @@ -28,7 +28,7 @@ func TestRunWithChannel(t *testing.T) { }() RunWithChannel(time.Second*5, func() error { - fmt.Println("test-channel") + fmt.Println("tree-template-engine-channel") return nil }, stopC) @@ -40,7 +40,7 @@ func TestRunWithContext(t *testing.T) { cancel() }() RunWithContext(time.Second*5, func() error { - fmt.Println("test-context") + fmt.Println("tree-template-engine-context") return nil }, ctx) } diff --git a/forever-mode/forever_time_test.go b/forever-mode/forever_time_test.go index 1e3ef74..2fc196b 100644 --- a/forever-mode/forever_time_test.go +++ b/forever-mode/forever_time_test.go @@ -10,7 +10,7 @@ import ( func TestRunWithTimeForever(t *testing.T) { go func() { RunForeverWithTime(time.Second*1, func() error { - fmt.Println("test-forever-with-time") + fmt.Println("tree-template-engine-forever-with-time") return nil }, 3) }() @@ -28,7 +28,7 @@ func TestRunWithTimeWithChannel(t *testing.T) { }() RunWithTimeWithChannel(time.Second*2, func() error { - fmt.Println("test-with-time-channel") + fmt.Println("tree-template-engine-with-time-channel") return nil }, stopC, 5) @@ -40,7 +40,7 @@ func TestRunWithTimeWithContext(t *testing.T) { cancel() }() RunWithTimeWithContext(time.Second*1, func() error { - fmt.Println("test-with-time-context") + fmt.Println("tree-template-engine-with-time-context") return nil }, ctx, 10) } diff --git a/go.mod b/go.mod index f39b7d4..eb2d53e 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ -module github.com/practice/Golang-Concurrency-Pattern-Demo +module github.com/study-io/Golang-Concurrency-Pattern-Demo go 1.18 require ( github.com/antlabs/timer v0.0.10 + github.com/ghodss/yaml v1.0.0 github.com/google/uuid v1.3.0 github.com/robfig/cron/v3 v3.0.1 - github.com/sirupsen/logrus v1.8.1 github.com/smartystreets/goconvey v1.8.0 github.com/spf13/cobra v1.2.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 5a0854e..ae07d34 100644 --- a/go.sum +++ b/go.sum @@ -117,6 +117,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -356,8 +357,6 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v1.13.1 h1:Ef7KhSmjZcK6AVf9YbJdvPYG9avaF0ZxudX+ThRdWfU= github.com/smartystreets/assertions v1.13.1/go.mod h1:cXr/IwVfSo/RbCSPhoAPv73p3hlSdrBH/b3SdnW/LMY= @@ -560,7 +559,6 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/gorountine-other-mode/group/group_test.go b/gorountine-other-mode/group/group_test.go index 7950879..490e233 100644 --- a/gorountine-other-mode/group/group_test.go +++ b/gorountine-other-mode/group/group_test.go @@ -13,7 +13,7 @@ func TestGroup(t *testing.T) { g.Start(func() { time.Sleep(time.Second * 2) - fmt.Println("test") + fmt.Println("tree-template-engine") }) g.Wait() @@ -25,7 +25,7 @@ func TestGroupN(t *testing.T) { g.StartN(5, func() { time.Sleep(time.Second * 2) - fmt.Println("test") + fmt.Println("tree-template-engine") }) g.Wait() @@ -37,7 +37,7 @@ func TestGroupCtx(t *testing.T) { g.StartWithContext(context.TODO(), func(ctx context.Context) { time.Sleep(time.Second * 2) - fmt.Println("test") + fmt.Println("tree-template-engine") }) g.Wait() @@ -50,7 +50,7 @@ func TestGroupStopC(t *testing.T) { stopC := make(chan struct{}) g.StartWithChannel(stopC, func(stopCh <-chan struct{}) { time.Sleep(time.Second * 2) - fmt.Println("test") + fmt.Println("tree-template-engine") select { case <-stopC: return diff --git a/kube-controller-manager-mode/app/controllermanager.go b/kube-controller-manager-mode/app/controllermanager.go index efc5cd3..a92167e 100644 --- a/kube-controller-manager-mode/app/controllermanager.go +++ b/kube-controller-manager-mode/app/controllermanager.go @@ -3,10 +3,10 @@ package app import ( "context" "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app/config" - "github.com/practice/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app/options" "github.com/spf13/cobra" "github.com/spf13/pflag" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app/config" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app/options" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/component-base/version/verflag" "k8s.io/klog/v2" diff --git a/kube-controller-manager-mode/app/my_controller.go b/kube-controller-manager-mode/app/my_controller.go index 9cd6b5f..acb6e4e 100644 --- a/kube-controller-manager-mode/app/my_controller.go +++ b/kube-controller-manager-mode/app/my_controller.go @@ -3,8 +3,8 @@ package app import ( "context" "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/pkg/controller/mycontroller1" - "github.com/practice/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/pkg/controller/mycontroller2" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/pkg/controller/mycontroller1" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/pkg/controller/mycontroller2" "k8s.io/klog/v2" ) diff --git a/kube-controller-manager-mode/app/options/options.go b/kube-controller-manager-mode/app/options/options.go index 6639a83..1abf859 100644 --- a/kube-controller-manager-mode/app/options/options.go +++ b/kube-controller-manager-mode/app/options/options.go @@ -3,8 +3,8 @@ package options import ( "flag" "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app/config" "github.com/spf13/pflag" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app/config" "k8s.io/klog/v2" "os" "strings" diff --git a/kube-controller-manager-mode/controller-manager.go b/kube-controller-manager-mode/controller-manager.go index 684e8ee..2201b0b 100644 --- a/kube-controller-manager-mode/controller-manager.go +++ b/kube-controller-manager-mode/controller-manager.go @@ -1,7 +1,7 @@ package main import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kube-controller-manager-mode/app" "k8s.io/component-base/cli" _ "k8s.io/component-base/logs/json/register" // for JSON log format registration _ "k8s.io/component-base/metrics/prometheus/restclient" diff --git a/kube-controller-manager-mode/pkg/controller/mycontroller1/controller.go b/kube-controller-manager-mode/pkg/controller/mycontroller1/controller.go index c4caf89..9edd1e1 100644 --- a/kube-controller-manager-mode/pkg/controller/mycontroller1/controller.go +++ b/kube-controller-manager-mode/pkg/controller/mycontroller1/controller.go @@ -3,7 +3,7 @@ package mycontroller1 import ( "context" "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/gorountine-other-mode/group" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/gorountine-other-mode/group" ) // Controller 控制器 diff --git a/kube-controller-manager-mode/pkg/controller/mycontroller2/controller.go b/kube-controller-manager-mode/pkg/controller/mycontroller2/controller.go index 29b2651..215784f 100644 --- a/kube-controller-manager-mode/pkg/controller/mycontroller2/controller.go +++ b/kube-controller-manager-mode/pkg/controller/mycontroller2/controller.go @@ -3,7 +3,7 @@ package mycontroller2 import ( "context" "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/gorountine-other-mode/group" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/gorountine-other-mode/group" ) // Controller 控制器 diff --git a/kube-controller-mode/controller.go b/kube-controller-mode/controller.go index 5565cd5..5b54481 100644 --- a/kube-controller-mode/controller.go +++ b/kube-controller-mode/controller.go @@ -2,7 +2,7 @@ package kube_controller_mode import ( "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/gorountine-other-mode/group" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/gorountine-other-mode/group" ) // Controller 控制器 diff --git a/kubelet-podworker-mode/kubelet.go b/kubelet-podworker-mode/kubelet.go index 4aadd03..f7a293a 100644 --- a/kubelet-podworker-mode/kubelet.go +++ b/kubelet-podworker-mode/kubelet.go @@ -1,7 +1,7 @@ package main import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/app" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/app" "k8s.io/component-base/cli" "os" ) diff --git a/kubelet-podworker-mode/kubelet/kubelet.go b/kubelet-podworker-mode/kubelet/kubelet.go index 1bbc2c3..0c91058 100644 --- a/kubelet-podworker-mode/kubelet/kubelet.go +++ b/kubelet-podworker-mode/kubelet/kubelet.go @@ -2,7 +2,7 @@ package kubelet import ( "context" - "github.com/practice/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/config" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/config" "k8s.io/klog/v2" "time" ) diff --git a/kubelet-podworker-mode/kubelet/kubelet_test.go b/kubelet-podworker-mode/kubelet/kubelet_test.go index 30da97e..117d204 100644 --- a/kubelet-podworker-mode/kubelet/kubelet_test.go +++ b/kubelet-podworker-mode/kubelet/kubelet_test.go @@ -1,8 +1,8 @@ package kubelet import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/config" - "github.com/practice/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/container" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/config" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/container" "testing" "time" ) @@ -11,7 +11,7 @@ func TestKubelet(t *testing.T) { k := &Kubelet{ config: config.NewKubeletConfig(), - node: "test", + node: "tree-template-engine", registerNode: true, registerSchedulable: true, podManager: NewBasicPodManager(), diff --git a/kubelet-podworker-mode/kubelet/pod.go b/kubelet-podworker-mode/kubelet/pod.go index c787a5e..700b136 100644 --- a/kubelet-podworker-mode/kubelet/pod.go +++ b/kubelet-podworker-mode/kubelet/pod.go @@ -1,7 +1,7 @@ package kubelet import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/container" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/container" ) // Pod 模拟Pod对象 diff --git a/kubelet-podworker-mode/kubelet/worker.go b/kubelet-podworker-mode/kubelet/worker.go index 853da3f..f5a5394 100644 --- a/kubelet-podworker-mode/kubelet/worker.go +++ b/kubelet-podworker-mode/kubelet/worker.go @@ -3,7 +3,7 @@ package kubelet import ( "context" "errors" - container2 "github.com/practice/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/container" + container2 "github.com/study-io/Golang-Concurrency-Pattern-Demo/kubelet-podworker-mode/kubelet/container" "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/klog/v2" "sync" diff --git a/scheduler-mode/scheduler-with-plugins/demo/plugin.go b/scheduler-mode/scheduler-with-plugins/demo/plugin.go index 959d5f4..ae4202b 100644 --- a/scheduler-mode/scheduler-with-plugins/demo/plugin.go +++ b/scheduler-mode/scheduler-with-plugins/demo/plugin.go @@ -1,8 +1,8 @@ package demo import ( - _interface "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" - "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" + _interface "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" "math/rand" ) diff --git a/scheduler-mode/scheduler-with-plugins/demo/pod.go b/scheduler-mode/scheduler-with-plugins/demo/pod.go index 12a5bf6..d0723f4 100644 --- a/scheduler-mode/scheduler-with-plugins/demo/pod.go +++ b/scheduler-mode/scheduler-with-plugins/demo/pod.go @@ -1,7 +1,7 @@ package demo import ( - _interface "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" + _interface "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" "k8s.io/klog/v2" ) diff --git a/scheduler-mode/scheduler-with-plugins/interface/interface.go b/scheduler-mode/scheduler-with-plugins/interface/interface.go index 938f993..9bb830a 100644 --- a/scheduler-mode/scheduler-with-plugins/interface/interface.go +++ b/scheduler-mode/scheduler-with-plugins/interface/interface.go @@ -1,6 +1,6 @@ package _interface -import "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" +import "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" // Pod 接口 type Pod interface { diff --git a/scheduler-mode/scheduler-with-plugins/queue.go b/scheduler-mode/scheduler-with-plugins/queue.go index 67fd62b..7ac67a1 100644 --- a/scheduler-mode/scheduler-with-plugins/queue.go +++ b/scheduler-mode/scheduler-with-plugins/queue.go @@ -1,6 +1,6 @@ package scheduler_with_plugins -import _interface "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" +import _interface "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" // Queue 调度队列 type Queue struct { diff --git a/scheduler-mode/scheduler-with-plugins/scheduler.go b/scheduler-mode/scheduler-with-plugins/scheduler.go index 6656038..cc64b25 100644 --- a/scheduler-mode/scheduler-with-plugins/scheduler.go +++ b/scheduler-mode/scheduler-with-plugins/scheduler.go @@ -1,8 +1,8 @@ package scheduler_with_plugins import ( - _interface "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" - "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" + _interface "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" "k8s.io/klog/v2" "sync" ) @@ -37,7 +37,7 @@ type SchedulerOption func(options *schedulerOptions) var defaultOptions = schedulerOptions{ numWorker: 3, queueCapacity: 10, - test: "test", + test: "tree-template-engine", test1: 10, } diff --git a/scheduler-mode/scheduler-with-plugins/scheduler_with_plugin_test.go b/scheduler-mode/scheduler-with-plugins/scheduler_with_plugin_test.go index 638c662..c9d4d39 100644 --- a/scheduler-mode/scheduler-with-plugins/scheduler_with_plugin_test.go +++ b/scheduler-mode/scheduler-with-plugins/scheduler_with_plugin_test.go @@ -2,9 +2,9 @@ package scheduler_with_plugins import ( "fmt" - "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/demo" - _interface "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" - "github.com/practice/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/demo" + _interface "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/interface" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/scheduler-mode/scheduler-with-plugins/nodes" "k8s.io/klog/v2" "sync" "testing" @@ -13,7 +13,7 @@ import ( func TestSchedulerWithPlugin(test *testing.T) { - schedulerName := fmt.Sprintf("%s-scheduler", "test") + schedulerName := fmt.Sprintf("%s-scheduler", "tree-template-engine") // 调度器 scheduler := Scheduler{ diff --git a/task-job-mode/common/constants/task.go b/task-job-mode/common/constants/task.go index 2d4b6dc..74021bd 100644 --- a/task-job-mode/common/constants/task.go +++ b/task-job-mode/common/constants/task.go @@ -1,6 +1,6 @@ package constants -import "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/model" +import "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/model" // 状态。目前task、job及plugin共用。 const ( diff --git a/task-job-mode/plugins/demo/demo.go b/task-job-mode/plugins/demo/demo.go index dca56d4..54a00cc 100644 --- a/task-job-mode/plugins/demo/demo.go +++ b/task-job-mode/plugins/demo/demo.go @@ -2,8 +2,8 @@ package demo import ( "encoding/json" - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/common/constants" - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/model" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/common/constants" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/model" "log" "strconv" "time" diff --git a/task-job-mode/plugins/interface.go b/task-job-mode/plugins/interface.go index 9bb9416..86030af 100644 --- a/task-job-mode/plugins/interface.go +++ b/task-job-mode/plugins/interface.go @@ -1,8 +1,8 @@ package plugins import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/model" - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/plugins/demo" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/model" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/plugins/demo" ) func init() { diff --git a/task-job-mode/pool.go b/task-job-mode/pool.go index 6bb110e..c00531e 100644 --- a/task-job-mode/pool.go +++ b/task-job-mode/pool.go @@ -1,7 +1,7 @@ package task_job_mode import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/model" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/model" "log" "sync" ) diff --git a/task-job-mode/queue.go b/task-job-mode/queue.go index ecde2ca..1f98578 100644 --- a/task-job-mode/queue.go +++ b/task-job-mode/queue.go @@ -2,7 +2,7 @@ package task_job_mode import ( "container/list" - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/model" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/model" "sync" ) diff --git a/task-job-mode/worker.go b/task-job-mode/worker.go index b8acde9..208f029 100644 --- a/task-job-mode/worker.go +++ b/task-job-mode/worker.go @@ -1,9 +1,9 @@ package task_job_mode import ( - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/common/constants" - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/model" - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/plugins" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/common/constants" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/model" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/plugins" "log" ) diff --git a/task-job-mode/worker_test.go b/task-job-mode/worker_test.go index b345cc2..5c2cfa5 100644 --- a/task-job-mode/worker_test.go +++ b/task-job-mode/worker_test.go @@ -3,8 +3,8 @@ package task_job_mode import ( "encoding/json" "github.com/google/uuid" - "github.com/practice/Golang-Concurrency-Pattern-Demo/task-job-mode/model" . "github.com/smartystreets/goconvey/convey" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/task-job-mode/model" "log" "strconv" "sync" diff --git a/tree-template-engine/manager.go b/tree-template-engine/manager.go new file mode 100644 index 0000000..4347fe5 --- /dev/null +++ b/tree-template-engine/manager.go @@ -0,0 +1,39 @@ +package tree_template + +import ( + "github.com/study-io/Golang-Concurrency-Pattern-Demo/tree-template-engine/model" + "gopkg.in/yaml.v2" + "log" + "os" +) + +type TreeTemplateEngine struct { +} + +func NewTreeTemplateEngine() *TreeTemplateEngine { + return &TreeTemplateEngine{} +} + +func (tte *TreeTemplateEngine) RunTask(task *model.Task) { + status := &model.Status{ + Tasks: make(map[string]*model.Task), + } + runTask(task, status) +} + +func (tte *TreeTemplateEngine) ParseYaml(path string) *model.Task { + // 读取YAML文件 + data, err := os.ReadFile(path) + if err != nil { + log.Fatalf("Error reading YAML file: %v\n", err) + } + + // 解析YAML文件 + + var taskTree model.Task + err = yaml.Unmarshal(data, &taskTree) + if err != nil { + log.Fatalf("Error parsing YAML: %v\n", err) + } + return &taskTree +} diff --git a/tree-template-engine/model/task.go b/tree-template-engine/model/task.go new file mode 100644 index 0000000..ce40412 --- /dev/null +++ b/tree-template-engine/model/task.go @@ -0,0 +1,18 @@ +package model + +import "sync" + +type Task struct { + Name string `yaml:"name"` + Type string `yaml:"type"` + Commands []string `yaml:"commands"` + Children []Task `yaml:"children"` + OnFailure string `yaml:"on_failure"` // 新增字段 + Failed bool // 新增字段 + Completed bool +} + +type Status struct { + sync.Mutex + Tasks map[string]*Task +} diff --git a/tree-template-engine/task.yaml b/tree-template-engine/task.yaml new file mode 100644 index 0000000..772fd63 --- /dev/null +++ b/tree-template-engine/task.yaml @@ -0,0 +1,51 @@ +name: MyTask +# 填入任务类型 区分为 serial(串行) parallel(并行) command(命令) 形式 +type: parallel # 代表子任务使用并行执行 +# 填入子任务模版 +children: + - name: Task 1 + type: parallel + children: + - name: Subtask 1 + type: command # 代表该任务是真正执行的任务本身 + commands: # bash 命令 + - echo "Running Subtask 1" + - name: Subtask 2 + type: command + on_failure: stop # 定义任务失败的处理逻辑,支持 1.skip 跳过 2.stop 停止任务 3.retry 重试 4.执行 bash 命令等 + commands: + - echo "Running Subtask 2" + - exit 0 + - name: Task 2 + type: parallel + children: + - name: Subtask 3 + type: parallel + children: + - name: Subtask children 3-1 + type: command + commands: + - sleep 10 && echo "Running Subtask children 3-1" + - exit 0 + - name: Subtask children 3-2 + type: command + on_failure: retry + commands: + - sleep 10 && echo "Running Subtask children 3-2" + - name: Subtask 4 + type: command + on_failure: skip + commands: + - echo "Running Subtask 4" + - exit 0 + - name: Task 3 + type: serial + children: + - name: Subtask 5 + type: command + commands: + - echo "Running Subtask 5" + - name: Subtask 6 + type: command + commands: + - sleep 10 && echo "Running Subtask 6" \ No newline at end of file diff --git a/tree-template-engine/template_engine.go b/tree-template-engine/template_engine.go new file mode 100644 index 0000000..abc0579 --- /dev/null +++ b/tree-template-engine/template_engine.go @@ -0,0 +1,105 @@ +package tree_template + +import ( + "fmt" + "github.com/study-io/Golang-Concurrency-Pattern-Demo/tree-template-engine/model" + "log" + "os" + "os/exec" + + "sync" + "syscall" +) + +// runCommand 执行 bash 命令 +func runCommand(command string) error { + cmd := exec.Command("sh", "-c", command) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + // 如果命令执行失败,则返回错误 + exitErr, ok := err.(*exec.ExitError) + if ok { + // 获取命令的退出状态码 + status := exitErr.Sys().(syscall.WaitStatus).ExitStatus() + return fmt.Errorf("command failed with exit code %d", status) + } + return err + } + return nil +} + +// handleFailure 处理错误 +func handleFailure(task *model.Task, status *model.Status) { + switch task.OnFailure { + case "stop": + os.Exit(1) + case "skip": + return + case "retry": + task.Failed = false + task.Completed = false + runTask(task, status) + default: + err := runCommand(task.OnFailure) + if err != nil { + log.Printf("Error executing custom script: %v\n", err) + } + } +} + +// runTask 递归执行命令 +func runTask(task *model.Task, status *model.Status) { + + switch task.Type { + // 执行命令 + case "command": + for _, command := range task.Commands { + err := runCommand(command) + if err != nil { + log.Printf("Error executing command: %v\n", err) + task.Failed = true // 标记任务为失败状态 + task.Completed = true // 完成任务以跳过 on_failure 任务 + handleFailure(task, status) // 执行 on_failure 任务 + return + } + } + // 串行执行 + case "serial": + for _, child := range task.Children { + runTask(&child, status) + if !child.Completed { + task.Failed = true + } + } + // 并行执行 + case "parallel": + var wg sync.WaitGroup + wg.Add(len(task.Children)) + for i := range task.Children { + go func(child model.Task) { + runTask(&child, status) + wg.Done() + }(task.Children[i]) + } + wg.Wait() + + task.Failed = false + for _, child := range task.Children { + if child.Failed { + task.Failed = true + break + } + } + } + + task.Completed = true + status.Lock() + defer status.Unlock() + for _, t := range status.Tasks { + if !t.Completed { + return + } + } +} diff --git a/tree-template-engine/template_engine_test.go b/tree-template-engine/template_engine_test.go new file mode 100644 index 0000000..b6cef7d --- /dev/null +++ b/tree-template-engine/template_engine_test.go @@ -0,0 +1,14 @@ +package tree_template + +import ( + "fmt" + "testing" +) + +func TestTreeTemplateTask(t *testing.T) { + + te := NewTreeTemplateEngine() + te.RunTask(te.ParseYaml("./task.yaml")) + + fmt.Println("All tasks completed successfully.") +} diff --git a/workqueue-mode/delay_queue_test.go b/workqueue-mode/delay_queue_test.go index 69799a4..7625cf7 100644 --- a/workqueue-mode/delay_queue_test.go +++ b/workqueue-mode/delay_queue_test.go @@ -20,7 +20,7 @@ func TestSimpleQueue(t *testing.T) { }, }) - first := "test" + first := "tree-template-engine" first1 := "test1" first2 := "test2" first3 := "test3" @@ -46,7 +46,7 @@ func TestSimpleQueue(t *testing.T) { klog.Infof("get item: %v", item) klog.Infof("after get ont item, queue len: %v", q.Len()) - q.AddAfter("test-add-in-8-seconds", 8*time.Second) + q.AddAfter("tree-template-engine-add-in-8-seconds", 8*time.Second) q.AddAfter("test2-add-in-8-seconds", 8*time.Second) q.AddAfter("test3-add-in-8-seconds;fj", 8*time.Second)