Skip to content

Commit

Permalink
Job scheduler: Report time stamp of next cycle
Browse files Browse the repository at this point in the history
Change-Id: I85484acbbfc5221a8dc728891798ed13c3a4ff4c
  • Loading branch information
LarsMichelsen committed Feb 11, 2025
1 parent 7a1a02b commit a15f4e5
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmk/gui/background_job/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class BackgroundJobsHealth(BaseModel, frozen=True):


class ScheduledJobsHealth(BaseModel, frozen=True):
next_cycle_start: int
running_jobs: Mapping[str, int]
job_executions: Mapping[str, int]

Expand Down
1 change: 1 addition & 0 deletions cmk/gui/job_scheduler/_background_jobs/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async def check_health(request: Request) -> HealthResponse:
job_executions=executor.job_executions(),
),
scheduled_jobs=ScheduledJobsHealth(
next_cycle_start=scheduler_state.next_cycle_start,
running_jobs={
name: job.started_at
for name, job in filter_running_jobs(scheduler_state.running_jobs).items()
Expand Down
2 changes: 2 additions & 0 deletions cmk/gui/job_scheduler/_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def _run_scheduler(
logger.error("Exception in scheduler (Crash ID: %s)", crash_msg, exc_info=True)

if (sleep_time := 60 - (time.time() - cycle_start)) > 0:
state.next_cycle_start = int(time.time() + sleep_time)
stop_event.wait(sleep_time)
finally:
# The UI code does not clean up locks properly in all cases, so we need to do it here
Expand Down Expand Up @@ -211,5 +212,6 @@ class ScheduledJob:

@dataclass
class SchedulerState:
next_cycle_start: int = 0
running_jobs: dict[str, ScheduledJob] = field(default_factory=dict)
job_executions: Counter[str] = field(default_factory=Counter)
2 changes: 2 additions & 0 deletions tests/unit/cmk/gui/job_scheduler/background_jobs/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _get_test_client(loaded_at: int) -> TestClient:
registered_jobs={"hello_job": HelloJob},
executor=DummyExecutor(logger),
scheduler_state=SchedulerState(
next_cycle_start=10,
running_jobs={
"scheduled_1_running": ScheduledJob(
started_at=123,
Expand Down Expand Up @@ -190,6 +191,7 @@ def test_health_check() -> None:
)
assert response.background_jobs.running_jobs == {"job_id": 42}
assert response.background_jobs.job_executions == {"job_1": 1, "job_2": 2}
assert response.scheduled_jobs.next_cycle_start == 10
assert response.scheduled_jobs.running_jobs == {"scheduled_1_running": 123}
assert response.scheduled_jobs.job_executions == {"scheduled_1": 1, "scheduled_2": 2}

Expand Down

0 comments on commit a15f4e5

Please sign in to comment.