Commit df22f277 authored by Alexandre Boyer's avatar Alexandre Boyer
Browse files

fix status command when encounter a crash

parent 840a124c
......@@ -83,7 +83,7 @@ class ActevCommand():
try:
logging.info("Starting %s" % self.command)
StatusFactory.generateStatus(self, 'start', args.__dict__)
StatusFactory.generate_status(self, 'start', args.__dict__)
self.before_command(args)
mon = Monitor(self.entry_point, args, self.command)
......@@ -91,9 +91,9 @@ class ActevCommand():
self.after_command(args)
logging.info("%s done" % self.command)
StatusFactory.generateStatus(self, 'done', args.__dict__)
StatusFactory.generate_status(self, 'done', args.__dict__)
except:
logging.error("Issue during %s" % self.command)
StatusFactory.generateStatus(self, 'issue', args.__dict__)
StatusFactory.generate_status(self, 'issue', args.__dict__)
sys.exit(1)
......@@ -3,39 +3,59 @@
"""
import json
import os
import logging
class StatusFactory():
"""Generate state of a given command each time it is called.
"""
@staticmethod
def generateStatus(command, status, args):
"""Generate a dictionary inside a json file containing status of the commands
def generate_status(command, status, args):
"""Generate a dictionary inside a json file containing states of the commands
Args:
command (str): command name
status (str): state of the command ('Starting', 'Done', 'Not defined', 'Issue')
command (str): Command name
status (str): State of the command ('Starting', 'Done', 'Not defined', 'Issue')
"""
from diva_evaluation_cli.bin.cli import public_subcommands
status_id = 0
states_filename = './command_state_monitoring.json'
path = os.path.dirname(__file__)
status_log = os.path.join(path, './command_state_monitoring.json')
json_status = {}
if not os.path.isfile(status_log) or os.stat(status_log).st_size == 0:
for subcommand in public_subcommands:
json_status[subcommand.command] = [{'status': 'not defined', 'id': status_id, 'args': None}]
status_id += 1
json_status['id'] = status_id
command_states_log = os.path.join(path, states_filename)
json_command_states = {}
if not os.path.isfile(command_states_log) or os.stat(command_states_log).st_size == 0:
StatusFactory.generate_file(command_states_log, public_subcommands, json_command_states)
else:
with open(status_log, 'r') as f:
json_status = json.load(f)
if command in public_subcommands:
json_status['id'] += 1
json_status[command.command].append({'status': status, 'id': json_status['id'], 'args': args})
with open(status_log, 'w+') as f:
json.dump(json_status, f)
with open(command_states_log, 'r') as f:
try:
json_command_states = json.load(f)
if command in public_subcommands:
json_command_states['id'] += 1
json_command_states[command.command].append({'status': status, 'id': json_command_states['id'], 'args': args})
except:
logging.warning("Status monitoring improperly terminated: status reset")
os.remove(states_filename)
StatusFactory.generate_file(command_states_log, public_subcommands, json_command_states)
with open(command_states_log, 'w+') as f:
json.dump(json_command_states, f)
@staticmethod
def generate_file(filename, subcommands, json_command_states):
"""Initialize json file containing states of the commands
Args:
filename (str): Name of the json file
subcommands (str): List of subcommands to monitor
json_command_states (:obj:`dict`): Dictionary of command states to initialize
"""
status_id = 0
for subcommand in subcommands:
json_command_states[subcommand.command] = [{'status': 'not defined', 'id': status_id, 'args': None}]
status_id += 1
json_command_states['id'] = status_id
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment