99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Cron-порция волны coarena: берёт следующие N аккаунтов из очереди.
|
|
|
|
Запускается cron'ом каждые 10 минут. State: cron_state.json
|
|
Останавливается, когда очередь исчерпана (пишет cron_done.flag).
|
|
"""
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
BASE = "C:/Users/User/tmp/coarena"
|
|
PYEXE = r"C:/Users/User/AppData/Local/Programs/Python/Python311/python.exe"
|
|
S = r"C:/Users/User/AppData/Local/hermes/skills/autoreg/coarena-autoreg/scripts/coa_one.py"
|
|
STATE = BASE + "/cron_state.json"
|
|
LOCKF = BASE + "/cron.lock"
|
|
QUEUE = BASE + "/order_cron.txt"
|
|
LOG = BASE + "/cron_wave.log"
|
|
|
|
N = int(os.environ.get("COA_N", "18")) # аккаунтов за прогон
|
|
TIME_BUDGET = int(os.environ.get("COA_BUDGET", "540")) # секунд на прогон
|
|
WORKERS = int(os.environ.get("COA_WORKERS", "3"))
|
|
|
|
|
|
def log(msg):
|
|
with open(LOG, "a", encoding="utf8") as f:
|
|
f.write("%s %s\n" % (time.strftime("%H:%M:%S"), msg))
|
|
|
|
|
|
def take_lock():
|
|
import os as _os
|
|
try:
|
|
if _os.path.exists(LOCKF) and time.time() - _os.path.getmtime(LOCKF) < 900:
|
|
return False
|
|
open(LOCKF, "w").write(str(_os.getpid()))
|
|
return True
|
|
except Exception:
|
|
return True
|
|
|
|
|
|
def main():
|
|
if not take_lock():
|
|
print("locked, skip")
|
|
return
|
|
queue = [int(l) for l in open(QUEUE) if l.strip()]
|
|
st = {"pos": 0, "done": 0, "keys_start": 0}
|
|
if os.path.exists(STATE):
|
|
try:
|
|
st = json.load(open(STATE))
|
|
except Exception:
|
|
pass
|
|
|
|
if st["pos"] >= len(queue):
|
|
open(BASE + "/cron_done.flag", "w").write("done")
|
|
log("очередь исчерпана (%d/%d)" % (st["pos"], len(queue)))
|
|
return
|
|
|
|
chunk = queue[st["pos"]: st["pos"] + N]
|
|
env = dict(os.environ)
|
|
env["COA_HOME"] = BASE
|
|
env["COA_CHROME_UP"] = "C:/Users/User/tmp/agentrouter/chrome_up.py"
|
|
env["OA_PORT_BASE"] = "8100"
|
|
env.pop("PYTHONPATH", None)
|
|
env["PYTHONIOENCODING"] = "utf-8"
|
|
|
|
import concurrent.futures as cf
|
|
|
|
def run(i):
|
|
try:
|
|
r = subprocess.run([PYEXE, "-u", S, str(i)], env=env, timeout=280,
|
|
capture_output=True, text=True, errors="replace")
|
|
return (i, r.returncode)
|
|
except Exception as e:
|
|
return (i, type(e).__name__)
|
|
|
|
t0 = time.time()
|
|
ok = 0
|
|
with cf.ThreadPoolExecutor(WORKERS) as ex:
|
|
futs = {ex.submit(run, i): i for i in chunk}
|
|
for fut in cf.as_completed(futs, timeout=TIME_BUDGET + 120):
|
|
i, rc = fut.result()
|
|
if rc == 0:
|
|
ok += 1
|
|
log("idx=%d rc=%s" % (i, rc))
|
|
# отмечаем прогресс сразу — при обрыве не потеряем
|
|
st["pos"] = queue.index(i) + 1
|
|
st["done"] = st.get("done", 0) + 1
|
|
json.dump(st, open(STATE, "w"))
|
|
|
|
log("порция готова: %d/%d ok, %.0fs, pos=%d/%d" % (ok, len(chunk), time.time() - t0, st["pos"], len(queue)))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
pass
|
|
except Exception:
|
|
pass
|
|
main() |