Skip to content

Commit

Permalink
Adjust methods params
Browse files Browse the repository at this point in the history
  • Loading branch information
hroff-1902 committed Feb 21, 2020
1 parent e0800b7 commit 881f602
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions freqtrade/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, args: Dict[str, Any], config: Dict[str, Any] = None) -> None:
self._config = config
self._init(False)

self.last_throttle_start_time: float = None
self.last_throttle_start_time: Optional[float] = None

# Tell systemd that we completed initialization phase
if self._sd_notify:
Expand Down Expand Up @@ -65,15 +65,13 @@ def run(self) -> None:
if state == State.RELOAD_CONF:
self._reconfigure()

def _worker(self, old_state: Optional[State], throttle_secs: Optional[float] = None) -> State:
def _worker(self, old_state: Optional[State]) -> State:
"""
Trading routine that must be run at each loop
:param old_state: the previous service state from the previous call
:return: current service state
"""
state = self.freqtrade.state
if throttle_secs is None:
throttle_secs = self._throttle_secs

# Log state transition
if state != old_state:
Expand All @@ -89,31 +87,31 @@ def _worker(self, old_state: Optional[State], throttle_secs: Optional[float] = N
logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: STOPPED.")
self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: STOPPED.")

self._throttle(func=self._process_stopped, min_secs=throttle_secs)
self._throttle(func=self._process_stopped, throttle_secs=self._throttle_secs)

elif state == State.RUNNING:
# Ping systemd watchdog before throttling
if self._sd_notify:
logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: RUNNING.")
self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: RUNNING.")

self._throttle(func=self._process_running, min_secs=throttle_secs)
self._throttle(func=self._process_running, throttle_secs=self._throttle_secs)

return state

def _throttle(self, func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any:
def _throttle(self, func: Callable[..., Any], throttle_secs: float, *args, **kwargs) -> Any:
"""
Throttles the given callable that it
takes at least `min_secs` to finish execution.
:param func: Any callable
:param min_secs: minimum execution time in seconds
:return: Any
:param throttle_secs: throttling interation execution time limit in seconds
:return: Any (result of execution of func)
"""
self.last_throttle_start_time = time.time()
logger.debug("========================================")
result = func(*args, **kwargs)
time_passed = time.time() - self.last_throttle_start_time
sleep_duration = max(min_secs - time_passed, 0.0)
sleep_duration = max(throttle_secs - time_passed, 0.0)
logger.debug(f"Throttling with '{func.__name__}()': sleep for {sleep_duration:.2f} s, "
f"last iteration took {time_passed:.2f} s.")
time.sleep(sleep_duration)
Expand Down

0 comments on commit 881f602

Please sign in to comment.