From 552f73182ada8550d9dd07af1b2edda89dc5d809 Mon Sep 17 00:00:00 2001 From: Fahad Ali Shaikh Date: Thu, 22 Aug 2024 20:15:52 +0530 Subject: [PATCH] added dispatch functionality for scheduler (#1447) --- kairon/events/scheduler/kscheduler.py | 5 ++++ kairon/events/server.py | 6 +++++ tests/integration_test/event_service_test.py | 24 ++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/kairon/events/scheduler/kscheduler.py b/kairon/events/scheduler/kscheduler.py index 3cb95e45f..809c7a406 100644 --- a/kairon/events/scheduler/kscheduler.py +++ b/kairon/events/scheduler/kscheduler.py @@ -11,6 +11,7 @@ from kairon.events.scheduler.base import EventSchedulerBase from kairon.exceptions import AppException from datetime import datetime +from apscheduler.events import JobEvent, EVENT_JOB_ADDED import logging logging.getLogger('apscheduler').setLevel(logging.DEBUG) @@ -69,3 +70,7 @@ def delete_job(self, event_id): except JobLookupError as e: logger.exception(e) raise AppException(e) + + def dispatch_event(self, event_id): + event = JobEvent(EVENT_JOB_ADDED, event_id, 'default') + KScheduler.__scheduler._dispatch_event(event) diff --git a/kairon/events/server.py b/kairon/events/server.py index 85779bb52..344e8528f 100644 --- a/kairon/events/server.py +++ b/kairon/events/server.py @@ -138,3 +138,9 @@ def update_scheduled_event( @app.delete("/api/events/{event_id}", response_model=Response) def delete_scheduled_event(event_id: Text = Path(description="Event id")): return {"data": KScheduler().delete_job(event_id), "message": "Scheduled event deleted!"} + + +@app.get("/api/events/dispatch/{event_id}", response_model=Response) +def dispatch_scheduled_event(event_id: Text = Path(description="Event id")): + KScheduler().dispatch_event(event_id) + return {"data": None, "message": "Scheduled event dispatch!"} diff --git a/tests/integration_test/event_service_test.py b/tests/integration_test/event_service_test.py index f03f65817..a042084a3 100644 --- a/tests/integration_test/event_service_test.py +++ b/tests/integration_test/event_service_test.py @@ -8,6 +8,8 @@ from kairon.shared.constants import EventClass, EventExecutor from kairon.shared.utils import Utility +from apscheduler.events import EVENT_JOB_ADDED, JobEvent +from apscheduler.schedulers.background import BackgroundScheduler os_patch = patch.dict( os.environ, @@ -509,4 +511,26 @@ def test_delete_scheduled_event_request(mock_delet_job): "message": "Scheduled event deleted!", } + +@patch("apscheduler.schedulers.base.BaseScheduler._dispatch_event", autospec=True) +def test_scheduled_event_request_dispatch(mock_dispatch_event): + response = client.get( + f"/api/events/dispatch/test", + ) + response_json = response.json() + assert response_json == { + "data": None, + "success": True, + "error_code": 0, + "message": "Scheduled event dispatch!", + } + + args, kwargs = mock_dispatch_event.call_args + event = args[1] + assert event.code == EVENT_JOB_ADDED + assert event.job_id == 'test' + assert event.jobstore == 'default' + assert isinstance(args[1], JobEvent) + assert isinstance(args[0], BackgroundScheduler) + os_patch.stop() \ No newline at end of file