Compare commits

..
6 Commits
Author SHA1 Message Date
wuxu c2289f889e fix: 显示任务传输状态 2026-06-20 12:50:43 +08:00
wuxu 0e3dc9a96f fix: 支持配置局域网访问 2026-06-20 12:43:26 +08:00
wuxu ba9d0977e3 add: 持久化所有任务记录 2026-06-20 12:20:39 +08:00
wuxu cd051900b5 add: 显示单次任务记录 2026-06-20 12:10:32 +08:00
wuxu 161f266829 add: 添加每月最后一天调度 2026-06-20 11:46:50 +08:00
wuxu bce0c6188e fix: 收紧 WebGUI 默认安全配置 2026-06-20 11:34:07 +08:00
15 changed files with 895 additions and 107 deletions
+3
View File
@@ -0,0 +1,3 @@
WEBGUI_BIND_ADDR=127.0.0.1
JOBS_API_BIND_ADDR=127.0.0.1
JOBS_API_ALLOWED_ORIGINS=http://localhost:5580,http://127.0.0.1:5580
+1
View File
@@ -2,6 +2,7 @@
config/rclone/rclone.conf
# Docker build artifacts / runtime
.env
.docker/
webgui/data/
+53 -8
View File
@@ -11,13 +11,15 @@
```
.
├── webgui/ # webgui 源码(在父仓库,不在 rclone 子模块里)
│ ├── api/server.py # SQLite-backed recurring jobs API / scheduler
│ ├── data/ # jobs.sqlite 数据目录(不提交)
│ ├── webgui.go # Go 子命令源码(仅当自行构建 rclone 时需要)
│ ├── rclone-cmd-all-add-webgui-import.patch # 注解:把 webgui 注册进 rclone 的 cmd/all
│ └── web/ # 静态前端(rclone rcd 直接服务)
│ ├── index.html
│ └── assets/
├── config/rclone/ # rclone.conf 挂载点(bind mount,不提交)
├── docker-compose.yml # 单容器 rclone rcd
├── docker-compose.yml # rclone rcd + jobs-api sidecar
├── DESIGN.md # UI 设计系统规范
├── CLAUDE.md # Claude Code 协作指引
└── rclone/ # submodule → github.com/rclone/rclone,纯净不改动
@@ -29,28 +31,59 @@
`/config/providers` 动态生成,覆盖全部 70+ 后端的全部选项。
- **文件浏览** — 面包屑导航 + 文件表格,支持 mkdir / upload / delete /
rename / download。
- **同步任务** — copy / sync / move 异步任务,1.5 秒轮询进度(速度、
ETA、已传输 / 总量、错误计数),任务元信息(src→dst)持久化到
localStorage,刷新页面不丢。
- **同步任务** — copy / sync / move 异步任务,5 秒轮询进度(速度、
ETA、已传输 / 总量、错误计数)。单次任务和固定循环任务都保存到
SQLite;固定循环任务由 jobs-api sidecar 调度。
> OAuth 后端(drive、dropbox、onedrive 等)目前仅显示提示横幅,
> 引导用户在终端跑 `rclone config` 完成授权。
## 快速开始
> 安全提示:默认 Docker 编排使用 `--rc-no-auth`,RC API 可以读写和删除
> 已配置 remote 上的数据。Compose 文件默认只绑定到 `127.0.0.1`。不要把
> `5580` / `5581` 直接暴露到 LAN 或公网;需要远程访问时,请先加带认证
> 和 TLS 的反向代理。
```bash
# 1. 拉取子模块(rcd 流程用不到,自行构建 rclone 二进制时才需要)
git clone --recurse-submodules <your-fork-url>
# 2. 启动堆栈(单容器,无需 build)
# 2. 启动堆栈(无需 build)
docker compose up -d
# 3. 打开 http://localhost:5580
```
### 局域网访问
如果要从另一台机器访问,例如 `http://10.0.0.138:5580`,需要同时让
`rclone` 和 `jobs-api` 两个端口监听这个地址,并把页面 Origin 加进
jobs-api 白名单:
```bash
cp .env.example .env
cat > .env <<'EOF'
WEBGUI_BIND_ADDR=10.0.0.138
JOBS_API_BIND_ADDR=10.0.0.138
JOBS_API_ALLOWED_ORIGINS=http://localhost:5580,http://127.0.0.1:5580,http://10.0.0.138:5580
EOF
docker compose up -d
```
前端页面会从 `10.0.0.138:5580` 调用 `10.0.0.138:5581`,所以只改
`JOBS_API_ALLOWED_ORIGINS` 不够,`5581` 也必须发布到同一个可访问地址。
## 架构
只用一个 rclone rcd 容器,一个端口(5580),同时承担:
Docker 编排包含两个服务:
| 服务 | 本机 URL | 职责 |
|---|---|---|
| `rclone` | `http://localhost:5580` | 静态前端、RC API、远端文件下载 |
| `jobs-api` | `http://localhost:5581` | SQLite 循环任务 API 和调度器 |
`rclone` 容器通过 `rclone rcd` 同时承担:
| 职责 | URL | 配置项 |
|---|---|---|
@@ -58,8 +91,20 @@ docker compose up -d
| RC API | `POST /config/*`、`/operations/*`、`/sync/*`、`/job/*` | 内置 |
| 远端文件下载 | `GET /<remote>:<path>` | `--rc-serve` |
浏览器同源访问 → 不需要 CORS、不需要 nginx、不需要双端口。
要加 TLS 或自定义 header 时,**在前面套你自己的反代**即可。
浏览器从静态前端调用 `5580` 的 RC API,并调用 `5581` 的 jobs-api 管理
固定循环任务。jobs-api 默认只允许 `http://localhost:5580` 和
`http://127.0.0.1:5580` 这两个 Origin。反代或改端口时,同时调整
`JOBS_API_ALLOWED_ORIGINS`。
## 开发检查
```bash
find webgui/web/assets/js -name '*.js' -exec node --check {} \;
python3 -m py_compile webgui/api/server.py
python3 -m unittest discover -s webgui/api -p '*_test.py'
curl -sS http://127.0.0.1:5580/
curl -sS http://127.0.0.1:5581/health
```
## 自行构建 rclone(可选)
+7 -3
View File
@@ -7,7 +7,10 @@
#
# The jobs-api sidecar stores recurring task definitions in SQLite and starts
# scheduled runs through rclone RC. The browser calls it on port 5581.
# For TLS, auth, or custom headers, put a reverse proxy of your choice in front.
# Default port bindings are loopback-only because the RC API runs without auth.
# For LAN/public access, either put TLS/authentication in a reverse proxy first,
# or set WEBGUI_BIND_ADDR, JOBS_API_BIND_ADDR, and JOBS_API_ALLOWED_ORIGINS in
# a local .env file.
#
# rclone config lives in ./config/rclone/rclone.conf (bind-mounted).
# If it doesn't exist yet, create your remotes with:
@@ -50,7 +53,7 @@ services:
- XDG_CONFIG_HOME=/config
- RCLONE_CACHE_DIR=/cache
ports:
- "5580:8080"
- "${WEBGUI_BIND_ADDR:-127.0.0.1}:5580:8080"
restart: unless-stopped
jobs-api:
@@ -67,9 +70,10 @@ services:
- JOBS_API_HOST=0.0.0.0
- JOBS_API_PORT=8081
- JOBS_SCHEDULER_INTERVAL=15
- JOBS_API_ALLOWED_ORIGINS=${JOBS_API_ALLOWED_ORIGINS:-http://localhost:5580,http://127.0.0.1:5580}
- RCLONE_RC_URL=http://rclone:8080
ports:
- "5581:8081"
- "${JOBS_API_BIND_ADDR:-127.0.0.1}:5581:8081"
restart: unless-stopped
volumes:
+331 -11
View File
@@ -27,8 +27,18 @@ RCLONE_RC_URL = os.environ.get("RCLONE_RC_URL", "http://rclone:8080").rstrip("/"
HOST = os.environ.get("JOBS_API_HOST", "0.0.0.0")
PORT = int(os.environ.get("JOBS_API_PORT", "8081"))
SCHEDULER_INTERVAL_SECONDS = int(os.environ.get("JOBS_SCHEDULER_INTERVAL", "15"))
MAX_BODY_BYTES = int(os.environ.get("JOBS_API_MAX_BODY_BYTES", str(64 * 1024)))
ALLOWED_ORIGINS = {
origin.strip().rstrip("/")
for origin in os.environ.get(
"JOBS_API_ALLOWED_ORIGINS",
"http://localhost:5580,http://127.0.0.1:5580",
).split(",")
if origin.strip()
}
VALID_ACTIONS = {"copy", "sync", "move"}
VALID_SCHEDULE_KINDS = {"daily", "weekly", "monthly"}
MONTHDAY_LAST = 0
db_lock = threading.RLock()
scheduler_lock = threading.RLock()
@@ -133,13 +143,15 @@ def compute_next_run_at(
return iso(local_to_utc(candidate, offset_minutes))
if kind == "monthly":
if monthday is None or monthday < 1 or monthday > 31:
raise ValueError("scheduleMonthday must be 1-31")
if monthday is None or monthday < MONTHDAY_LAST or monthday > 31:
raise ValueError("scheduleMonthday must be 0-31")
for offset_months in range(0, 36):
year, month = add_months(local_after.year, local_after.month, offset_months)
if monthday > monthrange(year, month)[1]:
days_in_month = monthrange(year, month)[1]
candidate_day = days_in_month if monthday == MONTHDAY_LAST else monthday
if candidate_day > days_in_month:
continue
candidate = datetime(year, month, monthday, hour, minute)
candidate = datetime(year, month, candidate_day, hour, minute)
if candidate > local_after:
return iso(local_to_utc(candidate, offset_minutes))
raise ValueError("could not compute next monthly run")
@@ -211,10 +223,27 @@ def init_db() -> None:
FOREIGN KEY(schedule_id) REFERENCES job_schedules(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS one_time_jobs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
action TEXT NOT NULL,
src TEXT NOT NULL,
dst TEXT NOT NULL,
jobid INTEGER,
status TEXT NOT NULL,
error TEXT,
started_at TEXT,
finished_at TEXT,
status_json TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_job_schedules_due
ON job_schedules(enabled, current_jobid, next_run_at);
CREATE INDEX IF NOT EXISTS idx_job_runs_schedule
ON job_runs(schedule_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_one_time_jobs_updated
ON one_time_jobs(updated_at DESC);
"""
)
ensure_column(conn, "job_schedules", "schedule_kind", "TEXT NOT NULL DEFAULT 'daily'")
@@ -266,6 +295,32 @@ def row_to_job(row: sqlite3.Row) -> dict[str, Any]:
}
def row_to_one_time_job(row: sqlite3.Row) -> dict[str, Any]:
snapshot = None
if row["status_json"]:
try:
snapshot = json.loads(row["status_json"])
except json.JSONDecodeError:
snapshot = None
return {
"id": row["id"],
"action": row["action"],
"src": row["src"],
"dst": row["dst"],
"jobid": row["jobid"],
"lastJobid": row["jobid"],
"currentJobid": row["jobid"] if row["status"] == "running" else None,
"status": row["status"],
"error": row["error"],
"startedAt": row["started_at"],
"finishedAt": row["finished_at"],
"running": row["status"] == "running",
"statusSnapshot": snapshot,
"createdAt": row["created_at"],
"updatedAt": row["updated_at"],
}
def fetch_job(schedule_id: int) -> dict[str, Any]:
with db_lock, connect() as conn:
row = conn.execute("SELECT * FROM job_schedules WHERE id=?", (schedule_id,)).fetchone()
@@ -281,6 +336,21 @@ def list_jobs() -> list[dict[str, Any]]:
return [row_to_job(row) for row in rows]
def fetch_one_time_job(job_id: int) -> dict[str, Any]:
with db_lock, connect() as conn:
row = conn.execute("SELECT * FROM one_time_jobs WHERE id=?", (job_id,)).fetchone()
if row is None:
raise ApiError(404, "job not found")
return row_to_one_time_job(row)
def list_one_time_jobs() -> list[dict[str, Any]]:
refresh_one_time_jobs()
with db_lock, connect() as conn:
rows = conn.execute("SELECT * FROM one_time_jobs ORDER BY updated_at DESC, id DESC").fetchall()
return [row_to_one_time_job(row) for row in rows]
def create_job(payload: dict[str, Any]) -> dict[str, Any]:
action = str(payload.get("action", "")).strip()
src = str(payload.get("src", "")).strip()
@@ -343,6 +413,30 @@ def create_job(payload: dict[str, Any]) -> dict[str, Any]:
return fetch_job(schedule_id)
def create_one_time_job(payload: dict[str, Any]) -> dict[str, Any]:
action = str(payload.get("action", "")).strip()
src = str(payload.get("src", "")).strip()
dst = str(payload.get("dst", "")).strip()
if action not in VALID_ACTIONS:
raise ApiError(400, "invalid action")
if not src or not dst:
raise ApiError(400, "src and dst are required")
now = iso(utcnow())
with db_lock, connect() as conn:
cur = conn.execute(
"""
INSERT INTO one_time_jobs
(action, src, dst, status, created_at, updated_at)
VALUES (?, ?, ?, 'starting', ?, ?)
""",
(action, src, dst, now, now),
)
job_id = int(cur.lastrowid)
start_one_time_job(job_id)
return fetch_one_time_job(job_id)
def parse_optional_int(value: Any) -> int | None:
if value in (None, ""):
return None
@@ -417,6 +511,52 @@ def stop_job(schedule_id: int) -> dict[str, Any]:
return fetch_job(schedule_id)
def manual_run_one_time_job(job_id: int) -> dict[str, Any]:
with db_lock, connect() as conn:
row = conn.execute("SELECT * FROM one_time_jobs WHERE id=?", (job_id,)).fetchone()
if row is None:
raise ApiError(404, "job not found")
if row["status"] == "running":
raise ApiError(409, "job is already running")
start_one_time_job(job_id)
return fetch_one_time_job(job_id)
def stop_one_time_job(job_id: int) -> dict[str, Any]:
with db_lock, connect() as conn:
row = conn.execute("SELECT * FROM one_time_jobs WHERE id=?", (job_id,)).fetchone()
if row is None:
raise ApiError(404, "job not found")
jobid = row["jobid"]
if row["status"] != "running" or jobid is None:
return row_to_one_time_job(row)
rc_post("job/stop", {"jobid": jobid})
now = iso(utcnow())
snapshot = json.dumps({"finished": True, "error": "stopped", "jobid": jobid}, separators=(",", ":"))
with db_lock, connect() as conn:
conn.execute(
"""
UPDATE one_time_jobs
SET status='stopped',
error=NULL,
finished_at=?,
status_json=?,
updated_at=?
WHERE id=?
""",
(now, snapshot, now, job_id),
)
return fetch_one_time_job(job_id)
def delete_one_time_job(job_id: int) -> None:
with db_lock, connect() as conn:
cur = conn.execute("DELETE FROM one_time_jobs WHERE id=?", (job_id,))
if cur.rowcount == 0:
raise ApiError(404, "job not found")
def rc_post(
path: str,
payload: dict[str, Any],
@@ -465,7 +605,7 @@ def start_schedule(row: sqlite3.Row, trigger: str) -> None:
"srcFs": row["src"],
"dstFs": row["dst"],
"_async": True,
"_group": f"webgui/recurring/{schedule_id}/{action}",
"_group": transfer_group("recurring", schedule_id, action),
}
if action == "move":
payload["deleteEmptySrcDirs"] = True
@@ -534,6 +674,66 @@ def start_schedule(row: sqlite3.Row, trigger: str) -> None:
)
def start_one_time_job(job_id: int) -> None:
with db_lock, connect() as conn:
row = conn.execute("SELECT * FROM one_time_jobs WHERE id=?", (job_id,)).fetchone()
if row is None:
raise ApiError(404, "job not found")
now = iso(utcnow())
action = row["action"]
payload: dict[str, Any] = {
"srcFs": row["src"],
"dstFs": row["dst"],
"_async": True,
"_group": transfer_group("once", job_id, action),
}
if action == "move":
payload["deleteEmptySrcDirs"] = True
try:
res = rc_post(f"sync/{action}", payload)
jobid = res.get("jobid")
if jobid is None:
raise RcloneError(f"sync/{action}: missing jobid")
except Exception as exc:
error = str(exc)
snapshot = json.dumps({"finished": True, "error": error}, separators=(",", ":"))
with db_lock, connect() as conn:
conn.execute(
"""
UPDATE one_time_jobs
SET jobid=NULL,
status='failed',
error=?,
started_at=?,
finished_at=?,
status_json=?,
updated_at=?
WHERE id=?
""",
(error, now, now, snapshot, now, job_id),
)
return
snapshot = json.dumps({"finished": False, "jobid": jobid}, separators=(",", ":"))
with db_lock, connect() as conn:
conn.execute(
"""
UPDATE one_time_jobs
SET jobid=?,
status='running',
error=NULL,
started_at=?,
finished_at=NULL,
status_json=?,
updated_at=?
WHERE id=?
""",
(jobid, now, snapshot, now, job_id),
)
def status_name(status: dict[str, Any]) -> str:
if not status.get("finished"):
return "running"
@@ -544,6 +744,23 @@ def status_name(status: dict[str, Any]) -> str:
return "finished"
def transfer_group(kind: str, record_id: int, action: str) -> str:
return f"webgui/{kind}/{record_id}/{action}"
def status_with_stats(kind: str, row: sqlite3.Row, jobid: int) -> dict[str, Any]:
status = rc_post("job/status", {"jobid": jobid}, timeout=15, allow_error_body=True)
group = transfer_group(kind, int(row["id"]), row["action"])
try:
stats = rc_post("core/stats", {"group": group}, timeout=15, allow_error_body=True)
except Exception as exc:
stats = {"error": str(exc)}
status["stats"] = stats
if "group" not in status:
status["group"] = group
return status
def refresh_running_jobs() -> None:
with db_lock, connect() as conn:
rows = conn.execute("SELECT * FROM job_schedules WHERE current_jobid IS NOT NULL").fetchall()
@@ -552,12 +769,25 @@ def refresh_running_jobs() -> None:
schedule_id = int(row["id"])
jobid = int(row["current_jobid"])
try:
status = rc_post("job/status", {"jobid": jobid}, timeout=15, allow_error_body=True)
status = status_with_stats("recurring", row, jobid)
except Exception as exc:
status = {"finished": True, "error": str(exc), "jobid": jobid}
apply_status(row, status)
def refresh_one_time_jobs() -> None:
with db_lock, connect() as conn:
rows = conn.execute("SELECT * FROM one_time_jobs WHERE status='running' AND jobid IS NOT NULL").fetchall()
for row in rows:
jobid = int(row["jobid"])
try:
status = status_with_stats("once", row, jobid)
except Exception as exc:
status = {"finished": True, "error": str(exc), "jobid": jobid}
apply_one_time_status(row, status)
def apply_status(row: sqlite3.Row, status: dict[str, Any]) -> None:
schedule_id = int(row["id"])
jobid = int(row["current_jobid"])
@@ -616,6 +846,44 @@ def apply_status(row: sqlite3.Row, status: dict[str, Any]) -> None:
)
def apply_one_time_status(row: sqlite3.Row, status: dict[str, Any]) -> None:
job_id = int(row["id"])
jobid = int(row["jobid"])
now = iso(utcnow())
snapshot = json.dumps(status, separators=(",", ":"), ensure_ascii=False)
name = status_name(status)
if name == "running":
with db_lock, connect() as conn:
conn.execute(
"""
UPDATE one_time_jobs
SET status='running',
error=NULL,
status_json=?,
updated_at=?
WHERE id=? AND jobid=? AND status='running'
""",
(snapshot, now, job_id, jobid),
)
return
error = status.get("error")
with db_lock, connect() as conn:
conn.execute(
"""
UPDATE one_time_jobs
SET status=?,
error=?,
finished_at=?,
status_json=?,
updated_at=?
WHERE id=? AND jobid=? AND status='running'
""",
(name, error, now, snapshot, now, job_id, jobid),
)
def run_due_jobs() -> None:
with scheduler_lock:
now = iso(utcnow())
@@ -635,6 +903,7 @@ def run_due_jobs() -> None:
def scheduler_tick() -> None:
refresh_running_jobs()
refresh_one_time_jobs()
run_due_jobs()
@@ -646,11 +915,22 @@ def scheduler_loop() -> None:
traceback.print_exc()
def scheduler_initial_tick() -> None:
try:
scheduler_tick()
except Exception:
traceback.print_exc()
class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def do_OPTIONS(self) -> None:
try:
self.require_allowed_origin()
self.send_json(204, None)
except ApiError as exc:
self.send_json(exc.status, {"error": exc.message})
def do_GET(self) -> None:
self.handle_request("GET")
@@ -663,6 +943,7 @@ class Handler(BaseHTTPRequestHandler):
def handle_request(self, method: str) -> None:
try:
self.require_allowed_origin()
parsed = urlparse(self.path)
path = parsed.path.rstrip("/") or "/"
parts = [p for p in path.split("/") if p]
@@ -679,6 +960,14 @@ class Handler(BaseHTTPRequestHandler):
self.send_json(201, {"job": create_job(self.read_json())})
return
if parts == ["api", "one-time-jobs"] and method == "GET":
self.send_json(200, {"jobs": list_one_time_jobs()})
return
if parts == ["api", "one-time-jobs"] and method == "POST":
self.send_json(201, {"job": create_one_time_job(self.read_json())})
return
if len(parts) == 3 and parts[:2] == ["api", "jobs"]:
schedule_id = parse_id(parts[2])
if method == "DELETE":
@@ -686,6 +975,13 @@ class Handler(BaseHTTPRequestHandler):
self.send_json(200, {"ok": True})
return
if len(parts) == 3 and parts[:2] == ["api", "one-time-jobs"]:
job_id = parse_id(parts[2])
if method == "DELETE":
delete_one_time_job(job_id)
self.send_json(200, {"ok": True})
return
if len(parts) == 4 and parts[:2] == ["api", "jobs"] and method == "POST":
schedule_id = parse_id(parts[2])
if parts[3] == "run":
@@ -695,6 +991,15 @@ class Handler(BaseHTTPRequestHandler):
self.send_json(200, {"job": stop_job(schedule_id)})
return
if len(parts) == 4 and parts[:2] == ["api", "one-time-jobs"] and method == "POST":
job_id = parse_id(parts[2])
if parts[3] == "run":
self.send_json(200, {"job": manual_run_one_time_job(job_id)})
return
if parts[3] == "stop":
self.send_json(200, {"job": stop_one_time_job(job_id)})
return
raise ApiError(404, "not found")
except ApiError as exc:
self.send_json(exc.status, {"error": exc.message})
@@ -704,8 +1009,22 @@ class Handler(BaseHTTPRequestHandler):
traceback.print_exc()
self.send_json(500, {"error": str(exc)})
def require_allowed_origin(self) -> None:
origin = self.headers.get("Origin")
if not origin:
return
if origin.rstrip("/") not in ALLOWED_ORIGINS:
raise ApiError(403, "origin not allowed")
def read_json(self) -> dict[str, Any]:
try:
length = int(self.headers.get("Content-Length") or "0")
except ValueError:
raise ApiError(400, "invalid Content-Length") from None
if length < 0:
raise ApiError(400, "invalid Content-Length")
if length > MAX_BODY_BYTES:
raise ApiError(413, "request body too large")
if length == 0:
return {}
raw = self.rfile.read(length)
@@ -719,8 +1038,11 @@ class Handler(BaseHTTPRequestHandler):
def send_json(self, status: int, body: Any) -> None:
raw = b"" if body is None else json.dumps(body, ensure_ascii=False).encode("utf-8")
origin = self.headers.get("Origin")
self.send_response(status)
self.send_header("Access-Control-Allow-Origin", "*")
if origin and origin.rstrip("/") in ALLOWED_ORIGINS:
self.send_header("Access-Control-Allow-Origin", origin)
self.send_header("Vary", "Origin")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.send_header("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS")
self.send_header("Content-Type", "application/json; charset=utf-8")
@@ -747,10 +1069,8 @@ def main() -> None:
init_db()
thread = threading.Thread(target=scheduler_loop, name="job-scheduler", daemon=True)
thread.start()
try:
scheduler_tick()
except Exception:
traceback.print_exc()
initial_thread = threading.Thread(target=scheduler_initial_tick, name="job-scheduler-initial", daemon=True)
initial_thread.start()
server = ThreadingHTTPServer((HOST, PORT), Handler)
print(f"jobs api listening on {HOST}:{PORT}, db={DB_PATH}, rclone={RCLONE_RC_URL}")
try:
+190
View File
@@ -0,0 +1,190 @@
import unittest
import tempfile
import os
from io import BytesIO
from datetime import datetime, timezone
import server
from server import ApiError, Handler, MAX_BODY_BYTES, compute_next_run_at
class ComputeNextRunAtTest(unittest.TestCase):
def test_daily_uses_same_day_when_time_is_future(self):
after = datetime(2026, 6, 20, 12, 0, tzinfo=timezone.utc)
self.assertEqual(
compute_next_run_at("daily", "23:30", 0, after),
"2026-06-20T23:30:00Z",
)
def test_daily_moves_to_next_day_when_time_has_passed(self):
after = datetime(2026, 6, 20, 23, 30, tzinfo=timezone.utc)
self.assertEqual(
compute_next_run_at("daily", "23:30", 0, after),
"2026-06-21T23:30:00Z",
)
def test_weekly_uses_next_target_weekday(self):
after = datetime(2026, 6, 20, 12, 0, tzinfo=timezone.utc) # Saturday
self.assertEqual(
compute_next_run_at("weekly", "09:00", 0, after, weekday=1),
"2026-06-22T09:00:00Z",
)
def test_weekly_rolls_forward_a_week_after_same_day_time_passed(self):
after = datetime(2026, 6, 22, 10, 0, tzinfo=timezone.utc) # Monday
self.assertEqual(
compute_next_run_at("weekly", "09:00", 0, after, weekday=1),
"2026-06-29T09:00:00Z",
)
def test_monthly_uses_next_valid_monthday(self):
after = datetime(2026, 1, 30, 12, 0, tzinfo=timezone.utc)
self.assertEqual(
compute_next_run_at("monthly", "23:00", 0, after, monthday=31),
"2026-01-31T23:00:00Z",
)
def test_monthly_skips_short_months_for_day_31(self):
after = datetime(2026, 2, 1, 0, 0, tzinfo=timezone.utc)
self.assertEqual(
compute_next_run_at("monthly", "23:00", 0, after, monthday=31),
"2026-03-31T23:00:00Z",
)
def test_monthly_last_day_uses_current_month_end(self):
after = datetime(2026, 2, 1, 0, 0, tzinfo=timezone.utc)
self.assertEqual(
compute_next_run_at("monthly", "23:00", 0, after, monthday=0),
"2026-02-28T23:00:00Z",
)
def test_monthly_last_day_rolls_forward_when_current_month_end_passed(self):
after = datetime(2026, 2, 28, 23, 0, tzinfo=timezone.utc)
self.assertEqual(
compute_next_run_at("monthly", "23:00", 0, after, monthday=0),
"2026-03-31T23:00:00Z",
)
def test_timezone_offset_is_applied_from_local_schedule_time(self):
after = datetime(2026, 6, 20, 12, 0, tzinfo=timezone.utc)
self.assertEqual(
compute_next_run_at("daily", "23:00", 8 * 60, after),
"2026-06-20T15:00:00Z",
)
def test_invalid_monthday_raises(self):
after = datetime(2026, 6, 20, 12, 0, tzinfo=timezone.utc)
with self.assertRaises(ValueError):
compute_next_run_at("monthly", "23:00", 0, after, monthday=32)
class HandlerSecurityTest(unittest.TestCase):
def test_allowed_origin_is_accepted(self):
handler = type("DummyHandler", (), {"headers": {"Origin": "http://localhost:5580"}})()
Handler.require_allowed_origin(handler)
def test_disallowed_origin_is_rejected(self):
handler = type("DummyHandler", (), {"headers": {"Origin": "http://example.invalid"}})()
with self.assertRaises(ApiError) as ctx:
Handler.require_allowed_origin(handler)
self.assertEqual(ctx.exception.status, 403)
def test_request_body_size_is_limited(self):
handler = type(
"DummyHandler",
(),
{
"headers": {"Content-Length": str(MAX_BODY_BYTES + 1)},
"rfile": BytesIO(),
},
)()
with self.assertRaises(ApiError) as ctx:
Handler.read_json(handler)
self.assertEqual(ctx.exception.status, 413)
def test_read_json_accepts_small_object(self):
body = b'{"ok":true}'
handler = type(
"DummyHandler",
(),
{
"headers": {"Content-Length": str(len(body))},
"rfile": BytesIO(body),
},
)()
self.assertEqual(Handler.read_json(handler), {"ok": True})
class OneTimeJobTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.old_db_path = server.DB_PATH
self.old_rc_post = server.rc_post
server.DB_PATH = os.path.join(self.tmp.name, "jobs.sqlite")
self.next_jobid = 100
def fake_rc_post(path, payload, timeout=30, allow_error_body=False):
if path.startswith("sync/"):
self.next_jobid += 1
return {"jobid": self.next_jobid}
if path == "job/status":
return {"finished": False, "jobid": payload["jobid"]}
if path == "core/stats":
return {
"bytes": 1,
"totalBytes": 2,
"speed": 3,
"eta": 4,
"transfers": 5,
"totalTransfers": 6,
"errors": 7,
"group": payload["group"],
}
if path == "job/stop":
return {}
raise AssertionError(path)
server.rc_post = fake_rc_post
server.init_db()
def tearDown(self):
server.rc_post = self.old_rc_post
server.DB_PATH = self.old_db_path
self.tmp.cleanup()
def test_create_and_list_one_time_job(self):
job = server.create_one_time_job({"action": "copy", "src": "/tmp/a", "dst": "/tmp/b"})
self.assertEqual(job["id"], 1)
self.assertEqual(job["jobid"], 101)
self.assertEqual(job["status"], "running")
self.assertEqual(job["src"], "/tmp/a")
jobs = server.list_one_time_jobs()
self.assertEqual(len(jobs), 1)
self.assertEqual(jobs[0]["id"], 1)
self.assertEqual(jobs[0]["status"], "running")
self.assertEqual(jobs[0]["statusSnapshot"]["stats"]["bytes"], 1)
self.assertEqual(jobs[0]["statusSnapshot"]["stats"]["totalBytes"], 2)
self.assertEqual(jobs[0]["statusSnapshot"]["stats"]["speed"], 3)
def test_stop_one_time_job_persists_stopped_status(self):
job = server.create_one_time_job({"action": "copy", "src": "/tmp/a", "dst": "/tmp/b"})
stopped = server.stop_one_time_job(job["id"])
self.assertEqual(stopped["status"], "stopped")
self.assertFalse(stopped["running"])
self.assertIsNotNone(stopped["statusSnapshot"])
def test_run_one_time_job_again_reuses_record_with_new_jobid(self):
job = server.create_one_time_job({"action": "copy", "src": "/tmp/a", "dst": "/tmp/b"})
server.stop_one_time_job(job["id"])
restarted = server.manual_run_one_time_job(job["id"])
self.assertEqual(restarted["id"], job["id"])
self.assertEqual(restarted["jobid"], 102)
self.assertEqual(restarted["status"], "running")
if __name__ == "__main__":
unittest.main()
+6 -6
View File
@@ -2,15 +2,15 @@
// the top-nav active state in sync, and re-renders chrome strings when
// the locale changes.
import { onRoute } from "./state.js?v=recurring-jobs-5";
import { t, currentLocale, setLocale, onLocale } from "./i18n.js?v=recurring-jobs-5";
import { renderRemotes } from "./views/remotes.js?v=recurring-jobs-5";
import { renderBrowse } from "./views/browser.js?v=recurring-jobs-5";
import { renderJobs, renderNewJob, stopJobPolling } from "./views/jobs.js?v=recurring-jobs-5";
import { onRoute } from "./state.js?v=job-stats-1";
import { t, currentLocale, setLocale, onLocale } from "./i18n.js?v=job-stats-1";
import { renderRemotes } from "./views/remotes.js?v=job-stats-1";
import { renderBrowse } from "./views/browser.js?v=job-stats-1";
import { renderJobs, renderNewJob, stopJobPolling } from "./views/jobs.js?v=job-stats-1";
import {
renderConfigureNew,
renderConfigureEdit,
} from "./views/configure.js?v=recurring-jobs-5";
} from "./views/configure.js?v=job-stats-1";
const views = {
remotes: renderRemotes,
+38 -6
View File
@@ -155,7 +155,7 @@ const STRINGS = {
// jobs view
"jobs.title": "Jobs",
"jobs.subtitle": "Saved recurring transfers and their latest runs.",
"jobs.subtitle": "Saved transfers and their latest runs.",
"jobs.new_btn": "New Job",
"jobs.cancel": "Cancel",
"jobs.new_title": "New Job",
@@ -167,7 +167,7 @@ const STRINGS = {
"jobs.schedule_type": "Job type",
"jobs.schedule_once": "One-time",
"jobs.schedule_recurring": "Fixed recurring",
"jobs.schedule_help": "One-time jobs are submitted to rclone and not saved. Fixed recurring jobs are stored in SQLite.",
"jobs.schedule_help": "One-time and fixed recurring jobs are stored in SQLite.",
"jobs.recurrence_kind": "Recurring plan",
"jobs.recurrence_daily": "Every day",
"jobs.recurrence_weekly": "Every week",
@@ -184,9 +184,11 @@ const STRINGS = {
"jobs.weekday_saturday": "Saturday",
"jobs.weekday_sunday": "Sunday",
"jobs.monthday": (n) => `Day ${n}`,
"jobs.monthday_last": "Last day",
"jobs.schedule_label_daily": (time) => `Every day at ${time}`,
"jobs.schedule_label_weekly": (day, time) => `Every ${day} at ${time}`,
"jobs.schedule_label_monthly": (day, time) => `Day ${day} of every month at ${time}`,
"jobs.schedule_label_monthly_last": (time) => `Last day of every month at ${time}`,
"jobs.local_option": "Local filesystem",
"jobs.local_path_help": "For Local filesystem, enter a path visible inside the rclone process, such as /data/source in this Docker setup. For remotes, enter a path inside the selected remote.",
"jobs.source_remote": "Source location",
@@ -201,10 +203,15 @@ const STRINGS = {
"jobs.pick_empty": "No folders here.",
"jobs.start": "Start Job",
"jobs.no_remotes_option": "(no remotes)",
"jobs.view_filter": "Job view",
"jobs.view_once": "One-time jobs",
"jobs.view_recurring": "Recurring jobs",
"jobs.empty_title": "No jobs yet",
"jobs.empty_once_title": "No one-time jobs yet",
"jobs.empty_recurring_title": "No recurring jobs yet",
"jobs.empty_body": "Use New Job to start a copy, sync, or move.",
"jobs.started": (a, id) => `Started ${a} job #${id}`,
"jobs.started_once": (a, id) => `Started one-time ${a} job #${id}. It was not saved.`,
"jobs.started_once": (a, id) => `Started one-time ${a} job #${id}`,
"jobs.recurring_saved": (id) => `Saved recurring job #${id}`,
"jobs.start_failed": "Job start failed",
"jobs.stop_confirm": (id) => `Stop job #${id}?`,
@@ -212,6 +219,7 @@ const STRINGS = {
"jobs.stop_failed": "Stop failed",
"jobs.start_again": "Start again",
"jobs.restarted": (id) => `Started recurring job #${id}`,
"jobs.restarted_once": (id) => `Started one-time job #${id}`,
"jobs.restart_failed": "Start again failed",
"jobs.details": "Details",
"jobs.hide_details": "Hide",
@@ -219,6 +227,7 @@ const STRINGS = {
"jobs.delete_confirm": (id) => `Delete job #${id} from this list? This won't cancel a running transfer.`,
"jobs.delete_failed": "Delete failed",
"jobs.detail_schedule_id": "Schedule ID",
"jobs.detail_record_id": "Record",
"jobs.detail_id": "Job ID",
"jobs.detail_status": "Status",
"jobs.detail_error": "Error",
@@ -229,6 +238,11 @@ const STRINGS = {
"jobs.detail_schedule": "Schedule",
"jobs.detail_group": "Group",
"jobs.detail_execute_id": "Execution",
"jobs.detail_bytes": "Bytes",
"jobs.detail_speed": "Speed",
"jobs.detail_eta": "ETA",
"jobs.detail_files": "Files",
"jobs.detail_errors": "Errors",
"jobs.detail_output": "Output",
"jobs.detail_none": "None",
"jobs.col.id": "#",
@@ -248,6 +262,8 @@ const STRINGS = {
"jobs.status.scheduled": "scheduled",
"jobs.status.stopped": "stopped",
"jobs.stop": "Stop",
"jobs.once_id": (id) => `once:${id}`,
"jobs.schedule_label_once": "One-time run",
"jobs.submitted_cli": "— submitted via CLI —",
},
@@ -369,7 +385,7 @@ const STRINGS = {
"configure.example_custom_placeholder": "自定义值",
"jobs.title": "任务",
"jobs.subtitle": "已保存的循环传输任务及其最近运行状态。",
"jobs.subtitle": "已保存的传输任务及其最近运行状态。",
"jobs.new_btn": "新建任务",
"jobs.cancel": "取消",
"jobs.new_title": "新建任务",
@@ -381,7 +397,7 @@ const STRINGS = {
"jobs.schedule_type": "任务类型",
"jobs.schedule_once": "单次任务",
"jobs.schedule_recurring": "固定循环",
"jobs.schedule_help": "单次任务只提交给 rclone,不保存记录。固定循环任务会保存到 SQLite。",
"jobs.schedule_help": "单次任务和固定循环任务都会保存到 SQLite。",
"jobs.recurrence_kind": "循环计划",
"jobs.recurrence_daily": "每天",
"jobs.recurrence_weekly": "每周",
@@ -398,9 +414,11 @@ const STRINGS = {
"jobs.weekday_saturday": "星期六",
"jobs.weekday_sunday": "星期日",
"jobs.monthday": (n) => `${n} 号`,
"jobs.monthday_last": "最后一天",
"jobs.schedule_label_daily": (time) => `每天 ${time}`,
"jobs.schedule_label_weekly": (day, time) => `每周${day.replace(/^星期/, "")} ${time}`,
"jobs.schedule_label_monthly": (day, time) => `每月 ${day} 号 ${time}`,
"jobs.schedule_label_monthly_last": (time) => `每月最后一天 ${time}`,
"jobs.local_option": "本地文件系统",
"jobs.local_path_help": "选择本地文件系统时填写 rclone 进程内可见的路径;当前 Docker 编排里通常是 /data/source。选择远程存储时填写该远程内的路径。",
"jobs.source_remote": "源位置",
@@ -415,10 +433,15 @@ const STRINGS = {
"jobs.pick_empty": "这里没有文件夹。",
"jobs.start": "开始任务",
"jobs.no_remotes_option": "(无远程存储)",
"jobs.view_filter": "任务视图",
"jobs.view_once": "单次任务",
"jobs.view_recurring": "循环任务",
"jobs.empty_title": "尚无任务",
"jobs.empty_once_title": "尚无单次任务",
"jobs.empty_recurring_title": "尚无循环任务",
"jobs.empty_body": "用「新建任务」开始一个 copy、sync 或 move。",
"jobs.started": (a, id) => `已启动 ${a} 任务 #${id}`,
"jobs.started_once": (a, id) => `已启动单次 ${a} 任务 #${id},不会保存记录。`,
"jobs.started_once": (a, id) => `已启动单次 ${a} 任务 #${id}`,
"jobs.recurring_saved": (id) => `已保存循环任务 #${id}`,
"jobs.start_failed": "任务启动失败",
"jobs.stop_confirm": (id) => `停止任务 #${id}?`,
@@ -426,6 +449,7 @@ const STRINGS = {
"jobs.stop_failed": "停止失败",
"jobs.start_again": "再次启动",
"jobs.restarted": (id) => `已启动循环任务 #${id}`,
"jobs.restarted_once": (id) => `已启动单次任务 #${id}`,
"jobs.restart_failed": "再次启动失败",
"jobs.details": "详情",
"jobs.hide_details": "收起",
@@ -433,6 +457,7 @@ const STRINGS = {
"jobs.delete_confirm": (id) => `从列表中删除任务 #${id}? 这不会取消正在运行的传输。`,
"jobs.delete_failed": "删除失败",
"jobs.detail_schedule_id": "调度 ID",
"jobs.detail_record_id": "记录",
"jobs.detail_id": "任务 ID",
"jobs.detail_status": "状态",
"jobs.detail_error": "错误",
@@ -443,6 +468,11 @@ const STRINGS = {
"jobs.detail_schedule": "固定计划",
"jobs.detail_group": "分组",
"jobs.detail_execute_id": "执行实例",
"jobs.detail_bytes": "字节",
"jobs.detail_speed": "速度",
"jobs.detail_eta": "预计剩余",
"jobs.detail_files": "文件",
"jobs.detail_errors": "错误",
"jobs.detail_output": "输出",
"jobs.detail_none": "无",
"jobs.col.id": "#",
@@ -462,6 +492,8 @@ const STRINGS = {
"jobs.status.scheduled": "已调度",
"jobs.status.stopped": "已停止",
"jobs.stop": "停止",
"jobs.once_id": (id) => `单次:${id}`,
"jobs.schedule_label_once": "单次运行",
"jobs.submitted_cli": "— 通过命令行提交 —",
},
};
+33
View File
@@ -44,6 +44,39 @@ export async function deleteRecurringJob(id) {
});
}
export async function listOneTimeJobs() {
const res = await request("/api/one-time-jobs");
return (res && res.jobs) || [];
}
export async function createOneTimeJob(job) {
const res = await request("/api/one-time-jobs", {
method: "POST",
body: job,
});
return res && res.job;
}
export async function runOneTimeJobNow(id) {
const res = await request(`/api/one-time-jobs/${encodeURIComponent(id)}/run`, {
method: "POST",
});
return res && res.job;
}
export async function stopOneTimeJob(id) {
const res = await request(`/api/one-time-jobs/${encodeURIComponent(id)}/stop`, {
method: "POST",
});
return res && res.job;
}
export async function deleteOneTimeJob(id) {
return request(`/api/one-time-jobs/${encodeURIComponent(id)}`, {
method: "DELETE",
});
}
async function request(path, { method = "GET", body } = {}) {
const headers = { "Content-Type": "application/json" };
const res = await fetch(`${API_BASE}${path}`, {
+4 -4
View File
@@ -31,7 +31,7 @@ export function isNoAuth() {
}
// POST JSON to an RC endpoint. Returns the parsed JSON response, or throws.
export async function post(path, body = {}) {
export async function post(path, body = {}, options = {}) {
const headers = { "Content-Type": "application/json" };
if (authHeader) headers["Authorization"] = authHeader;
const res = await fetch(RC_BASE + "/" + path.replace(/^\//, ""), {
@@ -39,7 +39,7 @@ export async function post(path, body = {}) {
headers,
body: JSON.stringify(body),
});
return parseResponse(res, path);
return parseResponse(res, path, options);
}
// POST JSON and request an async job. Returns { jobid, executeId }.
@@ -82,7 +82,7 @@ export function downloadURL(remoteFs, remotePath, fileName) {
return url;
}
async function parseResponse(res, path) {
async function parseResponse(res, path, { allowError = false } = {}) {
let body = null;
const ct = res.headers.get("Content-Type") || "";
if (ct.includes("application/json")) {
@@ -98,7 +98,7 @@ async function parseResponse(res, path) {
err.body = body;
throw err;
}
if (body && body.error) {
if (body && body.error && !allowError) {
throw new Error(`${path}: ${body.error}`);
}
return body;
+3 -3
View File
@@ -1,8 +1,8 @@
// views/browser.js — file/folder listing with breadcrumbs, mkdir, upload, delete, rename.
import { post, uploadFile, downloadURL } from "../rc.js";
import { toast, formatBytes, formatTime } from "../state.js?v=recurring-jobs-5";
import { t } from "../i18n.js?v=recurring-jobs-5";
import { post, uploadFile, downloadURL } from "../rc.js?v=job-stats-1";
import { toast, formatBytes, formatTime } from "../state.js?v=job-stats-1";
import { t } from "../i18n.js?v=job-stats-1";
export async function renderBrowse({ remote, path }) {
const app = document.getElementById("app");
+3 -3
View File
@@ -9,9 +9,9 @@
// OAuth backends (option named "token" with IsPassword) get a banner
// and disabled submit — user must run `rclone config` in a terminal.
import { post } from "../rc.js";
import { getState, setState, toast } from "../state.js?v=recurring-jobs-5";
import { t } from "../i18n.js?v=recurring-jobs-5";
import { post } from "../rc.js?v=job-stats-1";
import { getState, setState, toast } from "../state.js?v=job-stats-1";
import { t } from "../i18n.js?v=job-stats-1";
// --- Route entrypoints ---
+216 -56
View File
@@ -1,27 +1,32 @@
// views/jobs.js — submit sync/copy/move jobs and manage recurring transfers.
import { post, postAsync } from "../rc.js";
import { post } from "../rc.js?v=job-stats-1";
import {
toast,
formatBytes,
formatSpeed,
formatDuration,
} from "../state.js?v=recurring-jobs-5";
import { t } from "../i18n.js?v=recurring-jobs-5";
} from "../state.js?v=job-stats-1";
import { t } from "../i18n.js?v=job-stats-1";
import {
createOneTimeJob,
createRecurringJob,
deleteOneTimeJob,
deleteRecurringJob,
jobsApiURL,
listOneTimeJobs,
listRecurringJobs,
runOneTimeJobNow,
runRecurringJobNow,
stopOneTimeJob,
stopRecurringJob,
} from "../jobs_api.js?v=recurring-jobs-5";
} from "../jobs_api.js?v=job-stats-1";
let pollTimer = null;
const WEBGUI_JOB_GROUP_PREFIX = "webgui/transfer";
const LOCAL_FS_VALUE = "__local__";
const expandedJobs = new Set();
const LOCAL_PICKER_ROOT = "/root";
let currentJobsView = "once";
export async function renderJobs() {
const app = document.getElementById("app");
@@ -33,11 +38,35 @@ export async function renderJobs() {
</div>
<a class="btn btn-primary btn-sm" href="#/jobs/new">${t("jobs.new_btn")}</a>
</div>
<div class="toolbar">
<div class="segmented" role="tablist" aria-label="${t("jobs.view_filter")}">
<label>
<input type="radio" name="jobsView" value="once" ${currentJobsView === "once" ? "checked" : ""}>
<span>${t("jobs.view_once")}</span>
</label>
<label>
<input type="radio" name="jobsView" value="recurring" ${currentJobsView === "recurring" ? "checked" : ""}>
<span>${t("jobs.view_recurring")}</span>
</label>
</div>
</div>
<div id="jobs-card" class="card-outline">
<p class="empty">${t("loading.title")}…</p>
</div>
`;
app.querySelectorAll('input[name="jobsView"]').forEach((input) => {
input.addEventListener("change", async () => {
currentJobsView = input.value;
const hasRunning = await refreshJobs();
if (hasRunning) {
startPolling();
} else {
stopPolling();
}
});
});
const hasRunning = await refreshJobs();
if (hasRunning) {
startPolling();
@@ -246,14 +275,18 @@ export async function renderNewJob() {
...schedule,
});
toast(t("jobs.recurring_saved", job && job.id), "success");
currentJobsView = "recurring";
location.hash = "#/jobs";
return;
}
const body = buildTransferBody(action, src, dst);
const res = await postAsync(`sync/${action}`, body);
const jobid = res && res.jobid;
toast(t("jobs.started_once", action, jobid), "success");
const job = await createOneTimeJob({
action,
src,
dst,
});
toast(t("jobs.started_once", action, job && (job.jobid || job.id)), "success");
currentJobsView = "once";
location.hash = "#/jobs";
} catch (e) {
toast(`${t("jobs.start_failed")}: ${e.message}`, "error");
@@ -276,20 +309,12 @@ function renderWeekdayOptions() {
}
function renderMonthdayOptions() {
return Array.from({ length: 31 }, (_, index) => {
const lastDay = `<option value="0">${t("jobs.monthday_last")}</option>`;
const numberedDays = Array.from({ length: 31 }, (_, index) => {
const day = index + 1;
return `<option value="${day}">${t("jobs.monthday", day)}</option>`;
}).join("");
}
function buildTransferBody(action, src, dst) {
const body = {
srcFs: src,
dstFs: dst,
_group: `${WEBGUI_JOB_GROUP_PREFIX}/${action}`,
};
if (action === "move") body.deleteEmptySrcDirs = true;
return body;
return lastDay + numberedDays;
}
function buildJobFs(location, rawPath) {
@@ -322,7 +347,7 @@ function readRecurringSchedule(form) {
}
if (scheduleKind === "monthly") {
schedule.scheduleMonthday = Number(form.elements.scheduleMonthday.value);
if (!Number.isInteger(schedule.scheduleMonthday) || schedule.scheduleMonthday < 1 || schedule.scheduleMonthday > 31) {
if (!Number.isInteger(schedule.scheduleMonthday) || schedule.scheduleMonthday < 0 || schedule.scheduleMonthday > 31) {
throw new Error(t("error.no_schedule_selected"));
}
}
@@ -457,7 +482,11 @@ async function refreshJobs() {
let jobs = [];
try {
jobs = await listRecurringJobs();
if (currentJobsView === "once") {
jobs = (await listOneTimeJobs()).map(normalizeOneTimeJob);
} else {
jobs = (await listRecurringJobs()).map(normalizeRecurringJob);
}
} catch (e) {
card.innerHTML = `
<div class="empty">
@@ -472,7 +501,7 @@ async function refreshJobs() {
if (jobs.length === 0) {
card.innerHTML = `
<div class="empty">
<h3>${t("jobs.empty_title")}</h3>
<h3>${currentJobsView === "once" ? t("jobs.empty_once_title") : t("jobs.empty_recurring_title")}</h3>
<p>${t("jobs.empty_body").replace("New Job", `<a href="#/jobs/new">${t("jobs.new_btn")}</a>`)}</p>
</div>
`;
@@ -481,22 +510,26 @@ async function refreshJobs() {
card.innerHTML = renderJobTable(jobs);
card.querySelectorAll("[data-detail]").forEach((btn) => {
btn.addEventListener("click", () => onToggleDetails(parseInt(btn.dataset.detail, 10)));
btn.addEventListener("click", () => onToggleDetails(btn.dataset.detail));
});
card.querySelectorAll("[data-start]").forEach((btn) => {
btn.addEventListener("click", () => onStartAgain(parseInt(btn.dataset.start, 10)));
btn.addEventListener("click", () => onStartAgain(btn.dataset.start));
});
card.querySelectorAll("[data-stop]").forEach((btn) => {
btn.addEventListener("click", () => onStop(parseInt(btn.dataset.stop, 10)));
btn.addEventListener("click", () => onStop(btn.dataset.stop));
});
card.querySelectorAll("[data-delete]").forEach((btn) => {
btn.addEventListener("click", () => onDelete(parseInt(btn.dataset.delete, 10)));
btn.addEventListener("click", () => onDelete(btn.dataset.delete));
});
return jobs.length > 0;
return jobs.some((job) => job.running);
}
function renderJobTable(statuses) {
statuses.sort((a, b) => (b.id ?? 0) - (a.id ?? 0));
statuses.sort((a, b) => {
const bTime = Date.parse(b.updatedAt || b.createdAt || b.startedAt || 0) || 0;
const aTime = Date.parse(a.updatedAt || a.createdAt || a.startedAt || 0) || 0;
return bTime - aTime;
});
const rows = statuses.map(renderJobRows).join("");
@@ -525,7 +558,7 @@ function renderJobTable(statuses) {
function renderJobRows(s) {
const row = renderJobRow(s);
if (!expandedJobs.has(Number(s.id))) {
if (!expandedJobs.has(jobKey(s))) {
return row;
}
return row + renderJobDetailsRow(s);
@@ -533,10 +566,13 @@ function renderJobRows(s) {
function renderJobRow(job) {
const snapshot = job.statusSnapshot || {};
const p = snapshot.progress || {};
const stats = transferStats(snapshot);
const id = job.id;
const key = jobKey(job);
const running = !!job.running;
const jobid = job.currentJobid || job.lastJobid;
const displayedSpeed = stats.speed || averageSpeed(stats);
const displayedEta = running ? stats.eta : 0;
const badge = renderStatusBadge(job.status);
const jobCell = `
@@ -551,44 +587,44 @@ function renderJobRow(job) {
const scheduleCell = `
<div class="job-cell">
<span class="job-action">${escapeHtml(formatDateTime(job.nextRunAt))}</span>
<span class="job-action">${escapeHtml(job.kind === "once" ? t("jobs.schedule_once") : formatDateTime(job.nextRunAt))}</span>
<span class="job-paths">${escapeHtml(formatSchedule(job))}</span>
</div>
`;
const pct = p && p.totalBytes > 0 ? Math.min(100, (p.bytes / p.totalBytes) * 100) : 0;
const pct = stats.totalBytes > 0 ? Math.min(100, (stats.bytes / stats.totalBytes) * 100) : 0;
const progress = `
<div style="display:flex;flex-direction:column;gap:4px;min-width:120px">
<div class="progress"><span style="width:${pct.toFixed(1)}%"></span></div>
<span class="col-mono" style="font-size:11px;color:var(--color-muted)">
${formatBytes(p.bytes)} / ${formatBytes(p.totalBytes)}
${formatBytes(stats.bytes)} / ${formatBytes(stats.totalBytes)}
</span>
</div>
`;
const detailBtn = `<button class="btn btn-secondary btn-sm" data-detail="${id}">${expandedJobs.has(Number(id)) ? t("jobs.hide_details") : t("jobs.details")}</button>`;
const detailBtn = `<button class="btn btn-secondary btn-sm" data-detail="${escapeAttr(key)}">${expandedJobs.has(key) ? t("jobs.hide_details") : t("jobs.details")}</button>`;
const startBtn = running
? ""
: `<button class="btn btn-secondary btn-sm" data-start="${id}">${t("jobs.start_again")}</button>`;
: `<button class="btn btn-secondary btn-sm" data-start="${escapeAttr(key)}">${t("jobs.start_again")}</button>`;
const stopBtn = running
? `<button class="btn btn-danger btn-sm" data-stop="${id}">${t("jobs.stop")}</button>`
? `<button class="btn btn-danger btn-sm" data-stop="${escapeAttr(key)}">${t("jobs.stop")}</button>`
: "";
const deleteBtn = `<button class="btn btn-danger btn-sm" data-delete="${id}">${t("jobs.delete_record")}</button>`;
const deleteBtn = `<button class="btn btn-danger btn-sm" data-delete="${escapeAttr(key)}">${t("jobs.delete_record")}</button>`;
return `
<tr>
<td class="col-mono">
${id}
${escapeHtml(displayJobId(job))}
${jobid ? `<br><span style="color:var(--color-muted-soft)">job ${escapeHtml(jobid)}</span>` : ""}
</td>
<td>${jobCell}</td>
<td>${badge}</td>
<td>${scheduleCell}</td>
<td class="col-num">${progress}</td>
<td class="col-num col-mono">${running ? escapeHtml(formatSpeed(p.speed || 0)) : "—"}</td>
<td class="col-num col-mono">${running ? escapeHtml(formatDuration(p.eta || 0)) : "—"}</td>
<td class="col-num col-mono">${p.transfers ?? 0} / ${p.totalTransfers ?? 0}</td>
<td class="col-num col-mono">${(p.errors && p.errors.length) || 0}</td>
<td class="col-num col-mono">${escapeHtml(formatSpeed(displayedSpeed))}</td>
<td class="col-num col-mono">${escapeHtml(formatDuration(displayedEta))}</td>
<td class="col-num col-mono">${escapeHtml(formatTransferCount(stats))}</td>
<td class="col-num col-mono">${escapeHtml(String(stats.errors || 0))}</td>
<td class="col-num">
<div class="job-actions">
${detailBtn}
@@ -604,6 +640,7 @@ function renderJobRow(job) {
function renderStatusBadge(status) {
switch (status) {
case "running":
case "starting":
return `<span class="badge">${t("jobs.status.running")}</span>`;
case "failed":
return `<span class="badge badge-error">${t("jobs.status.failed")}</span>`;
@@ -619,11 +656,11 @@ function renderStatusBadge(status) {
}
function renderJobDetailsRow(job) {
const id = job.id;
const snapshot = job.statusSnapshot || {};
const stats = transferStats(snapshot);
const jobid = job.currentJobid || job.lastJobid || "";
const details = [
[t("jobs.detail_schedule_id"), id],
[job.kind === "once" ? t("jobs.detail_record_id") : t("jobs.detail_schedule_id"), displayJobId(job)],
[t("jobs.detail_id"), jobid],
[t("jobs.detail_status"), detailStatus(job)],
[t("jobs.detail_error"), job.error || snapshot.error || t("jobs.detail_none")],
@@ -634,6 +671,11 @@ function renderJobDetailsRow(job) {
[t("jobs.detail_schedule"), formatSchedule(job)],
[t("jobs.detail_group"), snapshot.group || ""],
[t("jobs.detail_execute_id"), snapshot.executeId || ""],
[t("jobs.detail_bytes"), `${formatBytes(stats.bytes)} / ${formatBytes(stats.totalBytes)}`],
[t("jobs.detail_speed"), formatSpeed(stats.speed || averageSpeed(stats))],
[t("jobs.detail_eta"), formatDuration(job.running ? stats.eta : 0)],
[t("jobs.detail_files"), formatTransferCount(stats)],
[t("jobs.detail_errors"), String(stats.errors || 0)],
];
const output = snapshot && Object.keys(snapshot).length > 0
? JSON.stringify(snapshot, null, 2)
@@ -664,6 +706,54 @@ function renderJobDetailsRow(job) {
`;
}
function transferStats(snapshot) {
const stats = snapshot.stats || snapshot.progress || snapshot || {};
return {
bytes: numberOrZero(stats.bytes),
totalBytes: numberOrZero(stats.totalBytes),
speed: numberOrZero(stats.speed),
eta: numberOrNull(stats.eta),
transfers: numberOrZero(stats.transfers),
totalTransfers: numberOrZero(stats.totalTransfers),
checks: numberOrZero(stats.checks),
totalChecks: numberOrZero(stats.totalChecks),
transferTime: numberOrZero(stats.transferTime),
errors: normalizeErrors(stats.errors),
};
}
function averageSpeed(stats) {
if (stats.bytes > 0 && stats.transferTime > 0) {
return stats.bytes / stats.transferTime;
}
return 0;
}
function formatTransferCount(stats) {
if (stats.totalTransfers > 0) {
return `${stats.transfers} / ${stats.totalTransfers}`;
}
if (stats.totalChecks > 0) {
return `${stats.checks} / ${stats.totalChecks}`;
}
return String(stats.transfers || stats.checks || 0);
}
function normalizeErrors(errors) {
if (Array.isArray(errors)) return errors.length;
return numberOrZero(errors);
}
function numberOrZero(value) {
const n = Number(value);
return Number.isFinite(n) ? n : 0;
}
function numberOrNull(value) {
const n = Number(value);
return Number.isFinite(n) ? n : null;
}
function detailStatus(job) {
const status = job.status || "scheduled";
const key = `jobs.status.${status}`;
@@ -671,10 +761,16 @@ function detailStatus(job) {
}
async function onStop(jobid) {
if (!confirm(t("jobs.stop_confirm", jobid))) return;
const key = String(jobid);
const label = displayKey(key);
if (!confirm(t("jobs.stop_confirm", label))) return;
try {
await stopRecurringJob(jobid);
toast(t("jobs.stopped", jobid), "success");
if (key.startsWith("once:")) {
await stopOneTimeJob(oneTimeIdFromKey(key));
} else {
await stopRecurringJob(recurringIdFromKey(key));
}
toast(t("jobs.stopped", label), "success");
await refreshJobs();
} catch (e) {
toast(`${t("jobs.stop_failed")}: ${e.message}`, "error");
@@ -682,18 +778,26 @@ async function onStop(jobid) {
}
async function onToggleDetails(jobid) {
if (expandedJobs.has(jobid)) {
expandedJobs.delete(jobid);
const key = String(jobid);
if (expandedJobs.has(key)) {
expandedJobs.delete(key);
} else {
expandedJobs.add(jobid);
expandedJobs.add(key);
}
await refreshJobs();
}
async function onStartAgain(jobid) {
const key = String(jobid);
try {
await runRecurringJobNow(jobid);
toast(t("jobs.restarted", jobid), "success");
if (key.startsWith("once:")) {
const job = await runOneTimeJobNow(oneTimeIdFromKey(key));
toast(t("jobs.restarted_once", job && (job.jobid || job.id)), "success");
} else {
const id = recurringIdFromKey(key);
await runRecurringJobNow(id);
toast(t("jobs.restarted", id), "success");
}
await refreshJobs();
} catch (e) {
toast(`${t("jobs.restart_failed")}: ${e.message}`, "error");
@@ -701,10 +805,16 @@ async function onStartAgain(jobid) {
}
async function onDelete(jobid) {
if (!confirm(t("jobs.delete_confirm", jobid))) return;
const key = String(jobid);
const label = displayKey(key);
if (!confirm(t("jobs.delete_confirm", label))) return;
try {
await deleteRecurringJob(jobid);
expandedJobs.delete(jobid);
if (key.startsWith("once:")) {
await deleteOneTimeJob(oneTimeIdFromKey(key));
} else {
await deleteRecurringJob(recurringIdFromKey(key));
}
expandedJobs.delete(key);
await refreshJobs();
} catch (e) {
toast(`${t("jobs.delete_failed")}: ${e.message}`, "error");
@@ -736,6 +846,50 @@ export function stopJobPolling() {
stopPolling();
}
function normalizeRecurringJob(job) {
return {
...job,
kind: "recurring",
id: `recurring:${job.id}`,
scheduleId: job.id,
};
}
function normalizeOneTimeJob(job) {
return {
...job,
kind: "once",
id: `once:${job.id}`,
recordId: job.id,
};
}
function jobKey(job) {
return String(job.id);
}
function displayJobId(job) {
if (job.kind === "once") {
return t("jobs.once_id", job.recordId || String(job.id).replace(/^once:/, ""));
}
return job.scheduleId || String(job.id).replace(/^recurring:/, "");
}
function displayKey(key) {
if (key.startsWith("once:")) {
return t("jobs.once_id", key.replace(/^once:/, ""));
}
return key.replace(/^recurring:/, "");
}
function recurringIdFromKey(key) {
return Number(String(key).replace(/^recurring:/, ""));
}
function oneTimeIdFromKey(key) {
return Number(String(key).replace(/^once:/, ""));
}
function escapeHtml(s) {
return String(s)
.replace(/&/g, "&amp;")
@@ -757,6 +911,9 @@ function formatDateTime(value) {
}
function formatSchedule(job) {
if (job.kind === "once") {
return t("jobs.schedule_label_once");
}
const time = job.scheduleTime || "";
switch (job.scheduleKind) {
case "daily":
@@ -764,6 +921,9 @@ function formatSchedule(job) {
case "weekly":
return t("jobs.schedule_label_weekly", weekdayName(job.scheduleWeekday), time);
case "monthly":
if (Number(job.scheduleMonthday) === 0) {
return t("jobs.schedule_label_monthly_last", time);
}
return t("jobs.schedule_label_monthly", job.scheduleMonthday, time);
default:
return "";
+3 -3
View File
@@ -1,8 +1,8 @@
// views/remotes.js — connector-tile grid of configured remotes with CRUD.
import { post } from "../rc.js";
import { getState, setState, toast } from "../state.js?v=recurring-jobs-5";
import { t } from "../i18n.js?v=recurring-jobs-5";
import { post } from "../rc.js?v=job-stats-1";
import { getState, setState, toast } from "../state.js?v=job-stats-1";
import { t } from "../i18n.js?v=job-stats-1";
export async function renderRemotes() {
const app = document.getElementById("app");
+2 -2
View File
@@ -7,7 +7,7 @@
<link rel="icon" type="image/svg+xml" href="/assets/favicon.svg">
<link rel="stylesheet" href="/assets/styles/tokens.css">
<link rel="stylesheet" href="/assets/styles/base.css">
<link rel="stylesheet" href="/assets/styles/components.css?v=recurring-jobs-5">
<link rel="stylesheet" href="/assets/styles/components.css?v=job-stats-1">
</head>
<body>
<header class="top-nav">
@@ -80,6 +80,6 @@
<div id="toast-stack" class="toast-stack"></div>
<div id="modal-root"></div>
<script type="module" src="/assets/js/app.js?v=recurring-jobs-5"></script>
<script type="module" src="/assets/js/app.js?v=job-stats-1"></script>
</body>
</html>