Skip to content

Commit

Permalink
Quick fix so we create the task only when scheduled time < now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles-Garnsey committed Feb 25, 2025
1 parent 6516c29 commit 986ab2d
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions internal/controllers/scheduledtask/scheduledtask_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (r *ScheduledTaskReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

now := r.Clock.Now().UTC()
createTask := false

// Calculate the next execution time
nextExecution := sched.Next(previousExecution).UTC()
Expand All @@ -118,6 +119,7 @@ func (r *ScheduledTaskReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
nextExecution = sched.Next(now)
previousExecution = now
createTask = true
}

// Update the status if there are modifications
Expand All @@ -131,25 +133,27 @@ func (r *ScheduledTaskReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
}

generatedName := fmt.Sprintf("%s-%d", scheduledtask.Name, now.Unix())
r.Log.V(1).Info("Scheduled time has been reached, creating a cassandraTask", "CassandraTask name", generatedName)
cassandraTask := &controlapi.CassandraTask{
ObjectMeta: metav1.ObjectMeta{
Name: generatedName,
Namespace: scheduledtask.Namespace,
Labels: dc.GetDatacenterLabels(),
},
Spec: scheduledtask.Spec.TaskDetails.CassandraTaskSpec,
}
if createTask {

generatedName := fmt.Sprintf("%s-%d", scheduledtask.Name, now.Unix())
r.Log.V(1).Info("Scheduled time has been reached, creating a cassandraTask", "CassandraTask name", generatedName)
cassandraTask := &controlapi.CassandraTask{
ObjectMeta: metav1.ObjectMeta{
Name: generatedName,
Namespace: scheduledtask.Namespace,
Labels: dc.GetDatacenterLabels(),
},
Spec: scheduledtask.Spec.TaskDetails.CassandraTaskSpec,
}

if err := r.Client.Create(ctx, cassandraTask); err != nil {
// We've already updated the Status times.. we'll miss this job now?
return ctrl.Result{}, err
if err := r.Client.Create(ctx, cassandraTask); err != nil {
// We've already updated the Status times.. we'll miss this job now?
return ctrl.Result{}, err
}
}

nextRunTime := nextExecution.Sub(now)
r.Log.V(1).Info("Requeing for next scheduled event", "nextRuntime", nextRunTime.String())
return ctrl.Result{}, nil
return ctrl.Result{RequeueAfter: nextRunTime}, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down

0 comments on commit 986ab2d

Please sign in to comment.