|
20 | 20 | import os |
21 | 21 | import re |
22 | 22 | import resource |
| 23 | +import signal |
23 | 24 | import sys |
24 | 25 | from collections import ChainMap |
25 | 26 | from itertools import chain |
|
36 | 37 | LOGGER = logging.getLogger("flux") |
37 | 38 |
|
38 | 39 |
|
| 40 | +def decode_signal(val): |
| 41 | + """ |
| 42 | + Decode a signal as string or number |
| 43 | + A string can be of the form 'SIGUSR1' or just 'USR1' |
| 44 | + """ |
| 45 | + if isinstance(val, int): |
| 46 | + return val |
| 47 | + try: |
| 48 | + return int(val) |
| 49 | + except ValueError: |
| 50 | + pass # Fall back to signal name |
| 51 | + try: |
| 52 | + return getattr(signal, val) |
| 53 | + except AttributeError: |
| 54 | + pass # Fall back SIG{name} |
| 55 | + try: |
| 56 | + return getattr(signal, f"SIG{val}") |
| 57 | + except AttributeError: |
| 58 | + pass |
| 59 | + raise ValueError(f"signal '{val}' is invalid") |
| 60 | + |
| 61 | + |
| 62 | +def decode_duration(val): |
| 63 | + """ |
| 64 | + Decode a duration as a number or string in FSD |
| 65 | + """ |
| 66 | + if isinstance(val, (float, int)): |
| 67 | + return val |
| 68 | + try: |
| 69 | + return float(val) |
| 70 | + except ValueError: |
| 71 | + pass # Fall back to fsd |
| 72 | + return util.parse_fsd(val) |
| 73 | + |
| 74 | + |
| 75 | +def parse_signal_option(arg): |
| 76 | + """ |
| 77 | + Parse the --signal= option argument of the form SIG@TIME, where |
| 78 | + both signal and time are optional. |
| 79 | +
|
| 80 | + Returns a dict with signal and timeleft members |
| 81 | + """ |
| 82 | + signo = signal.SIGUSR1 |
| 83 | + tleft = 60 |
| 84 | + if arg is not None: |
| 85 | + sig, _, time = arg.partition("@") |
| 86 | + if time: |
| 87 | + tleft = time |
| 88 | + if sig: |
| 89 | + signo = sig |
| 90 | + try: |
| 91 | + signum = decode_signal(signo) |
| 92 | + if signum <= 0: |
| 93 | + raise ValueError("signal must be > 0") |
| 94 | + except ValueError as exc: |
| 95 | + raise ValueError(f"--signal={arg}: {exc}") from None |
| 96 | + |
| 97 | + try: |
| 98 | + timeleft = decode_duration(tleft) |
| 99 | + except ValueError as exc: |
| 100 | + raise ValueError(f"--signal={arg}: {exc}") from None |
| 101 | + |
| 102 | + return {"signum": signum, "timeleft": timeleft} |
| 103 | + |
| 104 | + |
39 | 105 | class MiniConstraintParser(ConstraintParser): |
40 | 106 | operator_map = { |
41 | 107 | None: "properties", |
@@ -714,6 +780,12 @@ def create_parser( |
714 | 780 | + "flags: debug, waitable, novalidate", |
715 | 781 | metavar="FLAGS", |
716 | 782 | ) |
| 783 | + parser.add_argument( |
| 784 | + "--signal", |
| 785 | + help="Schedule delivery of signal SIG at a defined TIME before " |
| 786 | + + "job expiration. Default SIG is SIGUSR1, default TIME is 60s.", |
| 787 | + metavar="[SIG][@TIME]", |
| 788 | + ) |
717 | 789 | parser.add_argument( |
718 | 790 | "--dry-run", |
719 | 791 | action="store_true", |
@@ -742,6 +814,9 @@ def jobspec_create(self, args): |
742 | 814 | rlimits = get_filtered_rlimits(args.rlimit) |
743 | 815 | if rlimits: |
744 | 816 | jobspec.setattr_shell_option("rlimit", rlimits) |
| 817 | + if args.signal: |
| 818 | + entry = parse_signal_option(args.signal) |
| 819 | + jobspec.setattr_shell_option("signal", entry) |
745 | 820 |
|
746 | 821 | # --taskmap is only defined for run/submit, but we check |
747 | 822 | # for it in the base jobspec_create() for convenience |
|
0 commit comments