File tree 3 files changed +53
-12
lines changed 3 files changed +53
-12
lines changed Original file line number Diff line number Diff line change 9
9
"github.com/gitpod-io/gitpod/common-go/log"
10
10
"github.com/gitpod-io/gitpod/usage/pkg/controller"
11
11
"github.com/spf13/cobra"
12
+ "time"
12
13
)
13
14
14
15
func init () {
@@ -27,12 +28,15 @@ func run() *cobra.Command {
27
28
Run : func (cmd * cobra.Command , args []string ) {
28
29
log .Init (ServiceName , Version , true , verbose )
29
30
30
- ctrl , err := controller .New ("@every 1m" , controller .ReconcilerFunc (controller .HelloWorldReconciler ))
31
+ ctrl , err := controller .New (1 * time . Minute , controller .ReconcilerFunc (controller .HelloWorldReconciler ))
31
32
if err != nil {
32
33
log .WithError (err ).Fatal ("Failed to initialize usage controller." )
33
34
}
34
35
35
- ctrl .Start ()
36
+ err = ctrl .Start ()
37
+ if err != nil {
38
+ log .WithError (err ).Fatal ("Failed to start usage controller." )
39
+ }
36
40
defer ctrl .Stop ()
37
41
38
42
srv , err := baseserver .New ("usage" )
Original file line number Diff line number Diff line change @@ -12,32 +12,27 @@ import (
12
12
"time"
13
13
)
14
14
15
- func New (schedule string , reconciler Reconciler ) (* Controller , error ) {
16
- sched , err := cron .Parse (schedule )
17
- if err != nil {
18
- return nil , fmt .Errorf ("failed to parse cron schedule: %w" , err )
19
- }
20
-
15
+ func New (schedule time.Duration , reconciler Reconciler ) (* Controller , error ) {
21
16
return & Controller {
22
- schedule : sched ,
17
+ schedule : schedule ,
23
18
reconciler : reconciler ,
24
19
scheduler : cron .NewWithLocation (time .UTC ),
25
20
}, nil
26
21
}
27
22
28
23
type Controller struct {
29
- schedule cron. Schedule
24
+ schedule time. Duration
30
25
reconciler Reconciler
31
26
32
27
scheduler * cron.Cron
33
28
34
29
runningJobs sync.WaitGroup
35
30
}
36
31
37
- func (c * Controller ) Start () {
32
+ func (c * Controller ) Start () error {
38
33
log .Info ("Starting usage controller." )
39
34
40
- c .scheduler .Schedule ( c .schedule , cron .FuncJob (func () {
35
+ err := c .scheduler .AddFunc ( fmt . Sprintf ( "@every %s" , c .schedule . String ()) , cron .FuncJob (func () {
41
36
log .Info ("Starting usage reconciliation." )
42
37
43
38
c .runningJobs .Add (1 )
@@ -50,8 +45,13 @@ func (c *Controller) Start() {
50
45
log .Info ("Completed usage reconciliation run without errors." )
51
46
}
52
47
}))
48
+ if err != nil {
49
+ return fmt .Errorf ("failed to add function to scheduler: %w" , err )
50
+ }
53
51
54
52
c .scheduler .Start ()
53
+
54
+ return nil
55
55
}
56
56
57
57
// Stop terminates the Controller and awaits for all running jobs to complete.
Original file line number Diff line number Diff line change
1
+ // Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
+ // Licensed under the GNU Affero General Public License (AGPL).
3
+ // See License-AGPL.txt in the project root for license information.
4
+
5
+ package controller
6
+
7
+ import (
8
+ "github.com/stretchr/testify/require"
9
+ "testing"
10
+ "time"
11
+ )
12
+
13
+ func TestController (t * testing.T ) {
14
+ schedule := time .Second
15
+ triggered := false
16
+
17
+ ctrl , err := New (schedule , ReconcilerFunc (func () error {
18
+ triggered = true
19
+ return nil
20
+ }))
21
+ require .NoError (t , err )
22
+
23
+ require .NoError (t , ctrl .Start ())
24
+ time .Sleep (schedule + 20 * time .Millisecond )
25
+ require .True (t , triggered , "must trigger reconciler function" )
26
+ ctrl .Stop ()
27
+ }
28
+
29
+ func TestController_GracefullyHandlesPanic (t * testing.T ) {
30
+ ctrl , err := New (20 * time .Millisecond , ReconcilerFunc (func () error {
31
+ panic ("pls help" )
32
+ }))
33
+ require .NoError (t , err )
34
+
35
+ require .NoError (t , ctrl .Start ())
36
+ ctrl .Stop ()
37
+ }
You can’t perform that action at this time.
0 commit comments