Skip to content

Commit 9cdef7a

Browse files
author
Sergio García Prado
committed
ISSUE #306
* Add `CronTab` class.
1 parent acbff0e commit 9cdef7a

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

packages/core/minos-microservice-networks/minos/networks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
Router,
118118
)
119119
from .scheduling import (
120+
CronTab,
120121
PeriodicTask,
121122
PeriodicTaskScheduler,
122123
PeriodicTaskSchedulerPort,

packages/core/minos-microservice-networks/minos/networks/decorators/definitions/periodic.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1+
from __future__ import (
2+
annotations,
3+
)
4+
15
from abc import (
26
ABC,
37
)
48
from collections.abc import (
59
Iterable,
610
)
711
from typing import (
12+
TYPE_CHECKING,
813
Final,
914
Union,
1015
)
1116

12-
from crontab import (
13-
CronTab,
14-
)
15-
1617
from .abc import (
1718
EnrouteDecorator,
1819
)
1920
from .kinds import (
2021
EnrouteDecoratorKind,
2122
)
2223

24+
if TYPE_CHECKING:
25+
from crontab import CronTab as CronTabImpl
26+
27+
from ...scheduling import (
28+
CronTab,
29+
)
30+
2331

2432
class PeriodicEnrouteDecorator(EnrouteDecorator, ABC):
2533
"""Periodic Enroute class"""
2634

27-
def __init__(self, crontab: Union[str, CronTab]):
28-
if isinstance(crontab, str):
35+
def __init__(self, crontab: Union[str, CronTab, CronTabImpl]):
36+
from ...scheduling import (
37+
CronTab,
38+
)
39+
40+
if not isinstance(crontab, CronTab):
2941
crontab = CronTab(crontab)
3042
self.crontab = crontab
3143

3244
def __iter__(self) -> Iterable:
33-
yield from (self.crontab.matchers,)
45+
yield from (self.crontab,)
3446

3547

3648
class PeriodicEventEnrouteDecorator(PeriodicEnrouteDecorator):

packages/core/minos-microservice-networks/minos/networks/scheduling/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from .crontab import (
2+
CronTab,
3+
)
14
from .ports import (
25
PeriodicTaskSchedulerPort,
36
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import (
2+
annotations,
3+
)
4+
5+
from typing import (
6+
Any,
7+
Union,
8+
)
9+
10+
from crontab import CronTab as CrontTabImpl
11+
12+
13+
class CronTab:
14+
"""TODO"""
15+
16+
def __init__(self, pattern: Union[str, CrontTabImpl]):
17+
if not isinstance(pattern, CrontTabImpl):
18+
pattern = CrontTabImpl(pattern)
19+
self.impl = pattern
20+
21+
def __hash__(self):
22+
return hash(self.impl.matchers)
23+
24+
def __eq__(self, other: Any) -> bool:
25+
return isinstance(other, type(self)) and self.impl.matchers == other.impl.matchers
26+
27+
def next(self, *args, **kwargs):
28+
"""TODO"""
29+
self.impl.next(*args, **kwargs)

packages/core/minos-microservice-networks/minos/networks/scheduling/schedulers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
Union,
2222
)
2323

24-
from crontab import (
25-
CronTab,
26-
)
24+
from crontab import CronTab as CronTabImpl
2725

2826
from minos.common import (
2927
Config,
@@ -37,6 +35,9 @@
3735
from ..requests import (
3836
ResponseException,
3937
)
38+
from .crontab import (
39+
CronTab,
40+
)
4041
from .requests import (
4142
ScheduledRequest,
4243
)
@@ -93,8 +94,8 @@ class PeriodicTask:
9394

9495
_task: Optional[asyncio.Task]
9596

96-
def __init__(self, crontab: Union[str, CronTab], fn: Callable[[ScheduledRequest], Awaitable[None]]):
97-
if isinstance(crontab, str):
97+
def __init__(self, crontab: Union[str, CronTab, CronTabImpl], fn: Callable[[ScheduledRequest], Awaitable[None]]):
98+
if not isinstance(crontab, CronTab):
9899
crontab = CronTab(crontab)
99100

100101
self._crontab = crontab

packages/core/minos-microservice-networks/tests/test_networks/test_scheduling/test_schedulers.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
patch,
99
)
1010

11-
from crontab import (
12-
CronTab,
13-
)
14-
1511
from minos.common import (
1612
Config,
1713
current_datetime,
1814
)
1915
from minos.networks import (
16+
CronTab,
2017
PeriodicTask,
2118
PeriodicTaskScheduler,
2219
ScheduledRequest,
@@ -81,7 +78,7 @@ def setUp(self) -> None:
8178
self.periodic = PeriodicTask("@daily", self.fn_mock)
8279

8380
def test_crontab(self) -> None:
84-
self.assertEqual(CronTab("@daily").matchers, self.periodic.crontab.matchers)
81+
self.assertEqual(CronTab("@daily"), self.periodic.crontab)
8582

8683
def test_fn(self) -> None:
8784
self.assertEqual(self.fn_mock, self.periodic.fn)

0 commit comments

Comments
 (0)