Skip to content

Commit

Permalink
Merge pull request #91 from PerfectFit-project/344-periodic-tasks
Browse files Browse the repository at this point in the history
changed dialog checking and new day to periodic tasks
  • Loading branch information
wbaccinelli authored Jun 2, 2023
2 parents a9dad68 + 5d580c4 commit 00f7e61
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions scheduler/celery_tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logging
import os
import requests
from celery import Celery
from celery.signals import worker_ready
from celery.schedules import crontab
from datetime import date, datetime, timedelta
from state_machine.state_machine import EventEnum, Event
from state_machine.const import (REDIS_URL, TIMEZONE, MAXIMUM_DIALOG_DURATION,
Expand All @@ -16,18 +15,15 @@
app.conf.enable_utc = True
app.conf.timezone = TIMEZONE

TEST_USER = int(os.getenv('TEST_USER_ID'))


@worker_ready.connect
def at_start(sender, **k): # pylint: disable=unused-argument
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs): # pylint: disable=unused-argument
"""
When celery is ready, the watchdogs for the new day notification
and for the dialogs status check are started
"""
notify_new_day.apply_async(args=[datetime.today()])
## TODO: uncomment and solve issue
# check_dialogs_status.apply_async()
sender.add_periodic_task(crontab(hour=00, minute=00), notify_new_day.s(datetime.today()))
sender.add_periodic_task(MAXIMUM_DIALOG_DURATION, check_dialogs_status.s())


@app.task
Expand Down Expand Up @@ -68,16 +64,12 @@ def check_dialogs_status(self): # pylint: disable=unused-argument
fsm.dialog_state.set_to_idle()
save_state_machine_to_db(fsm)

next_day = datetime.now().replace(hour=10, minute=00) + timedelta(days=1)
next_day = datetime.now().replace(hour=00, minute=00) + timedelta(days=1)

reschedule_dialog.apply_async(args=[fsm.machine_id,
dialog,
next_day])

# schedule the task every max_dialog_duration
next_execution_time = datetime.now() + timedelta(seconds=MAXIMUM_DIALOG_DURATION)
check_dialogs_status.apply_async(eta=next_execution_time)


@app.task(bind=True)
def intervention_component_completed(self, # pylint: disable=unused-argument
Expand Down Expand Up @@ -106,10 +98,6 @@ def notify_new_day(self, current_date: date): # pylint: disable=unused-argument
for item in state_machines:
send_fsm_event(user_id=item.machine_id, event=Event(EventEnum.NEW_DAY, current_date))

# schedule the task for tomorrow
# tomorrow = datetime.today() + timedelta(days=1)
# notify_new_day.apply_async(args=[tomorrow], eta=tomorrow)


@app.task
def reschedule_dialog(user_id: int, intervention_component_name: str, new_date: datetime):
Expand Down

0 comments on commit 00f7e61

Please sign in to comment.