refactor(scheduler): Add statusThreshold parameter to job.Run

The scheduler monitors running jobs to make sure they are not hung. If
the status of a running job does not change after a duration (the
interruptThreshold in the scheduler), the scheduler kills the job. This
change passes the scheduler's interruptThreshold to the job when it
calls Run() on the job.
This commit is contained in:
Michael Gaffney committed 2024-10-25 10:25:51 -04:00
1 parent 50ab600519
commit 9354a4ebcf
27 files changed
+87 -80

No files matched your search

@@ -66,7 +66,7 @@ func (r *OrphanedHostCleanupJob) Status() scheduler.JobStatus {
// deletes those hosts. Can not be run in parallel, if
// Run is invoked while already running an error with code JobAlreadyRunning
// will be returned.
func (r *OrphanedHostCleanupJob) Run(ctx context.Context) error {
func (r *OrphanedHostCleanupJob) Run(ctx context.Context, _ time.Duration) error {
const op = "plugin.(OrphanedHostCleanupJob).Run"
if !r.running.CompareAndSwap(r.running.Load(), true) {
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
@@ -118,7 +118,7 @@ func TestOrphanedHostCleanupJob_Run(t *testing.T) {
err = sche.RegisterJob(context.Background(), r)
require.NoError(err)
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// No sets should have been synced.
assert.Equal(0, r.numProcessed)
@@ -134,7 +134,7 @@ func TestOrphanedHostCleanupJob_Run(t *testing.T) {
TestHost(t, conn, cat.GetPublicId(), "host2")
// Run sync again with the newly created set
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// The single existing set should have been processed
assert.Equal(1, r.numHosts)
@@ -148,14 +148,14 @@ func TestOrphanedHostCleanupJob_Run(t *testing.T) {
TestHost(t, conn, cat.GetPublicId(), "5")
// Run sync again with the freshly synced set
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// The single existing set should have been processed
assert.Equal(5, r.numHosts)
assert.Equal(5, r.numProcessed)
// Run sync again with the freshly synced set
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// The single existing set should have been processed
assert.Equal(0, r.numHosts)
+1 -1
View File
@@ -85,7 +85,7 @@ func (r *SetSyncJob) Status() scheduler.JobStatus {
// creates a plugin client and syncs each set. Can not be run in parallel, if
// Run is invoked while already running an error with code JobAlreadyRunning
// will be returned.
func (r *SetSyncJob) Run(ctx context.Context) error {
func (r *SetSyncJob) Run(ctx context.Context, _ time.Duration) error {
const op = "plugin.(SetSyncJob).Run"
if !r.running.CompareAndSwap(r.running.Load(), true) {
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
+7 -7
View File
@@ -156,7 +156,7 @@ func TestSetSyncJob_Run(t *testing.T) {
err = sche.RegisterJob(context.Background(), r)
require.NoError(err)
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// No sets should have been synced.
assert.Equal(0, r.numProcessed)
@@ -175,7 +175,7 @@ func TestSetSyncJob_Run(t *testing.T) {
TestSetMembers(t, conn, setToRemoveHosts.GetPublicId(), []*Host{hostToRemove})
// Run sync again with the newly created set
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
hsa := &hostSetAgg{PublicId: setToRemoveHosts.GetPublicId()}
@@ -217,7 +217,7 @@ func TestSetSyncJob_Run(t *testing.T) {
assert.Less(hsa.LastSyncTime.AsTime().UnixNano(), hsa.CreateTime.AsTime().UnixNano())
// Run sync again with the newly created set
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// The single existing set should have been processed
assert.Equal(1, r.numSets)
@@ -243,7 +243,7 @@ func TestSetSyncJob_Run(t *testing.T) {
firstSyncTime := hsa.LastSyncTime
// Run sync again with the freshly synced set
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
assert.Equal(0, r.numSets)
assert.Equal(0, r.numProcessed)
@@ -258,7 +258,7 @@ func TestSetSyncJob_Run(t *testing.T) {
assert.True(hs.NeedSync)
// Run sync again with the set needing update
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// The single existing set should have been processed
assert.Equal(1, r.numSets)
@@ -273,7 +273,7 @@ func TestSetSyncJob_Run(t *testing.T) {
// Run sync with a new second set
_ = TestSet(t, conn, kmsCache, sched, cat, plgm)
require.NoError(r.Run(context.Background()))
require.NoError(r.Run(context.Background(), 0))
assert.Equal(1, r.numSets)
assert.Equal(1, r.numProcessed)
@@ -398,7 +398,7 @@ func TestSetSyncJob_Run(t *testing.T) {
assert.Equal(1, count)
// Run job
err = r.Run(context.Background())
err = r.Run(context.Background(), 0)
require.NoError(err)
// Validate results
@@ -1547,7 +1547,7 @@ func (j *testSyncJob) Status() scheduler.JobStatus {
}
}
func (j *testSyncJob) Run(_ context.Context) error { return nil }
func (j *testSyncJob) Run(_ context.Context, _ time.Duration) error { return nil }
func (j *testSyncJob) NextRunIn(_ context.Context) (time.Duration, error) {
return setSyncJobRunInterval, nil
}
+1 -1
View File
@@ -209,7 +209,7 @@ func TestRunSetSync(t testing.TB, conn *db.DB, kmsCache *kms.Kms, plgm map[strin
j, err := newSetSyncJob(ctx, rw, rw, kmsCache, plgm)
require.NoError(t, err)
require.NoError(t, j.Run(ctx))
require.NoError(t, j.Run(ctx, 0))
}
func testGetDnsName(t testing.TB) string {