|
| 1 | +from datetime import datetime |
| 2 | +from collections import namedtuple |
| 3 | +from typing import Dict, List |
| 4 | + |
| 5 | +from investing_bot_framework.core.utils import TimeUnit |
| 6 | +from investing_bot_framework.core.exceptions import OperationalException |
| 7 | + |
| 8 | +ExecutionTask = namedtuple('ExecutionTask', 'time_unit interval last_run') |
| 9 | + |
| 10 | + |
| 11 | +class ExecutionScheduler: |
| 12 | + """ |
| 13 | + Class ExecutionScheduler: This is a lazy scheduler. It only runs it's scheduling algorithm when it is asked to. |
| 14 | + It will then evaluate all the time units and intervals and decide which executions it needs to return. |
| 15 | +
|
| 16 | + At the initial run, it will schedule all the executions. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + self._planning: Dict[str, ExecutionTask] = {} |
| 21 | + |
| 22 | + def add_execution_task(self, execution_id: str, time_unit: TimeUnit, interval: int = None) -> None: |
| 23 | + """ |
| 24 | + Function that will add an appointment to the scheduler |
| 25 | + """ |
| 26 | + |
| 27 | + if execution_id not in self._planning: |
| 28 | + |
| 29 | + if time_unit is not TimeUnit.ALWAYS: |
| 30 | + |
| 31 | + if interval is None: |
| 32 | + raise OperationalException("Appoint must set an interval with the corresponding time unit") |
| 33 | + elif interval < 1: |
| 34 | + raise OperationalException("Interval for task time unit is smaller then 1") |
| 35 | + |
| 36 | + self._planning[execution_id] = ExecutionTask(time_unit=time_unit, interval=interval, last_run=None) |
| 37 | + |
| 38 | + else: |
| 39 | + raise OperationalException("Can't add execution task, execution id is already taken") |
| 40 | + |
| 41 | + def schedule_executions(self) -> List[str]: |
| 42 | + """ |
| 43 | + Function that will return all appointments that have hit their time threshold |
| 44 | + """ |
| 45 | + |
| 46 | + appointments: List[str] = [] |
| 47 | + |
| 48 | + for appointment_id in self._planning: |
| 49 | + |
| 50 | + # Run at the first call |
| 51 | + if self._planning[appointment_id].last_run is None: |
| 52 | + appointments.append(appointment_id) |
| 53 | + |
| 54 | + # Executions that always need to be scheduled |
| 55 | + elif self._planning[appointment_id].time_unit is TimeUnit.ALWAYS: |
| 56 | + appointments.append(appointment_id) |
| 57 | + |
| 58 | + else: |
| 59 | + |
| 60 | + # Get the current time |
| 61 | + now = datetime.now() |
| 62 | + |
| 63 | + # More seconds passed then the given interval |
| 64 | + if self._planning[appointment_id].time_unit is TimeUnit.SECOND: |
| 65 | + last_run = self._planning[appointment_id].last_run |
| 66 | + elapsed_time = now - last_run |
| 67 | + seconds = elapsed_time.total_seconds() |
| 68 | + |
| 69 | + if seconds >= self._planning[appointment_id].interval: |
| 70 | + appointments.append(appointment_id) |
| 71 | + |
| 72 | + # More minutes passed then the given interval |
| 73 | + if self._planning[appointment_id].time_unit is TimeUnit.MINUTE: |
| 74 | + last_run = self._planning[appointment_id].last_run |
| 75 | + elapsed_time = now - last_run |
| 76 | + minutes = divmod(elapsed_time.total_seconds(), 60) |
| 77 | + |
| 78 | + if minutes[0] >= self._planning[appointment_id].interval: |
| 79 | + appointments.append(appointment_id) |
| 80 | + |
| 81 | + # More hours run then the given interval |
| 82 | + elif self._planning[appointment_id].time_unit is TimeUnit.HOUR: |
| 83 | + last_run = self._planning[appointment_id].last_run |
| 84 | + elapsed_time = now - last_run |
| 85 | + hours = divmod(elapsed_time.total_seconds(), 3600) |
| 86 | + |
| 87 | + if hours[0] >= self._planning[appointment_id].interval: |
| 88 | + appointments.append(appointment_id) |
| 89 | + |
| 90 | + for appointment in appointments: |
| 91 | + self._planning[appointment] = ExecutionTask( |
| 92 | + self._planning[appointment].time_unit, |
| 93 | + self._planning[appointment].interval, |
| 94 | + datetime.now() |
| 95 | + ) |
| 96 | + |
| 97 | + return appointments |
0 commit comments