Skip to content

Commit

Permalink
Merge pull request #53 from datmo/issue-36
Browse files Browse the repository at this point in the history
Added check for status if task is already run
  • Loading branch information
asampat3090 committed May 1, 2018
2 parents 4773738 + 2b5e0cc commit d9e9b58
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 74 deletions.
11 changes: 9 additions & 2 deletions datmo/core/controller/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def run(self, task_id, snapshot_dict=None, task_dict=None):
of whether the user provides another value for `visible`.
task_dict : dict
set of parameters to characterize the task run
(default is None, which translate to {}, see datmo.core.entity.task for more details on inputs)
(default is None, which translate to {}, see datmo.core.entity.task.Task for more details on inputs)
Returns
-------
Expand All @@ -211,6 +211,13 @@ def run(self, task_id, snapshot_dict=None, task_dict=None):
# Obtain Task to run
task_obj = self.dal.task.get_by_id(task_id)

if task_obj.status==None:
task_obj.status = 'RUNNING'
else:
raise TaskRunException(__("error",
"cli.task.run.already_running",
task_obj.id))

# Create Task directory for user during run
task_dirpath = os.path.join("datmo_tasks", task_obj.id)
try:
Expand Down Expand Up @@ -249,7 +256,7 @@ def run(self, task_id, snapshot_dict=None, task_dict=None):
# Set the parameters set in the task
environment_run_options = {
"command": task_obj.command,
"ports": task_obj.ports,
"ports": [] if task_obj.ports is None else task_obj.ports,
"gpu": task_obj.gpu,
"name": "datmo-task-" + task_obj.id,
"volumes": {
Expand Down
71 changes: 44 additions & 27 deletions datmo/core/controller/test/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from datmo.core.controller.environment.environment import EnvironmentController
from datmo.core.controller.task import TaskController
from datmo.core.util.exceptions import EntityNotFound, \
EnvironmentExecutionException
EnvironmentExecutionException, TaskRunException


class TestTaskController():
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_run(self):
assert task_obj.id == updated_task_obj.id

assert updated_task_obj.before_snapshot_id
assert updated_task_obj.ports == []
assert updated_task_obj.ports == None
assert updated_task_obj.gpu == False
assert updated_task_obj.interactive == False
assert updated_task_obj.task_dirpath
Expand All @@ -194,7 +194,7 @@ def test_run(self):
failed = False
try:
self.task.run(task_obj.id)
except EnvironmentExecutionException:
except TaskRunException:
failed = True
assert failed

Expand All @@ -217,38 +217,55 @@ def test_run(self):
try:
self.task.run(task_obj.id,
snapshot_dict=snapshot_dict)
except EnvironmentExecutionException:
except TaskRunException:
failed = True
assert failed

# Test when the specific task id is already RUNNING
# Create task in the project
task_obj_1 = self.task.create(input_dict)
self.task.dal.task.update({"id": task_obj_1.id, "status": "RUNNING"})
# Create environment_driver definition
env_def_path = os.path.join(self.project.home,
"Dockerfile")
with open(env_def_path, "w") as f:
f.write(to_unicode(str("FROM datmo/xgboost:cpu")))

failed = False
try:
self.task.run(task_obj_1.id)
except TaskRunException:
failed = True
assert failed

# 4) Test option 4

# Create a new task in the project
task_obj_1 = self.task.create(input_dict)
task_obj_2 = self.task.create(input_dict)

# Run another task in the project
updated_task_obj_1 = self.task.run(task_obj_1.id,
updated_task_obj_2 = self.task.run(task_obj_2.id,
snapshot_dict=snapshot_dict)

assert task_obj_1.id == updated_task_obj_1.id

assert updated_task_obj_1.before_snapshot_id
assert updated_task_obj_1.ports == []
assert updated_task_obj_1.gpu == False
assert updated_task_obj_1.interactive == False
assert updated_task_obj_1.task_dirpath
assert updated_task_obj_1.log_filepath
assert updated_task_obj_1.start_time

assert updated_task_obj_1.after_snapshot_id
assert updated_task_obj_1.run_id
assert updated_task_obj_1.logs
assert "accuracy" in updated_task_obj_1.logs
assert updated_task_obj_1.results
assert updated_task_obj_1.results == {"accuracy": "0.45"}
assert updated_task_obj_1.status == "SUCCESS"
assert updated_task_obj_1.end_time
assert updated_task_obj_1.duration
assert task_obj_2.id == updated_task_obj_2.id

assert updated_task_obj_2.before_snapshot_id
assert updated_task_obj_2.ports == None
assert updated_task_obj_2.gpu == False
assert updated_task_obj_2.interactive == False
assert updated_task_obj_2.task_dirpath
assert updated_task_obj_2.log_filepath
assert updated_task_obj_2.start_time

assert updated_task_obj_2.after_snapshot_id
assert updated_task_obj_2.run_id
assert updated_task_obj_2.logs
assert "accuracy" in updated_task_obj_2.logs
assert updated_task_obj_2.results
assert updated_task_obj_2.results == {"accuracy": "0.45"}
assert updated_task_obj_2.status == "SUCCESS"
assert updated_task_obj_2.end_time
assert updated_task_obj_2.duration

# 5) Test option 5

Expand Down Expand Up @@ -281,7 +298,7 @@ def test_run(self):
updated_task_obj_2 = self.task.run(task_obj_2.id)

assert updated_task_obj_2.before_snapshot_id
assert updated_task_obj_2.ports == []
assert updated_task_obj_2.ports == None
assert updated_task_obj_2.gpu == False
assert updated_task_obj_2.interactive == False
assert updated_task_obj_2.task_dirpath
Expand All @@ -291,7 +308,7 @@ def test_run(self):
assert updated_task_obj_2.after_snapshot_id
assert updated_task_obj_2.run_id
assert updated_task_obj_2.logs
assert "accuracy" in updated_task_obj_1.logs
assert "accuracy" in updated_task_obj_2.logs
assert updated_task_obj_2.results
assert updated_task_obj_2.results == {"accuracy": "0.56"}
assert updated_task_obj_2.status == "SUCCESS"
Expand Down
36 changes: 18 additions & 18 deletions datmo/core/entity/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class Task():
command that is used by the task
before_snapshot_id : str, optional
snapshot created before the task is run
(default is "", which means it isn't set yet)
(default is None, which means it isn't set yet)
ports : list, optional
list of string mappings from host system (left) to environment (right)
(e.g. ["9999:9999", "8888:8888"])
(default is [], which means it isn't set yet)
(default is None, which means it isn't set yet)
gpu : bool, optional
boolean to signify if run requires gpu
(default is False, which means no gpu unless specified)
Expand All @@ -37,28 +37,28 @@ class Task():
(default is False, which means no interactive mode unless specified)
task_dirpath : str, optional
task directory path relative to the project root
(default is "", which means it isn't set yet)
(default is None, which means it isn't set yet)
log_filepath : str, optional
log filepath relative to the project root
(default is "", which means it isn't set yet)
(default is None, which means it isn't set yet)
start_time : datetime.datetime
timestamp for the beginning time of the task
(default is None, which means it isn't set yet)
after_snapshot_id : str, optional
snapshot created after the task is run
(default is "", which means it isn't set yet)
(default is None, which means it isn't set yet)
run_id : str, optional
run id for the run (different from environment id and task id)
(default is "", which means it isn't set yet)
(default is None, which means it isn't set yet)
logs : str, optional
string output of logs
(default is "", which means it isn't set yet)
(default is None, which means it isn't set yet)
status : str, optional
status of the current task
(default is "", which means it isn't set yet)
(default is None, which means it isn't set yet)
results : dict, optional
dictionary containing output results from the task
(default is {}, which means it isn't set yet)
(default is None, which means it isn't set yet)
end_time : datetime.datetime, optional
timestamp for the beginning time of the task
(default is None, which means it isn't set yet)
Expand Down Expand Up @@ -122,20 +122,20 @@ def __init__(self, dictionary):
self.command = dictionary['command']

# Pre-Execution
self.before_snapshot_id = dictionary.get('before_snapshot_id', "")
self.ports = dictionary.get('ports', [])
self.before_snapshot_id = dictionary.get('before_snapshot_id', None)
self.ports = dictionary.get('ports', None)
self.gpu = dictionary.get('gpu', False)
self.interactive = dictionary.get('interactive', False)
self.task_dirpath = dictionary.get('task_dirpath', "")
self.log_filepath = dictionary.get('log_filepath', "")
self.task_dirpath = dictionary.get('task_dirpath', None)
self.log_filepath = dictionary.get('log_filepath', None)
self.start_time = dictionary.get('start_time', None)

# Post-Execution
self.after_snapshot_id = dictionary.get('after_snapshot_id', "")
self.run_id = dictionary.get('run_id', "")
self.logs = dictionary.get('logs', "")
self.status = dictionary.get('status', "")
self.results = dictionary.get('results', {})
self.after_snapshot_id = dictionary.get('after_snapshot_id', None)
self.run_id = dictionary.get('run_id', None)
self.logs = dictionary.get('logs', None)
self.status = dictionary.get('status', None)
self.results = dictionary.get('results', None)
self.end_time = dictionary.get('end_time', None)
self.duration = dictionary.get('duration', None)

Expand Down
36 changes: 18 additions & 18 deletions datmo/core/entity/test/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ def test_init_no_id(self):
for k, v in self.input_dict.items():
assert getattr(task_entity, k) == v
assert task_entity.id == None
assert task_entity.before_snapshot_id == ""
assert task_entity.ports == []
assert task_entity.before_snapshot_id == None
assert task_entity.ports == None
assert task_entity.gpu == False
assert task_entity.interactive == False
assert task_entity.task_dirpath == ""
assert task_entity.log_filepath == ""
assert task_entity.task_dirpath == None
assert task_entity.log_filepath == None
assert task_entity.start_time == None

# Post-Execution
assert task_entity.after_snapshot_id == ""
assert task_entity.run_id == ""
assert task_entity.logs == ""
assert task_entity.status == ""
assert task_entity.results == {}
assert task_entity.after_snapshot_id == None
assert task_entity.run_id == None
assert task_entity.logs == None
assert task_entity.status == None
assert task_entity.results == None
assert task_entity.end_time == None
assert task_entity.duration == None
assert task_entity.created_at
Expand All @@ -43,20 +43,20 @@ def test_init_with_id(self):

for k, v in self.input_dict.items():
assert getattr(task_entity, k) == v
assert task_entity.before_snapshot_id == ""
assert task_entity.ports == []
assert task_entity.before_snapshot_id == None
assert task_entity.ports == None
assert task_entity.gpu == False
assert task_entity.interactive == False
assert task_entity.task_dirpath == ""
assert task_entity.log_filepath == ""
assert task_entity.task_dirpath == None
assert task_entity.log_filepath == None
assert task_entity.start_time == None

# Post-Execution
assert task_entity.after_snapshot_id == ""
assert task_entity.run_id == ""
assert task_entity.logs == ""
assert task_entity.status == ""
assert task_entity.results == {}
assert task_entity.after_snapshot_id == None
assert task_entity.run_id == None
assert task_entity.logs == None
assert task_entity.status == None
assert task_entity.results == None
assert task_entity.end_time == None
assert task_entity.duration == None
assert task_entity.created_at
Expand Down
1 change: 1 addition & 0 deletions datmo/core/util/lang/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cli.general.method.not_found": "Method %s.%s not found",
"cli.project": "No project found in the current directory: %s",
"cli.task.run": "Error while running the task: %s",
"cli.task.run.already_running": "Already task running with id: %s",
"cli.task.stop": "Error while stopping the task: %s",
"util.misc_functions.get_filehash": "Filepath does not point to a valid file: %s",
"util.misc_functions.mutually_exclusive": "Mutually exclusive arguments passed: %s",
Expand Down
12 changes: 6 additions & 6 deletions datmo/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ class Task():
id of session associated with task
command : str
command that is used by the task
status : str
status : str or None
status of the current task
start_time : datetime.datetime
start_time : datetime.datetime or None
timestamp for the beginning time of the task
end_time : datetime.datetime
end_time : datetime.datetime or None
timestamp for the end time of the task
duration : datetime.timedelta
duration : datetime.timedelta or None
delta between start and end times
logs : str
logs : str or None
string output of logs
results : dict
results : dict or None
dictionary containing output results from the task
Methods
Expand Down
6 changes: 3 additions & 3 deletions datmo/test/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def test_task_entity_instantiate(self):

for k, v in input_dict.items():
assert getattr(task_entity, k) == v
assert task_entity.status == ""
assert task_entity.status == None
assert task_entity.start_time == None
assert task_entity.end_time == None
assert task_entity.duration == None
assert task_entity.logs == ""
assert task_entity.results == {}
assert task_entity.logs == None
assert task_entity.results == None

def test_run(self):
# 1) Run task with no commit or code available (cannot save states before), string command
Expand Down

0 comments on commit d9e9b58

Please sign in to comment.