Skip to content

Commit

Permalink
added dispatch functionality for scheduler (#1447)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfahad1414 authored Aug 22, 2024
1 parent 9675380 commit 552f731
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions kairon/events/scheduler/kscheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
6 changes: 6 additions & 0 deletions kairon/events/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"}
24 changes: 24 additions & 0 deletions tests/integration_test/event_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

0 comments on commit 552f731

Please sign in to comment.