Skip to content

Commit

Permalink
Merge pull request #39 from st31ny/cronsim
Browse files Browse the repository at this point in the history
switch to cronsim for cron parsing
  • Loading branch information
gawel authored Dec 31, 2024
2 parents a94c8bf + 95ccbad commit 39f40de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
18 changes: 7 additions & 11 deletions aiocron/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from croniter.croniter import croniter
from cronsim import CronSim
from datetime import datetime
from functools import wraps, partial
from tzlocal import get_localzone
Expand Down Expand Up @@ -38,7 +38,6 @@ def __init__(
uuid=None,
loop=None,
tz=None,
croniter_kwargs=None,
):
self.spec = spec
if func is not None:
Expand All @@ -50,11 +49,10 @@ def __init__(
self.cron = wrap_func(self.func)
self.auto_start = start
self.uuid = uuid if uuid is not None else uuid4()
self.handle = self.future = self.croniter = None
self.handle = self.future = self.cronsim = None
self.loop = loop if loop is not None else asyncio.get_event_loop()
if start and self.func is not null_callback:
self.handle = self.loop.call_soon_threadsafe(self.start)
self.croniter_kwargs = croniter_kwargs or {}

def start(self):
"""Start scheduling"""
Expand All @@ -66,7 +64,7 @@ def stop(self):
"""Stop scheduling"""
if self.handle is not None:
self.handle.cancel()
self.handle = self.future = self.croniter = None
self.handle = self.future = self.cronsim = None

async def next(self, *args):
"""yield from .next()"""
Expand All @@ -76,18 +74,16 @@ async def next(self, *args):
return await self.future

def initialize(self):
"""Initialize croniter and related times"""
if self.croniter is None:
"""Initialize cronsim and related times"""
if self.cronsim is None:
self.time = time.time()
self.datetime = datetime.now(self.tz)
self.loop_time = self.loop.time()
self.croniter = croniter(
self.spec, start_time=self.datetime, **self.croniter_kwargs
)
self.cronsim = CronSim(self.spec, self.datetime)

def get_next(self):
"""Return next iteration time related to loop time"""
return self.loop_time + (self.croniter.get_next(float) - self.time)
return self.loop_time + (next(self.cronsim).timestamp() - self.time)

def call_next(self):
"""Set next hop in the loop. Call task"""
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ classifiers = [
]
keywords = ["crontab", "cron", "asyncio"]
dependencies = [
"croniter>=3.0.3",
"cronsim>=2.6",
"python-dateutil>=2.9.0",
"tox>=4.21.1",
"tox-uv>=1.13.0",
"tzlocal>=5.2",
Expand Down
16 changes: 8 additions & 8 deletions tests/test_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CustomError(Exception):
def test_str():
loop = asyncio.new_event_loop()

@crontab("* * * * * *", loop=loop)
@crontab("* * * * *", loop=loop)
def t():
pass

Expand All @@ -26,7 +26,7 @@ def test_cron():

future = asyncio.Future(loop=loop)

@crontab("* * * * * *", start=False, loop=loop)
@crontab("* * * * *", start=False, loop=loop)
def t():
future.set_result(1)

Expand All @@ -41,7 +41,7 @@ def test_raise():

future = asyncio.Future(loop=loop)

@crontab("* * * * * *", start=False, loop=loop)
@crontab("* * * * *", start=False, loop=loop)
def t():
loop.call_later(1, future.set_result, 1)
raise ValueError()
Expand All @@ -58,7 +58,7 @@ def test_next():
def t():
return 1

t = crontab("* * * * * *", func=t, loop=loop)
t = crontab("* * * * *", func=t, loop=loop)

future = asyncio.ensure_future(t.next(), loop=loop)

Expand All @@ -69,7 +69,7 @@ def t():
def test_null_callback():
loop = asyncio.new_event_loop()

t = crontab("* * * * * *", loop=loop)
t = crontab("* * * * *", loop=loop)

assert t.handle is None # not started

Expand All @@ -82,7 +82,7 @@ def test_null_callback():
def test_next_raise():
loop = asyncio.new_event_loop()

@crontab("* * * * * *", loop=loop)
@crontab("* * * * *", loop=loop)
def t():
raise CustomError()

Expand All @@ -95,7 +95,7 @@ def t():
def test_coro_next():
loop = asyncio.new_event_loop()

@crontab("* * * * * *", loop=loop)
@crontab("* * * * *", loop=loop)
async def t():
return 1

Expand All @@ -108,7 +108,7 @@ async def t():
def test_coro_next_raise():
loop = asyncio.new_event_loop()

@crontab("* * * * * *", loop=loop)
@crontab("* * * * *", loop=loop)
async def t():
raise CustomError()

Expand Down

0 comments on commit 39f40de

Please sign in to comment.