| Conditions | 9 |
| Total Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 22 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | #!/usr/bin/env python2 |
||
| 32 | def main(): |
||
| 33 | # Necessary if we want 'logging.info' to appear in stderr. |
||
| 34 | logging.root.setLevel(logging.INFO) |
||
| 35 | |||
| 36 | args = parse_arguments() |
||
| 37 | |||
| 38 | command_manager = CommandManager(args.commands_filename) |
||
| 39 | |||
| 40 | # Handle TERM signal gracefully by sending running commands to back to |
||
| 41 | # the list of pending commands. |
||
| 42 | def sigterm_handler(signal, frame): |
||
| 43 | if sigterm_handler.triggered: |
||
| 44 | return |
||
| 45 | else: |
||
| 46 | sigterm_handler.triggered = True |
||
| 47 | if sigterm_handler.command is not None: |
||
| 48 | command_manager.set_running_command_as_pending(sigterm_handler.command) |
||
| 49 | sys.exit(0) |
||
| 50 | |||
| 51 | sigterm_handler.triggered = False |
||
| 52 | sigterm_handler.command = None |
||
| 53 | signal.signal(signal.SIGTERM, sigterm_handler) |
||
| 54 | |||
| 55 | while True: |
||
| 56 | command = command_manager.get_command_to_run() |
||
| 57 | sigterm_handler.command = command |
||
| 58 | |||
| 59 | if command is None: |
||
| 60 | break |
||
| 61 | |||
| 62 | uid = utils.generate_uid_from_string(command) |
||
| 63 | stdout_filename = os.path.join(args.logs_dir, uid + ".out") |
||
| 64 | stderr_filename = os.path.join(args.logs_dir, uid + ".err") |
||
| 65 | |||
| 66 | # Get job and node ID |
||
| 67 | job_id = os.environ.get('PBS_JOBID', 'undefined') |
||
| 68 | node_name = os.environ.get('HOSTNAME', 'undefined') |
||
| 69 | |||
| 70 | with open(stdout_filename, 'a') as stdout_file: |
||
| 71 | with open(stderr_filename, 'a') as stderr_file: |
||
| 72 | log_datetime = t.strftime("## SMART-DISPATCH - Started on: %Y-%m-%d %H:%M:%S - In job: {job_id} - On nodes: {node_name} ##\n".format(job_id=job_id, node_name=node_name)) |
||
| 73 | if stdout_file.tell() > 0: # Not the first line in the log file. |
||
| 74 | log_datetime = t.strftime("\n## SMART-DISPATCH - Resumed on: %Y-%m-%d %H:%M:%S - In job: {job_id} - On nodes: {node_name} ##\n".format(job_id=job_id, node_name=node_name)) |
||
| 75 | |||
| 76 | log_command = "## SMART-DISPATCH - Command: " + command + '\n' |
||
| 77 | |||
| 78 | stdout_file.write(log_datetime + log_command) |
||
| 79 | stdout_file.flush() |
||
| 80 | stderr_file.write(log_datetime + log_command) |
||
| 81 | stderr_file.flush() |
||
| 82 | |||
| 83 | error_code = subprocess.call(command, stdout=stdout_file, stderr=stderr_file, shell=True) |
||
| 84 | |||
| 85 | command_manager.set_running_command_as_finished(command, error_code) |
||
| 86 | |||
| 89 |