-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[usage] Setup controller and reconciler
- Loading branch information
Showing
6 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gitpod-io/gitpod/common-go/log" | ||
"github.com/robfig/cron" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func New(schedule time.Duration, reconciler Reconciler) (*Controller, error) { | ||
return &Controller{ | ||
schedule: schedule, | ||
reconciler: reconciler, | ||
scheduler: cron.NewWithLocation(time.UTC), | ||
}, nil | ||
} | ||
|
||
type Controller struct { | ||
schedule time.Duration | ||
reconciler Reconciler | ||
|
||
scheduler *cron.Cron | ||
|
||
runningJobs sync.WaitGroup | ||
} | ||
|
||
func (c *Controller) Start() error { | ||
log.Info("Starting usage controller.") | ||
|
||
err := c.scheduler.AddFunc(fmt.Sprintf("@every %s", c.schedule.String()), cron.FuncJob(func() { | ||
log.Info("Starting usage reconciliation.") | ||
|
||
c.runningJobs.Add(1) | ||
defer c.runningJobs.Done() | ||
|
||
err := c.reconciler.Reconcile() | ||
if err != nil { | ||
log.WithError(err).Errorf("Reconciliation run failed.") | ||
} else { | ||
log.Info("Completed usage reconciliation run without errors.") | ||
} | ||
})) | ||
if err != nil { | ||
return fmt.Errorf("failed to add function to scheduler: %w", err) | ||
} | ||
|
||
c.scheduler.Start() | ||
|
||
return nil | ||
} | ||
|
||
// Stop terminates the Controller and awaits for all running jobs to complete. | ||
func (c *Controller) Stop() { | ||
log.Info("Stopping usage controller.") | ||
// Stop any new jobs from running | ||
c.scheduler.Stop() | ||
|
||
log.Info("Awaiting existing reconciliation runs to complete..") | ||
// Wait for existing jobs to finish | ||
c.runningJobs.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package controller | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestController(t *testing.T) { | ||
schedule := time.Second | ||
triggered := false | ||
|
||
ctrl, err := New(schedule, ReconcilerFunc(func() error { | ||
triggered = true | ||
return nil | ||
})) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, ctrl.Start()) | ||
time.Sleep(schedule + 20*time.Millisecond) | ||
require.True(t, triggered, "must trigger reconciler function") | ||
ctrl.Stop() | ||
} | ||
|
||
func TestController_GracefullyHandlesPanic(t *testing.T) { | ||
ctrl, err := New(20*time.Millisecond, ReconcilerFunc(func() error { | ||
panic("pls help") | ||
})) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, ctrl.Start()) | ||
ctrl.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package controller | ||
|
||
import "github.com/gitpod-io/gitpod/common-go/log" | ||
|
||
type Reconciler interface { | ||
Reconcile() error | ||
} | ||
|
||
type ReconcilerFunc func() error | ||
|
||
func (f ReconcilerFunc) Reconcile() error { | ||
return f() | ||
} | ||
|
||
func HelloWorldReconciler() error { | ||
log.Info("Hello world reconciler!") | ||
return nil | ||
} |