Repository: adhocore/gronx
Pull request #36 (2024-03-08 13:52:14)

feat: add stop for tasker
### summary

add `stop()` for task manger.

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/adhocore/gronx/pull/36?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra) Report
Attention: Patch coverage is `0%` with `2 lines` in your changes are missing coverage. Please review.
> Project coverage is 94.46%. Comparing base [(`d3b6afc`)](https://app.codecov.io/gh/adhocore/gronx/commit/d3b6afc93d7118352dc13ae3479c3f4407d66053?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra) to head [(`f155865`)](https://app.codecov.io/gh/adhocore/gronx/pull/36?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra).

| [Files](https://app.codecov.io/gh/adhocore/gronx/pull/36?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra) | Patch % | Lines |
|---|---|---|
| [pkg/tasker/tasker.go](https://app.codecov.io/gh/adhocore/gronx/pull/36?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra#diff-cGtnL3Rhc2tlci90YXNrZXIuZ28=) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/adhocore/gronx/pull/36?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra) |

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main      #36      +/-   ##
==========================================
- Coverage   94.78%   94.46%   -0.32%     
==========================================
  Files           9        9              
  Lines         594      596       +2     
==========================================
  Hits          563      563              
- Misses         16       18       +2     
  Partials       15       15              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/adhocore/gronx/pull/36?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jitendra).


Patch:
diff --git a/pkg/tasker/tasker.go b/pkg/tasker/tasker.go
index afdb79e..0f2dc22 100644
--- a/pkg/tasker/tasker.go
+++ b/pkg/tasker/tasker.go
@@ -256,6 +256,11 @@ func (t *Tasker) Run() {
 	t.wait()
 }
 
+// Stop the task manager.
+func (t *Tasker) Stop() {
+	t.abort = true
+}
+
 var dateFormat = "2006/01/02 15:04:05"
 
 func (t *Tasker) doSetup() {


Test patch:
diff --git a/pkg/tasker/tasker_test.go b/pkg/tasker/tasker_test.go
index 09cdd3a..06bc753 100644
--- a/pkg/tasker/tasker_test.go
+++ b/pkg/tasker/tasker_test.go
@@ -200,3 +200,26 @@ func TestConcurrency(t *testing.T) {
 		}
 	})
 }
+
+func TestStopTasker(t *testing.T) {
+	t.Run("Run", func(t *testing.T) {
+		taskr := New(Option{Verbose: true, Out: "../../test/tasker.out"})
+
+		var incr int
+		taskr.Task("* * * * * *", func(ctx context.Context) (int, error) {
+			incr++
+			return 0, nil
+		}, false)
+
+		go func() {
+			time.Sleep(2 * time.Second)
+			taskr.Stop()
+		}()
+		taskr.Run()
+		fmt.Println(incr)
+
+		if incr != 1 {
+			t.Errorf("the task should run 1x, not %dx", incr)
+		}
+	})
+}