Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions pycron/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from croniter import croniter
from dataclasses import dataclass
from datetime import datetime
from datetime import datetime, timezone
from threading import Thread
import time
from types import FunctionType
Expand Down Expand Up @@ -46,13 +46,7 @@ class ScheduledFunc:
def _add_scheduled_func(function_def: FunctionType, cron_str: str) -> None:
global scheduled_functions

scheduled_functions.append(
ScheduledFunc(
function_def,
cron_str,
datetime.utcnow().timestamp()
)
)
scheduled_functions.append(ScheduledFunc(function_def, cron_str, datetime.now(timezone.utc).timestamp()))

def cron(cron_str: str) -> None:
# we don't actually need to wrap the function, so just return it from
Expand Down Expand Up @@ -81,7 +75,7 @@ def start() -> None:

# determine the next time the function should run as specified by its cron string
next_run_timestamp: int = croniter(scheduled_function.cron_str, last_run).get_next(float)

# check if the function should run based on whether or not the current timestamp
# meets or exceeds the next run timestamp
current_timestamp: float = datetime.now().timestamp()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not here be current_timestamp also be UTC ?

on line 88(82) ur are comparing current_timestamp (which is not UTC) with scheduled_function.last_run (with is UTC) + cron delta. looks like bug, no?)

Expand All @@ -95,25 +89,23 @@ def start() -> None:
running_functions[-1].start()

# set the last run timestamp of this function to the current UTC time
scheduled_function.last_run = datetime.utcnow().timestamp()
scheduled_function.last_run = datetime.now(timezone.utc).timestamp()

# remove any threads that are done
for n in range(len(running_functions) - 1, -1, -1):
if not running_functions[n].running:
del running_functions[n]

# sleep so that this method only pulses every 1 second at the most
end_time: float = time.time()
if end_time - start_time < 1.0:
time.sleep(1 - (end_time - start_time))

# join all of the threads that we've created until they're done
for n in range(len(running_functions) - 1, -1, -1):
if not running_functions[n].running:
del running_functions[n]
else:
if running_functions[n].running:
running_functions[n].join()
del running_functions[n]
del running_functions[n]

def stop() -> None:
global _running
Expand Down