Skip to content

Commit

Permalink
0.0.7: Fixes for Sunrise/Sunset
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Apr 15, 2021
1 parent 30c70b3 commit 28b7070
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/eascheduler/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.6'
__version__ = '0.0.7'
4 changes: 3 additions & 1 deletion src/eascheduler/jobs/job_sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def _update_run_time(self, dt_start: Optional[DateTime] = None) -> DateTime:

# Allow skipping certain occurrences
dt_next = dt_start if dt_start is not None else get_now(tz=local_tz)
next_run = update_run_time(dt_next)
next_run = pd_instance(self._sun_func(OBSERVER, dt_next.date(), tzinfo=local_tz))
next_run = update_run_time(next_run.set(microsecond=0))

while next_run is SKIP_EXECUTION:
dt_next = dt_next.add(days=1)
next_run = pd_instance(self._sun_func(OBSERVER, dt_next.add(days=1).date(), tzinfo=local_tz))
Expand Down
9 changes: 5 additions & 4 deletions src/eascheduler/schedulers/scheduler_asyncthread.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from asyncio import run_coroutine_threadsafe, AbstractEventLoop, current_task
from asyncio import AbstractEventLoop, run_coroutine_threadsafe
from threading import _MainThread, current_thread

from eascheduler.jobs.job_base import ScheduledJobBase
from eascheduler.schedulers import AsyncScheduler
Expand All @@ -14,7 +15,7 @@ async def __add_job(self, job: ScheduledJobBase):
super().add_job(job)

def add_job(self, job: ScheduledJobBase):
if current_task(self.LOOP) is None:
if not isinstance(current_thread(), _MainThread):
run_coroutine_threadsafe(self.__add_job(job), self.LOOP).result()
else:
super().add_job(job)
Expand All @@ -24,7 +25,7 @@ async def __remove_job(self, job: ScheduledJobBase):
super().remove_job(job)

def remove_job(self, job: ScheduledJobBase):
if current_task(self.LOOP) is None:
if not isinstance(current_thread(), _MainThread):
run_coroutine_threadsafe(self.__remove_job(job), self.LOOP).result()
else:
super().remove_job(job)
Expand All @@ -34,7 +35,7 @@ async def __cancel_all(self):
super().cancel_all()

def cancel_all(self):
if current_task(self.LOOP) is None:
if not isinstance(current_thread(), _MainThread):
run_coroutine_threadsafe(self.__cancel_all(), self.LOOP).result()
else:
super().cancel_all()
Expand Down
25 changes: 24 additions & 1 deletion tests/jobs/test_job_sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pendulum import from_timestamp

from eascheduler import set_location
from eascheduler.const import SKIP_EXECUTION
from eascheduler.const import SKIP_EXECUTION, local_tz
from eascheduler.executors import SyncExecutor
from eascheduler.jobs import SunriseJob, SunsetJob, DawnJob, DuskJob
from eascheduler.schedulers import AsyncScheduler
Expand Down Expand Up @@ -63,3 +63,26 @@ def skip_func(dt: datetime):
assert from_timestamp(j._next_run).naive() == datetime(2001, 1, 3, 7, 15, 16)

s.cancel_all()


@pytest.mark.asyncio
async def test_calc_advance():
set_location(52.5185537, 13.3758636, 43)
set_now(2001, 1, 1, 12)
s = AsyncScheduler()

j = SunriseJob(s, SyncExecutor(lambda x: 1 / 0))
s.add_job(j)
j._update_base_time()
assert from_timestamp(j._next_run).naive() == datetime(2001, 1, 2, 7, 15, 29)
j.cancel()

j = SunsetJob(s, SyncExecutor(lambda x: 1 / 0))
s.add_job(j)
j._update_base_time()
assert from_timestamp(j._next_run).naive() == datetime(2001, 1, 1, 15, 4, 48)

j.latest(time(15))
assert from_timestamp(j._next_run).in_tz(local_tz).naive() == datetime(2001, 1, 1, 15, 0, 0)

j.cancel()

0 comments on commit 28b7070

Please sign in to comment.