|
1 | | -import subprocess |
2 | | -import time |
3 | | -from datetime import datetime |
4 | 1 | from typing import Optional, Tuple |
5 | 2 |
|
6 | 3 | import typer |
|
10 | 7 | from taskbadger import Action, StatusEnum, Task, __version__, integrations |
11 | 8 | from taskbadger.config import get_config, write_config |
12 | 9 | from taskbadger.exceptions import ConfigurationError |
| 10 | +from taskbadger.process import ProcessRunner |
13 | 11 |
|
14 | 12 | app = typer.Typer( |
15 | 13 | rich_markup_mode="rich", |
@@ -49,6 +47,7 @@ def run( |
49 | 47 | show_default=False, |
50 | 48 | help="Action definition e.g. 'success,error email to:me@email.com'", |
51 | 49 | ), |
| 50 | + capture_output: bool = typer.Option(False, help="Capture stdout and stderr."), |
52 | 51 | ): |
53 | 52 | """Execute a command using the CLI and create a Task to track its outcome. |
54 | 53 |
|
@@ -81,33 +80,43 @@ def run( |
81 | 80 | else: |
82 | 81 | print(f"Task created: {task.public_url}") |
83 | 82 | env = {"TASKBADGER_TASK_ID": task.id} if task else None |
84 | | - last_update = datetime.utcnow() |
85 | 83 | try: |
86 | | - process = subprocess.Popen(ctx.args, env=env, shell=True) |
87 | | - while process.poll() is None: |
88 | | - try: |
89 | | - time.sleep(0.1) |
90 | | - if task and _should_update_task(last_update, update_frequency): |
91 | | - last_update = datetime.utcnow() |
92 | | - task.ping() |
93 | | - except Exception as e: |
94 | | - err_console.print(f"Error updating task status: {e}") |
| 84 | + process = ProcessRunner(ctx.args, env, capture_output=capture_output, update_frequency=update_frequency) |
| 85 | + for output in process.run(): |
| 86 | + _update_task(task, **(output or {})) |
95 | 87 | except Exception as e: |
96 | | - task and task.error(data={"exception": str(e)}) |
| 88 | + _update_task(task, exception=str(e)) |
97 | 89 | raise typer.Exit(1) |
98 | 90 |
|
99 | 91 | if task: |
100 | 92 | if process.returncode == 0: |
101 | 93 | task.success(value=100) |
102 | 94 | else: |
103 | | - task.error(data={"return_code": process.returncode}) |
| 95 | + _update_task(task, status=StatusEnum.ERROR, return_code=process.returncode) |
104 | 96 |
|
105 | 97 | if process.returncode != 0: |
106 | 98 | raise typer.Exit(process.returncode) |
107 | 99 |
|
108 | 100 |
|
109 | | -def _should_update_task(last_update: datetime, update_frequency_seconds): |
110 | | - return (datetime.utcnow() - last_update).total_seconds() >= update_frequency_seconds |
| 101 | +def _update_task(task, status=None, **data_kwargs): |
| 102 | + """Update the task and merge the data""" |
| 103 | + if not task: |
| 104 | + return |
| 105 | + |
| 106 | + task_data = task.data or {} |
| 107 | + for key, value in data_kwargs.items(): |
| 108 | + if key in ("stdout", "stderr"): |
| 109 | + if key in task_data and value: |
| 110 | + task_data[key] += value |
| 111 | + elif value: |
| 112 | + task_data[key] = value |
| 113 | + else: |
| 114 | + task_data[key] = value |
| 115 | + |
| 116 | + try: |
| 117 | + task.update(status=status, data=task_data or None) |
| 118 | + except Exception as e: |
| 119 | + err_console.print(f"Error updating task status: {e}") |
111 | 120 |
|
112 | 121 |
|
113 | 122 | @app.command() |
|
0 commit comments