| Conditions | 13 |
| Total Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 23 | ||
| 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:
Complex classes like main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/env python2 |
||
| 33 | def main(): |
||
| 34 | # Necessary if we want 'logging.info' to appear in stderr. |
||
| 35 | logging.root.setLevel(logging.INFO) |
||
| 36 | |||
| 37 | args = parse_arguments() |
||
| 38 | |||
| 39 | command_manager = CommandManager(args.commands_filename) |
||
| 40 | |||
| 41 | if args.assumeResumable: |
||
| 42 | # Handle TERM signal gracefully by sending running commands back to |
||
| 43 | # the list of pending commands. |
||
| 44 | # NOTE: There are several cases when the handler will not have |
||
| 45 | # up-to-date information on running the command and/or process, |
||
| 46 | # but chances of that happening are VERY slim and the |
||
| 47 | # consequences are not fatal. |
||
| 48 | def sigterm_handler(signal, frame): |
||
| 49 | if sigterm_handler.triggered: |
||
| 50 | return |
||
| 51 | else: |
||
| 52 | sigterm_handler.triggered = True |
||
| 53 | |||
| 54 | if sigterm_handler.proc is not None: |
||
| 55 | sigterm_handler.proc.wait() |
||
| 56 | if sigterm_handler.command is not None: |
||
| 57 | command_manager.set_running_command_as_pending(sigterm_handler.command) |
||
| 58 | sys.exit(0) |
||
| 59 | sigterm_handler.triggered = False |
||
| 60 | sigterm_handler.command = None |
||
| 61 | sigterm_handler.proc = None |
||
| 62 | signal.signal(signal.SIGTERM, sigterm_handler) |
||
| 63 | |||
| 64 | while True: |
||
| 65 | command = command_manager.get_command_to_run() |
||
| 66 | if args.assumeResumable: |
||
| 67 | sigterm_handler.proc = None |
||
| 68 | sigterm_handler.command = command |
||
| 69 | |||
| 70 | if command is None: |
||
| 71 | break |
||
| 72 | |||
| 73 | uid = utils.generate_uid_from_string(command) |
||
| 74 | stdout_filename = os.path.join(args.logs_dir, uid + ".out") |
||
| 75 | stderr_filename = os.path.join(args.logs_dir, uid + ".err") |
||
| 76 | |||
| 77 | # Get job and node ID |
||
| 78 | job_id = os.environ.get('PBS_JOBID', 'undefined') |
||
| 79 | node_name = os.environ.get('HOSTNAME', 'undefined') |
||
| 80 | |||
| 81 | with open(stdout_filename, 'a') as stdout_file: |
||
| 82 | with open(stderr_filename, 'a') as stderr_file: |
||
| 83 | 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)) |
||
| 84 | if stdout_file.tell() > 0: # Not the first line in the log file. |
||
| 85 | 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)) |
||
| 86 | |||
| 87 | log_command = "## SMART-DISPATCH - Command: " + command + '\n' |
||
| 88 | |||
| 89 | stdout_file.write(log_datetime + log_command) |
||
| 90 | stdout_file.flush() |
||
| 91 | stderr_file.write(log_datetime + log_command) |
||
| 92 | stderr_file.flush() |
||
| 93 | |||
| 94 | proc = subprocess.Popen(command, stdout=stdout_file, stderr=stderr_file, shell=True) |
||
| 95 | if args.assumeResumable: |
||
| 96 | sigterm_handler.proc = proc |
||
| 97 | error_code = proc.wait() |
||
| 98 | |||
| 99 | command_manager.set_running_command_as_finished(command, error_code) |
||
| 100 | |||
| 103 |