Skip to content

Commit 5af19fc

Browse files
committed
Fix command output
1 parent 1dabe81 commit 5af19fc

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

investing_algorithm_framework/core/workers/worker.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import logging
12
from abc import abstractmethod, ABC
23
from typing import Dict, Any
4+
from wrapt import synchronized
5+
from datetime import datetime
36

47
from investing_algorithm_framework.core.events.observable import Observable
58
from investing_algorithm_framework.core.events.observer import Observer
69

10+
logger = logging.getLogger('investing_algorithm_framework')
11+
712

813
class Worker(Observable, ABC):
914
"""
@@ -12,15 +17,19 @@ class Worker(Observable, ABC):
1217
"""
1318

1419
id = None
20+
last_run: datetime = None
1521

1622
def start(self, **kwargs: Dict[str, Any]) -> None:
1723
"""
1824
Function that will start the worker, and notify its observers when
1925
it is finished
2026
"""
2127

28+
logger.info("Starting worker {}".format(self.get_id()))
2229
self.work(**kwargs)
2330
self.notify_observers()
31+
self.update_last_run()
32+
logger.info("Worker {} finished".format(self.get_id()))
2433

2534
@abstractmethod
2635
def work(self, **kwargs: Dict[str, Any]) -> None:
@@ -42,3 +51,17 @@ def get_id(self) -> str:
4251
)
4352

4453
return getattr(self, 'id')
54+
55+
@classmethod
56+
@synchronized
57+
def update_last_run(cls) -> None:
58+
"""
59+
Update last run, this function is synchronized, which means that
60+
different instances can update the last_run attribute from different
61+
threads.
62+
"""
63+
cls.last_run = datetime.now()
64+
65+
@classmethod
66+
def get_last_run(cls):
67+
return cls.last_run

investing_algorithm_framework/management/command.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def execute(self, *args, **options) -> Any:
8989
if response is None and self.success_message is not None:
9090
return self.success_message
9191

92+
return response
93+
9294
@abstractmethod
9395
def handle(self, *args, **options) -> Any:
9496
"""

investing_algorithm_framework/management/command_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def execute(self) -> None:
4242
]:
4343
response = self.fetch_command(sub_command).\
4444
run_from_argv(self.argv)
45+
response = format_success_message(response)
4546
else:
4647
# Help for sub command
4748
if len(self.argv) > 2:
@@ -55,10 +56,12 @@ def execute(self) -> None:
5556
self.argv[2] = option
5657
response = self.fetch_command(sub_command)\
5758
.run_from_argv(self.argv)
59+
response = format_success_message(response)
5860
else:
5961
# Show general help command
6062
command = HelpCommand()
6163
response = command.run_from_argv(self.argv)
64+
response = format_success_message(response)
6265
except Exception as e:
6366
response = format_error_message(str(e))
6467

investing_algorithm_framework/management/commands/create_standard_algo.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111

1212
class CreateStandardAlgorithmCommand(BaseCommand):
13-
help = (
14-
"Creates a project directory structure for the given "
15-
"investing_algorithm_framework instance in the current "
16-
"directory or optionally in the given directory."
17-
)
13+
help = "Creates a project directory structure for the given " \
14+
"investing_algorithm_framework instance in the current " \
15+
"directory or optionally in the given directory."
1816

1917
missing_args_message = "You must provide a project name."
2018
success_message = "Algorithm created and initialized."
@@ -42,7 +40,7 @@ def handle(self, **options) -> None:
4240
raise ImproperlyConfigured(
4341
"Directory {} already exists. Please make sure that "
4442
"the project name does not correspond to an existing "
45-
"directory".format(str(directory))
43+
"directory.".format(str(directory))
4644
)
4745

4846
os.mkdir(directory)
@@ -71,13 +69,13 @@ def validate_name(name: str) -> None:
7169
"""
7270

7371
if name is None:
74-
raise CommandError("you must provide a project name")
72+
raise CommandError("you must provide a project name.")
7573

7674
if not re.match("^[a-zA-Z_.-]+$", name):
7775
raise CommandError(
7876
"{} is not allowed, value must begin with a letter and "
7977
"only contains the characters of A-Z, "
80-
"a-z and _".format(name)
78+
"a-z and _ .".format(name)
8179
)
8280

8381
# Make sure it can't be imported

investing_algorithm_framework/management/commands/run_algorithm.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88

99

1010
class RunAlgorithmCommand(BaseCommand):
11-
help_message = (
12-
"Runs a instance of an algorithm created with the "
13-
"investing_algorithm_framework, by default it will run "
14-
"until stopped, if cycles is specified it will run the according "
15-
"to the amount of cycles"
16-
)
17-
18-
success_message = (
19-
"Algorithm is finished running"
20-
)
11+
help_message = "Runs an instance of an algorithm created with the " \
12+
"investing_algorithm_framework."
13+
14+
success_message = "Algorithm is finished running."
2115

2216
def add_arguments(self, parser) -> None:
2317
pass

0 commit comments

Comments
 (0)