-
Notifications
You must be signed in to change notification settings - Fork 1
Dev/minimal flow #16
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
Merged
Merged
Dev/minimal flow #16
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6cf6715
Added minimal Run classes (#6)
Raalsky b85f1c5
Added `OperationsQueue` component (#7)
Raalsky 2faf3a5
Logging metadata (#8)
Raalsky 4c91b15
Run creation and basic data synchronization (#9)
Raalsky 67f63cb
Added support for env variables for project and api token (#11)
Raalsky 6e4ada2
Splitting metadata into multiple messages on `log` (#12)
Raalsky d8098f9
Added ErrorsMonitor and ErrorsQueue (#13)
Raalsky e35876c
Added support for family parameter (#14)
Raalsky cceddab
Code review
Raalsky 04a7dce
Code review 2
Raalsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
__all__ = ["Daemon"] | ||
|
||
import abc | ||
import threading | ||
from enum import Enum | ||
|
||
|
||
class Daemon(threading.Thread): | ||
class DaemonState(Enum): | ||
INIT = 1 | ||
WORKING = 2 | ||
PAUSING = 3 | ||
PAUSED = 4 | ||
INTERRUPTED = 5 | ||
STOPPED = 6 | ||
|
||
def __init__(self, sleep_time: float, name: str) -> None: | ||
super().__init__(daemon=True, name=name) | ||
self._sleep_time = sleep_time | ||
self._state: Daemon.DaemonState = Daemon.DaemonState.INIT | ||
self._wait_condition = threading.Condition() | ||
|
||
def interrupt(self) -> None: | ||
with self._wait_condition: | ||
self._state = Daemon.DaemonState.INTERRUPTED | ||
self._wait_condition.notify_all() | ||
|
||
def pause(self) -> None: | ||
with self._wait_condition: | ||
if self._state != Daemon.DaemonState.PAUSED: | ||
if not self._is_interrupted(): | ||
self._state = Daemon.DaemonState.PAUSING | ||
self._wait_condition.notify_all() | ||
self._wait_condition.wait_for(lambda: self._state != Daemon.DaemonState.PAUSING) | ||
|
||
def resume(self) -> None: | ||
with self._wait_condition: | ||
if not self._is_interrupted(): | ||
self._state = Daemon.DaemonState.WORKING | ||
self._wait_condition.notify_all() | ||
|
||
def wake_up(self) -> None: | ||
with self._wait_condition: | ||
self._wait_condition.notify_all() | ||
|
||
def disable_sleep(self) -> None: | ||
self._sleep_time = 0 | ||
|
||
def is_running(self) -> bool: | ||
with self._wait_condition: | ||
return self._state in ( | ||
Daemon.DaemonState.WORKING, | ||
Daemon.DaemonState.PAUSING, | ||
Daemon.DaemonState.PAUSED, | ||
) | ||
|
||
def _is_interrupted(self) -> bool: | ||
with self._wait_condition: | ||
return self._state in (Daemon.DaemonState.INTERRUPTED, Daemon.DaemonState.STOPPED) | ||
|
||
def run(self) -> None: | ||
with self._wait_condition: | ||
if not self._is_interrupted(): | ||
self._state = Daemon.DaemonState.WORKING | ||
try: | ||
while not self._is_interrupted(): | ||
with self._wait_condition: | ||
if self._state == Daemon.DaemonState.PAUSING: | ||
self._state = Daemon.DaemonState.PAUSED | ||
self._wait_condition.notify_all() | ||
self._wait_condition.wait_for(lambda: self._state != Daemon.DaemonState.PAUSED) | ||
|
||
if self._state == Daemon.DaemonState.WORKING: | ||
self.work() | ||
with self._wait_condition: | ||
if self._sleep_time > 0 and self._state == Daemon.DaemonState.WORKING: | ||
self._wait_condition.wait(timeout=self._sleep_time) | ||
finally: | ||
with self._wait_condition: | ||
self._state = Daemon.DaemonState.STOPPED | ||
self._wait_condition.notify_all() | ||
|
||
@abc.abstractmethod | ||
def work(self) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
__all__ = ("ErrorsMonitor",) | ||
|
||
import logging | ||
import queue | ||
from typing import Callable | ||
|
||
from neptune_scale.core.components.abstract import Resource | ||
from neptune_scale.core.components.daemon import Daemon | ||
from neptune_scale.core.components.errors_queue import ErrorsQueue | ||
|
||
logger = logging.getLogger("neptune") | ||
logger.setLevel(level=logging.INFO) | ||
|
||
|
||
def on_error(error: BaseException) -> None: | ||
logger.error(error) | ||
kgodlewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class ErrorsMonitor(Daemon, Resource): | ||
def __init__( | ||
self, | ||
errors_queue: ErrorsQueue, | ||
on_error_callback: Callable[[BaseException], None] = on_error, | ||
): | ||
super().__init__(name="ErrorsMonitor", sleep_time=2) | ||
self._errors_queue = errors_queue | ||
self._on_error_callback = on_error_callback | ||
|
||
def work(self) -> None: | ||
try: | ||
error = self._errors_queue.get(block=False) | ||
if error is not None: | ||
self._on_error_callback(error) | ||
except KeyboardInterrupt: | ||
with self._wait_condition: | ||
self._wait_condition.notify_all() | ||
raise | ||
except queue.Empty: | ||
pass | ||
|
||
def cleanup(self) -> None: | ||
pass | ||
|
||
def close(self) -> None: | ||
self.interrupt() | ||
self.join(timeout=10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
__all__ = ("ErrorsQueue",) | ||
|
||
from multiprocessing import Queue | ||
|
||
from neptune_scale.core.components.abstract import Resource | ||
|
||
|
||
class ErrorsQueue(Resource): | ||
def __init__(self) -> None: | ||
self._errors_queue: Queue[BaseException] = Queue() | ||
kgodlewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def put(self, error: BaseException) -> None: | ||
self._errors_queue.put(error) | ||
|
||
def get(self, block: bool = True, timeout: float | None = None) -> BaseException: | ||
return self._errors_queue.get(block=block, timeout=timeout) | ||
|
||
def cleanup(self) -> None: | ||
pass | ||
|
||
def close(self) -> None: | ||
self._errors_queue.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from unittest.mock import Mock | ||
|
||
from neptune_scale.core.components.errors_monitor import ErrorsMonitor | ||
from neptune_scale.core.components.errors_queue import ErrorsQueue | ||
|
||
|
||
def test_errors_monitor(): | ||
# given | ||
callback = Mock() | ||
|
||
# and | ||
errors_queue = ErrorsQueue() | ||
errors_monitor = ErrorsMonitor(errors_queue=errors_queue, on_error_callback=callback) | ||
|
||
# when | ||
errors_queue.put(ValueError("error1")) | ||
errors_monitor.start() | ||
errors_monitor.interrupt() | ||
errors_monitor.join(timeout=1) | ||
|
||
# then | ||
callback.assert_called() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.