Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/sleep schedule check if current sleep is over #4031

Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion configs/config.json.cluster.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"time": "22:54",
"duration":"7:46",
"time_random_offset": "00:24",
"duration_random_offset": "00:43"
"duration_random_offset": "00:43",
"skip_current_sleep_cycle": true
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"time": "22:54",
"duration":"7:46",
"time_random_offset": "00:24",
"duration_random_offset": "00:43"
"duration_random_offset": "00:43",
"skip_current_sleep_cycle": true
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion configs/config.json.map.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"time": "22:54",
"duration":"7:46",
"time_random_offset": "00:24",
"duration_random_offset": "00:43"
"duration_random_offset": "00:43",
"skip_current_sleep_cycle": true
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion configs/config.json.path.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"time": "22:54",
"duration":"7:46",
"time_random_offset": "00:24",
"duration_random_offset": "00:43"
"duration_random_offset": "00:43",
"skip_current_sleep_cycle": true
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"time": "22:54",
"duration":"7:46",
"time_random_offset": "00:24",
"duration_random_offset": "00:43"
"duration_random_offset": "00:43",
"skip_current_sleep_cycle": true
}
},
{
Expand Down
21 changes: 19 additions & 2 deletions pokemongo_bot/cell_workers/sleep_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ def initialize(self):
# self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))
self._process_config()
self._schedule_next_sleep()
self._calculate_current_sleep()

def work(self):
if datetime.now() >= self._next_sleep:
if self._should_sleep_now():
self._sleep()
self._schedule_next_sleep()
self.bot.login()
Expand All @@ -60,6 +61,8 @@ def _process_config(self):
timedelta(
hours=duration_random_offset.hour, minutes=duration_random_offset.minute).total_seconds())

self.skip_current_sleep_cycle = self.config.get('skip_current_sleep_cycle', True)
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't change during work() set it in initialize()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is in _process_config(), not work() :)

Copy link
Contributor

Choose a reason for hiding this comment

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

doh! skimmed over the @@ -60,6 +61,8 @@ def _process_config(self): Sorry! Carry on ;)


def _schedule_next_sleep(self):
self._next_sleep = self._get_next_sleep_schedule()
self._next_duration = self._get_next_duration()
Expand All @@ -71,6 +74,20 @@ def _schedule_next_sleep(self):
}
)

def _calculate_current_sleep(self):
current_sleep = self._next_sleep - timedelta(days=1)
current_duration = self._get_next_duration()
self._current_end = current_sleep + timedelta(seconds = current_duration)

def _should_sleep_now(self):
if datetime.now() >= self._next_sleep:
return True
if not self.skip_current_sleep_cycle and datetime.now() <= self._current_end:
self._next_duration = (self._current_end - datetime.now()).total_seconds()
return True

return False

def _get_next_sleep_schedule(self):
now = datetime.now() + self.SCHEDULING_MARGIN
next_time = now.replace(hour=self.time.hour, minute=self.time.minute)
Expand All @@ -95,7 +112,7 @@ def _sleep(self):
sleep_to_go = self._next_duration
self.emit_event(
'bot_sleep',
formatted="Sleeping for {time_in_seconds}",
formatted="Sleeping for {time_in_seconds} seconds...",
data={
'time_in_seconds': sleep_to_go
}
Expand Down