coiled / feedback

A place to provide Coiled feedback
14 stars 3 forks source link

handling exit status of scripts run with Coiled CLI #268

Open andersy005 opened 10 months ago

andersy005 commented 10 months ago

i'm using coiled from the command line to run scripts, and i'm encountering a challenge with capturing the exit status of my scripts. specifically, i'm trying to determine if there's a way to capture the exit status of a script (like sample.py) when running it with the coiled run command.

# contents of sample.py
import subprocess

command = "bash -c 'echox test'"
result = subprocess.run(command, shell=True)
print(f'Exit Code: {result.returncode}')

# exit with the returncode
exit(result.returncode)

for instance, when i run the following command

$ coiled run sample.py

i get an output indicating the progress and the details of the run, followed by the exit code of the script. however, when I try to check the exit status immediately after the script finishes, using echo $?, it always returns 0, regardless of the actual exit status of sample.py.

$ coiled run sample.py                                                                                                                                                    
╭────────────────────────── Running python sample.py ──────────────────────────╮
│                                                                              │
│ Details: https://cloud.coiled.io/clusters/XXXXX          │
│                                                                              │
│ Ready  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━          │
│                                                                              │
│ Region:   us-west-2                 Uptime:                              42s │
│ VM Type:  m6i.xlarge                Approx cloud cost:              $0.20/hr │
│                                     Total cost:                        $0.00 │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯

Output
------

bash: line 1: echox: command not found
Exit Code: 127

❯ echo "$status $?"                                                                                                                                                                         
0 0

calling the script directly w/ python, works as expected

$ python sample.py                                                                                                                                                                          
bash: echox: command not found
Exit Code: 127
$ echo "$status $?"                                                                                                                                                                        
127 127

it seems that the coiled run command might be masking the actual exit status of the script i am running. i'd appreciate guidance on how to properly catch the exit status of my script when running it through Coiled. this is crucial for my workflow, where i need to make decisions based on the success or failure of the script.

thank you ---

ntabris commented 10 months ago

Hi, @andersy005. You're correct that coiled run foo does not currently exit with the exit code from foo. This is something that would require some work on our end. I can take a look next week and see if this is easy for us to add.

That said, perhaps another solution would be to have your script print messages, and then to look for those messages in stdout from coiled run and have your workflow act based on that. I know that's less elegant, but would that work for you?

andersy005 commented 10 months ago

This is something that would require some work on our end. I can take a look next week and see if this is easy for us to add.

that would be perfect

That said, perhaps another solution would be to have your script print messages, and then to look for those messages in stdout from coiled run and have your workflow act based on that. I know that's less elegant, but would that work for you?

thank you very much for the suggestion. i hadn't thought about this idea. i tinkered with this approach of using the output to detect failures/exit codes from foo, and it's working :)