Refactor bot and integrate services
This commit is contained in:
24
services/runner.py
Normal file
24
services/runner.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import asyncio
|
||||
import os
|
||||
from app import RESTIC_ENV
|
||||
|
||||
|
||||
async def run_cmd(cmd: list[str], *, use_restic_env: bool = False, timeout: int = 60):
|
||||
env = os.environ.copy()
|
||||
env["PATH"] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
if use_restic_env:
|
||||
env.update(RESTIC_ENV)
|
||||
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.STDOUT,
|
||||
env=env,
|
||||
)
|
||||
|
||||
try:
|
||||
out, _ = await asyncio.wait_for(proc.communicate(), timeout=timeout)
|
||||
return proc.returncode, out.decode(errors="ignore")[-3500:]
|
||||
except asyncio.TimeoutError:
|
||||
proc.kill()
|
||||
return 124, "❌ timeout"
|
||||
Reference in New Issue
Block a user