diff --git a/openhands/app_server/worker/__main__.py b/openhands/app_server/worker/__main__.py index 5c665579c3..f1a6c3dd7c 100644 --- a/openhands/app_server/worker/__main__.py +++ b/openhands/app_server/worker/__main__.py @@ -17,10 +17,22 @@ from openhands.app_server.utils.logger import openhands_logger # noqa: E402 from openhands.app_server.worker.app import QUEUES, WorkerConfig, app # noqa: E402 from openhands.app_server.worker.database import build_connector # noqa: E402 +from openhands.app_server.worker.housekeeping import SCHEDULED_JOBS_QUEUE # noqa: E402 +from openhands.app_server.worker.scheduled_jobs import ( # noqa: E402 + NAMESPACE, + scheduled_jobs_blueprint, +) async def run() -> None: config: WorkerConfig = from_env(WorkerConfig, 'OH_WORKER') + scheduled = config.scheduled_jobs.enabled() + queues = list(QUEUES) + if scheduled: + app.add_tasks_from( + scheduled_jobs_blueprint(config.scheduled_jobs), namespace=NAMESPACE + ) + queues.append(SCHEDULED_JOBS_QUEUE) connector = build_connector( get_global_config().db_session, # One connection per running job, plus the worker's own queries. @@ -30,9 +42,13 @@ async def run() -> None: async with app.open_async(): openhands_logger.info( 'worker.started', - extra={'concurrency': config.concurrency, 'queues': QUEUES}, + extra={ + 'concurrency': config.concurrency, + 'queues': queues, + 'scheduled_jobs': scheduled, + }, ) - await app.run_worker_async(concurrency=config.concurrency, queues=QUEUES) + await app.run_worker_async(concurrency=config.concurrency, queues=queues) def main() -> None: diff --git a/openhands/app_server/worker/app.py b/openhands/app_server/worker/app.py index ff8e8ec124..727d777c6f 100644 --- a/openhands/app_server/worker/app.py +++ b/openhands/app_server/worker/app.py @@ -5,6 +5,7 @@ from openhands.app_server.sandbox.lifecycle.tasks import lifecycle from openhands.app_server.worker.housekeeping import housekeeping +from openhands.app_server.worker.scheduled_jobs import ScheduledJobsConfig class WorkerConfig(BaseModel): @@ -13,6 +14,11 @@ class WorkerConfig(BaseModel): concurrency: int = Field( default=10, description='How many jobs the worker runs at once.' ) + scheduled_jobs: ScheduledJobsConfig = Field( + default_factory=ScheduledJobsConfig, + description='The CronJob jobs this worker schedules, from ' + '``OH_WORKER_SCHEDULED_JOBS_``.', + ) # procrastinate binds each task to the one app its blueprint is added to, so diff --git a/openhands/app_server/worker/scheduled_jobs.py b/openhands/app_server/worker/scheduled_jobs.py new file mode 100644 index 0000000000..6b8f818a67 --- /dev/null +++ b/openhands/app_server/worker/scheduled_jobs.py @@ -0,0 +1,125 @@ +"""The Kubernetes CronJobs' jobs, as periodic worker tasks. + +For installs without CronJobs, such as Docker Compose. Every job is off until +enabled with ``OH_WORKER_SCHEDULED_JOBS_=true``. Never enable a job on a +database where its CronJob still runs, or both will run it. + +Schedules and time limits match the OpenHands-Cloud chart's CronJobs. A failed +run isn't retried; the next scheduled run is the retry. +""" + +import asyncio +from collections.abc import Callable +from typing import Any + +from procrastinate import Blueprint +from pydantic import BaseModel + +from openhands.app_server.worker.housekeeping import SCHEDULED_JOBS_QUEUE + +NAMESPACE = 'scheduled_jobs' + +# The CronJobs' activeDeadlineSeconds, set only on the two budget jobs. +DEADLINE_SECONDS = 1800 + + +class ScheduledJobsConfig(BaseModel): + """Which jobs the worker schedules.""" + + budget_maintenance: bool = False + maintenance_tasks: bool = False + app_conversation_start_task_clean: bool = False + proactive_convo_clean: bool = False + resend_sync: bool = False + install_gitlab_webhooks: bool = False + enrich_user_interaction: bool = False + + def enabled(self) -> list[str]: + return [name for name, on in self.model_dump().items() if on] + + +# Each job imports its module when it runs: some modules build API clients or +# read their settings at import, which a worker not scheduling that job shouldn't do. + + +async def budget_maintenance(timestamp: int) -> None: + import run_budget_maintenance + + async with asyncio.timeout(DEADLINE_SECONDS): + await run_budget_maintenance.run() + + +async def maintenance_tasks(timestamp: int) -> None: + import run_maintenance_tasks + + async with asyncio.timeout(DEADLINE_SECONDS): + await run_maintenance_tasks.main() + + +async def app_conversation_start_task_clean(timestamp: int) -> None: + from sync import clean_app_conversation_start_tasks + + await clean_app_conversation_start_tasks.main() + + +async def proactive_convo_clean(timestamp: int) -> None: + from sync import clean_proactive_convo_table + + await clean_proactive_convo_table.main() + + +def resend_sync(timestamp: int) -> None: + # A plain def, so procrastinate runs it in a thread where its blocking calls + # can't stall the other jobs. + from sync import resend_keycloak + + resend_keycloak.sync_users_to_resend() + + +async def install_gitlab_webhooks(timestamp: int) -> None: + from sync.install_gitlab_webhooks import VerifyWebhookStatus + + await VerifyWebhookStatus().install_webhooks() + + +async def enrich_user_interaction(timestamp: int) -> None: + from sync import enrich_user_interaction_data + + await enrich_user_interaction_data.main() + + +SCHEDULES: dict[str, tuple[Callable[..., Any], str]] = { + 'budget_maintenance': (budget_maintenance, '*/15 * * * *'), + 'maintenance_tasks': (maintenance_tasks, '0 6 * * *'), + 'app_conversation_start_task_clean': ( + app_conversation_start_task_clean, + '0 3 * * *', + ), + 'proactive_convo_clean': (proactive_convo_clean, '0 2 * * *'), + 'resend_sync': (resend_sync, '0 1 * * *'), + 'install_gitlab_webhooks': (install_gitlab_webhooks, '* * * * *'), + 'enrich_user_interaction': (enrich_user_interaction, '*/10 * * * *'), +} + + +def scheduled_jobs_blueprint(config: ScheduledJobsConfig) -> Blueprint: + """A periodic task for each enabled job.""" + blueprint = Blueprint() + for name in config.enabled(): + func, cron = SCHEDULES[name] + # Runs don't overlap, as under the CronJobs' concurrencyPolicy: Forbid, but + # one run that comes due meanwhile waits instead of being skipped. The + # exception is a run that housekeeping ends as stalled while its worker is + # still alive: it keeps going beside the next run, so jobs must tolerate that. + # No retry=: a retry returns the failed run to todo, which that waiting run's + # queueing lock forbids, so the failed run would stay doing and hold lock. + lock = f'{NAMESPACE}:{name}' + task = blueprint.task( + func, + name=name, + queue=SCHEDULED_JOBS_QUEUE, + lock=lock, + queueing_lock=lock, + ) + blueprint.periodic(cron=cron)(task) + return blueprint diff --git a/run_budget_maintenance.py b/run_budget_maintenance.py index 6e5c4704f0..20920ecc92 100644 --- a/run_budget_maintenance.py +++ b/run_budget_maintenance.py @@ -81,7 +81,7 @@ def enqueue_budget_tasks(batch_size: int = BATCH_SIZE) -> int: return len(org_ids) -def main() -> None: +async def run() -> None: # Guard against LiteLLM's interactive device login on this headless entrypoint # (OpenHands/enterprise#565); run_maintenance_tasks.main() installs it too, but # this keeps the protection local to the entrypoint and is idempotent. @@ -97,7 +97,11 @@ def main() -> None: else: logger.info('No org budget settings found; skipping maintenance enqueue') - asyncio.run(run_maintenance_tasks.main()) + await run_maintenance_tasks.main() + + +def main() -> None: + asyncio.run(run()) if __name__ == '__main__': diff --git a/tests/unit/app_server/test_worker_scheduled_jobs.py b/tests/unit/app_server/test_worker_scheduled_jobs.py new file mode 100644 index 0000000000..9df67bfa8f --- /dev/null +++ b/tests/unit/app_server/test_worker_scheduled_jobs.py @@ -0,0 +1,334 @@ +"""The CronJob jobs as periodic worker tasks, run by a real worker on this test's Postgres.""" + +import asyncio +import contextlib +import logging +import time + +import psycopg +import pytest +from procrastinate import App, PsycopgConnector + +import run_budget_maintenance +from openhands.agent_server.env_parser import from_env +from openhands.app_server.utils.logger import openhands_logger +from openhands.app_server.worker import __main__ as worker_main +from openhands.app_server.worker import scheduled_jobs +from openhands.app_server.worker.app import WorkerConfig, app +from openhands.app_server.worker.housekeeping import SCHEDULED_JOBS_QUEUE +from openhands.app_server.worker.scheduled_jobs import ( + NAMESPACE, + ScheduledJobsConfig, + scheduled_jobs_blueprint, +) + +# The OpenHands-Cloud chart's CronJob schedules. +CHART_SCHEDULES = { + 'budget_maintenance': '*/15 * * * *', + 'maintenance_tasks': '0 6 * * *', + 'app_conversation_start_task_clean': '0 3 * * *', + 'proactive_convo_clean': '0 2 * * *', + 'resend_sync': '0 1 * * *', + 'install_gitlab_webhooks': '* * * * *', + 'enrich_user_interaction': '*/10 * * * *', +} +ALL_ON = ScheduledJobsConfig(**dict.fromkeys(CHART_SCHEDULES, True)) + +# Each job's CronJob entrypoint; resend_sync's is sync and must run off the loop. +ENTRYPOINTS = { + 'budget_maintenance': 'run_budget_maintenance.run', + 'maintenance_tasks': 'run_maintenance_tasks.main', + 'app_conversation_start_task_clean': 'sync.clean_app_conversation_start_tasks.main', + 'proactive_convo_clean': 'sync.clean_proactive_convo_table.main', + 'resend_sync': 'sync.resend_keycloak.sync_users_to_resend', + 'install_gitlab_webhooks': 'sync.install_gitlab_webhooks.VerifyWebhookStatus.install_webhooks', + 'enrich_user_interaction': 'sync.enrich_user_interaction_data.main', +} + +# A lost deadline would otherwise hang the suite on the planted delay=600 row. +pytestmark = pytest.mark.timeout(60) + + +def test_every_job_is_off_unless_enabled(monkeypatch): + assert from_env(WorkerConfig, 'OH_WORKER').scheduled_jobs.enabled() == [] + + monkeypatch.setenv('OH_WORKER_SCHEDULED_JOBS_RESEND_SYNC', 'true') + monkeypatch.setenv('OH_WORKER_SCHEDULED_JOBS_BUDGET_MAINTENANCE', '1') + monkeypatch.setenv('OH_WORKER_SCHEDULED_JOBS_MAINTENANCE_TASKS', 'false') + + config = from_env(WorkerConfig, 'OH_WORKER').scheduled_jobs + assert config.enabled() == ['budget_maintenance', 'resend_sync'] + + +def test_only_enabled_jobs_are_scheduled(): + blueprint = scheduled_jobs_blueprint(ScheduledJobsConfig(resend_sync=True)) + + assert list(blueprint.tasks) == ['resend_sync'] + assert scheduled_jobs_blueprint(ScheduledJobsConfig()).tasks == {} + + +def test_each_job_keeps_its_cronjobs_schedule_and_overlap_policy(): + blueprint = scheduled_jobs_blueprint(ALL_ON) + + schedules = {task.name: cron for (task, cron) in _periodic(blueprint).items()} + assert schedules == CHART_SCHEDULES + for name, task in blueprint.tasks.items(): + assert task.queue == SCHEDULED_JOBS_QUEUE + assert task.lock == task.queueing_lock == f'{NAMESPACE}:{name}' + assert task.retry_strategy is None + + +def _periodic(blueprint) -> dict: + return { + periodic.task: periodic.cron + for periodic in blueprint.periodic_registry.periodic_tasks.values() + } + + +@pytest.fixture +def restore_app(): + """Undo the tasks ``worker_main.run`` adds to the process-wide app.""" + tasks = dict(app.tasks) + periodic = dict(app.periodic_registry.periodic_tasks) + yield + app.tasks.clear() + app.tasks.update(tasks) + app.periodic_registry.periodic_tasks.clear() + app.periodic_registry.periodic_tasks.update(periodic) + + +@pytest.mark.usefixtures('app_db_session', 'restore_app') +async def test_worker_takes_scheduled_jobs_only_once_one_is_enabled( + monkeypatch, caplog +): + started_with: dict = {} + + async def run_worker_async(**kwargs): + # The real call runs until the process is stopped. + started_with.update(kwargs) + + monkeypatch.setattr(app, 'run_worker_async', run_worker_async) + monkeypatch.setattr(openhands_logger, 'propagate', True) + monkeypatch.setenv('OH_WORKER_SCHEDULED_JOBS_RESEND_SYNC', 'true') + + with caplog.at_level(logging.INFO, logger=openhands_logger.name): + await worker_main.run() + + assert started_with['queues'] == ['default', SCHEDULED_JOBS_QUEUE] + assert f'{NAMESPACE}:resend_sync' in app.tasks + assert not any( + name.startswith(f'{NAMESPACE}:') and name != f'{NAMESPACE}:resend_sync' + for name in app.tasks + ) + [started] = [r for r in caplog.records if r.getMessage() == 'worker.started'] + assert started.scheduled_jobs == ['resend_sync'] + + +@pytest.mark.usefixtures('app_db_session', 'restore_app') +async def test_worker_skips_scheduled_jobs_queue_when_none_is_enabled(monkeypatch): + started_with: dict = {} + + async def run_worker_async(**kwargs): + started_with.update(kwargs) + + monkeypatch.setattr(app, 'run_worker_async', run_worker_async) + + await worker_main.run() + + assert started_with['queues'] == ['default'] + + +def test_budget_cronjob_entrypoint_still_runs_the_job(monkeypatch): + ran = [] + + async def run(): + ran.append(True) + + monkeypatch.setattr(run_budget_maintenance, 'run', run) + + run_budget_maintenance.main() + + assert ran == [True] + + +@pytest.fixture +def conninfo(test_database) -> dict: + server = test_database.server + return { + 'host': server.host, + 'port': server.port, + 'user': server.user, + 'password': server.password, + 'dbname': test_database.name, + } + + +@contextlib.asynccontextmanager +async def _open_worker_app(conninfo: dict, config: ScheduledJobsConfig): + """Only ``config``'s jobs, so no other job's periodic run comes due mid-test.""" + worker_app = App( + connector=PsycopgConnector(kwargs=conninfo, min_size=1, max_size=3) + ) + worker_app.add_tasks_from(scheduled_jobs_blueprint(config), namespace=NAMESPACE) + async with worker_app.open_async(): + yield worker_app + + +def _sql(conninfo: dict, query: str, *params) -> list[tuple]: + with psycopg.connect(**conninfo, autocommit=True) as connection: + cursor = connection.execute(query, params) + return cursor.fetchall() if cursor.description else [] + + +async def _run_job(worker_app: App, conninfo: dict, name: str) -> tuple[str, int]: + """Run one job to its end on a real worker and return its status and attempts.""" + job_id = await worker_app.configure_task(f'{NAMESPACE}:{name}').defer_async( + timestamp=0 + ) + await worker_app.run_worker_async( + queues=[SCHEDULED_JOBS_QUEUE], wait=False, concurrency=1 + ) + [(status, attempts)] = _sql( + conninfo, + 'SELECT status, attempts FROM procrastinate_jobs WHERE id = %s', + job_id, + ) + return status, attempts + + +@pytest.mark.usefixtures('app_db_session') +async def test_a_job_runs_its_existing_code_in_the_worker(conninfo): + _sql( + conninfo, + 'INSERT INTO app_conversation_start_task (id, created_by_user_id, request, created_at) ' + "VALUES (gen_random_uuid(), 'user', '{}', now() - interval '2 days'), " + "(gen_random_uuid(), 'user', '{}', now())", + ) + + config = ScheduledJobsConfig(app_conversation_start_task_clean=True) + async with _open_worker_app(conninfo, config) as worker_app: + status, _ = await _run_job( + worker_app, conninfo, 'app_conversation_start_task_clean' + ) + + assert status == 'succeeded' + # Only the row older than a day is gone. + assert _sql(conninfo, 'SELECT count(*) FROM app_conversation_start_task') == [(1,)] + + +@pytest.mark.usefixtures('app_db_session') +async def test_a_sync_job_that_exits_fails_and_the_worker_survives( + conninfo, monkeypatch +): + from sync import resend_keycloak + + # The job calls sys.exit(1) without its API key, as its CronJob pod did. + monkeypatch.setattr(resend_keycloak, 'RESEND_API_KEY', None) + + config = ScheduledJobsConfig(resend_sync=True, proactive_convo_clean=True) + async with _open_worker_app(conninfo, config) as worker_app: + status, attempts = await _run_job(worker_app, conninfo, 'resend_sync') + + assert (status, attempts) == ('failed', 1) + # The worker is still usable after the job's SystemExit. + status, _ = await _run_job(worker_app, conninfo, 'proactive_convo_clean') + assert status == 'succeeded' + + +@pytest.mark.usefixtures('app_db_session') +@pytest.mark.parametrize('name', ['budget_maintenance', 'maintenance_tasks']) +async def test_a_budget_job_is_stopped_at_its_deadline(conninfo, monkeypatch, name): + monkeypatch.setattr(scheduled_jobs, 'DEADLINE_SECONDS', 1) + # run_maintenance_tasks sleeps for a task's delay after processing it, so this + # row keeps the job running past its deadline. + _sql( + conninfo, + 'INSERT INTO maintenance_tasks (status, processor_type, processor_json, delay) ' + "VALUES ('PENDING', 'not.a.processor', '{}', 600)", + ) + + started = time.monotonic() + async with _open_worker_app( + conninfo, ScheduledJobsConfig(**{name: True}) + ) as worker_app: + status, attempts = await _run_job(worker_app, conninfo, name) + + assert time.monotonic() - started < 30 + assert (status, attempts) == ('failed', 1) + + +@pytest.mark.usefixtures('app_db_session') +@pytest.mark.parametrize('name', ENTRYPOINTS) +async def test_each_job_runs_its_cronjobs_entrypoint(conninfo, monkeypatch, name): + # enrich_user_interaction_data builds a GitHub App client at import. + monkeypatch.setattr( + 'integrations.github.data_collector.GitHubDataCollector', object + ) + ran_on: list[str] = [] + + def record(*args): + try: + asyncio.get_running_loop() + ran_on.append('event loop') + except RuntimeError: + ran_on.append('thread') + + async def async_record(*args): + record() + + entrypoint = record if name == 'resend_sync' else async_record + monkeypatch.setattr(ENTRYPOINTS[name], entrypoint) + + async with _open_worker_app( + conninfo, ScheduledJobsConfig(**{name: True}) + ) as worker_app: + status, _ = await _run_job(worker_app, conninfo, name) + + assert status == 'succeeded' + # Its own periodic run may come due too, so it can run more than once. + assert ran_on + assert set(ran_on) == {'thread' if name == 'resend_sync' else 'event loop'} + + +async def _wait_for(conninfo: dict, job_id: int, statuses: set[str]) -> str: + for _ in range(200): + [(status,)] = _sql( + conninfo, 'SELECT status FROM procrastinate_jobs WHERE id = %s', job_id + ) + if status in statuses: + return status + await asyncio.sleep(0.1) + return status + + +@pytest.mark.usefixtures('app_db_session') +async def test_a_run_that_fails_while_the_next_waits_lets_the_next_start( + conninfo, monkeypatch +): + monkeypatch.setattr(scheduled_jobs, 'DEADLINE_SECONDS', 1) + # The first run claims this row and sleeps past its deadline. The second finds + # the row claimed, so it has nothing to do and succeeds. + _sql( + conninfo, + 'INSERT INTO maintenance_tasks (status, processor_type, processor_json, delay) ' + "VALUES ('PENDING', 'not.a.processor', '{}', 600)", + ) + config = ScheduledJobsConfig(maintenance_tasks=True) + async with _open_worker_app(conninfo, config) as worker_app: + task = worker_app.configure_task(f'{NAMESPACE}:maintenance_tasks') + first = await task.defer_async(timestamp=0) + worker = asyncio.create_task( + worker_app.run_worker_async(queues=[SCHEDULED_JOBS_QUEUE], concurrency=2) + ) + try: + assert await _wait_for(conninfo, first, {'doing'}) == 'doing' + second = await task.defer_async(timestamp=1) + + assert await _wait_for(conninfo, first, {'failed'}) == 'failed' + assert await _wait_for(conninfo, second, {'succeeded', 'failed'}) == ( + 'succeeded' + ) + finally: + worker.cancel() + with contextlib.suppress(asyncio.CancelledError): + await worker