From 4f6c0cc9d2285f151a13d4902dd09c32ec4bf0a9 Mon Sep 17 00:00:00 2001 From: sami Date: Sat, 12 Sep 2026 14:26:18 +0400 Subject: [PATCH] =?UTF-8?q?daemon:=20D3's=20remainder=20=E2=80=94=20rules.?= =?UTF-8?q?*,=20queue.reorder,=20schedule.*,=20limiter.*,=20download.updat?= =?UTF-8?q?e/refreshUrl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rules.list/rules.upsert: new store/rules.{hpp,cpp} (list in priority order; apply() upserts+removes atomically, generating an id when absent, per the schema's own "never leaves the table in a half-valid state"). Found and fixed along the way: migration 0001's rules table had no column for Rule.name at all — every rules.list/.upsert call failed outright ("no such column: name"), unit tests included, since :memory: migrates through the same path. Migration 0004 adds it. queue.reorder: new store::Queues::reorder — taskIds must be an exact permutation of the queue's current membership (compared as sorted sets) or nothing is written and -32602 names the queue; a valid permutation rewrites every member's queue_position in one transaction. schedule.get/schedule.set: a thin wrapper over the queues.schedule column (already read since D3b, never independently settable). nextRunAt is deliberately left unset — computing it needs the same local-time, DST-aware window logic sched/schedule_window.hpp's window_open() only has half of; called out rather than approximated, and the field is optional. limiter.get/limiter.set: backed by the same downloads.speedLimitEnabled/ downloads.speedLimitBps settings keys D9 already wired — one bag of truth, not two. The new part is reaching the engine: EnginePort/TaskActionPort gain set_global_speed_limit(bps) (0 = unlimited, TokenBucket's own convention), wired to Engine::rate_limiter().set_global_limit(). Pushed live on every limiter.set *and* on Scheduler::reload_config() so a limit from a previous run isn't silently unlimited again after a restart. applyToRunning is accepted but has no lever to pull differently — a single shared global bucket has no "next task only" variant. Also found, not chased further: the schema's "globalBps:0 with enabled:true means 'stop everything'" is the opposite of what TokenBucket does with rate_bps==0 (unlimited) — a real discrepancy, but the schema says the GUI must not offer that combination. download.update: "moving saveDir or filename moves the file on disk in the same operation" — resolved and root-checked like download.add's destination, then the .veloxpart/.veloxpart.meta pair (or the finished file, if complete) is moved via rename, falling back to copy+remove across filesystems, only when the resolved location actually differs. categoryId/queueId(appended to the new queue's run order)/description/segments/bufferBytes/checksum apply through new store::Tasks::apply_update. download.refreshUrl: same async server-layer special-case as download.probe (a real network round trip, same 30s deadline). Re-probes, flags contentChanged only when size or validator are both known and actually differ, persists the new URL and probe result, and swaps the URL on a live engine handle via a newly-widened EnginePort::refresh_url (now takes headers too, matching DownloadHandle's real signature — the seam had silently dropped them). Found and documented, not fixed: the generated parser collapses "field absent" and "field explicitly null" to the same nullopt for every optional patch field (DownloadUpdateParamsPatch, Settings) — both schemas document "an explicit null clears the field" but neither handler can act on it because the wire distinction is already gone by the time either sees the parsed struct. A generator-level gap (PROTO's), not something to hand-route around locally. Verified against real veloxd + tools/testserver: rules create/list, limiter.set takes effect and reads back, schedule.set/get round-trips, queue.reorder against real membership (and rejects a non-permutation), download.update renames+recategorizes a task, download.refreshUrl swaps a paused task's URL and reports contentChanged correctly. Full ctest: 55/55 (excluding the pre-existing, unrelated conformance failure noted two commits back). Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01GRDjHGgpYmMoPE2UFbe7pP --- daemon/docs/deferrals.md | 9 +- daemon/src/rpc/dispatcher.cpp | 247 +++++++++++++++++- daemon/src/rpc/task_action_port.hpp | 17 ++ daemon/src/rpc/uds_server.cpp | 35 +++ daemon/src/rpc/uds_server.hpp | 3 + daemon/src/rpc/ws_server.cpp | 35 +++ daemon/src/rpc/ws_server.hpp | 1 + daemon/src/sched/engine_port.hpp | 9 +- daemon/src/sched/engine_port_core.hpp | 8 +- daemon/src/sched/fake_engine_port.hpp | 14 +- daemon/src/sched/scheduler.cpp | 83 ++++++ daemon/src/sched/scheduler.hpp | 9 + .../src/store/migrations/0004_rules_name.sql | 10 + daemon/src/store/queues.cpp | 51 ++++ daemon/src/store/queues.hpp | 12 + daemon/src/store/tasks.cpp | 82 ++++++ daemon/src/store/tasks.hpp | 28 ++ daemon/tests/CMakeLists.txt | 1 + daemon/tests/dispatcher_misc_test.cpp | 244 +++++++++++++++++ daemon/tests/sched_scheduler_test.cpp | 69 +++++ 20 files changed, 948 insertions(+), 19 deletions(-) create mode 100644 daemon/src/store/migrations/0004_rules_name.sql create mode 100644 daemon/tests/dispatcher_misc_test.cpp diff --git a/daemon/docs/deferrals.md b/daemon/docs/deferrals.md index 4f14e2d..4e54da2 100644 --- a/daemon/docs/deferrals.md +++ b/daemon/docs/deferrals.md @@ -10,7 +10,14 @@ close. Kept here (not buried in commit messages) so the next pass can see them a | D1 | Pairing prompt is `EnvAutoApprover` (needs `VELOX_PAIR_AUTO=1`) | `rpc/pairing.hpp`, `main.cpp` | A GUI dialog / `org.freedesktop.Notifications` approver is integration work | Build step 7 (systemd + notifications) | | — | **D1, checked this pass, not attempted:** `libdbus-1-dev` (or `libsystemd-dev` for `sd-bus`) has no headers installed in this build environment — only the runtime `.so`s (`dpkg -l`/`apt-cache policy` confirm `libdbus-1-3` present, `libdbus-1-dev` not, "Candidate" available but not installed). A real notification-backed approver needs one of those linked into `veloxd`, which is a new build dependency for `daemon/CMakeLists.txt` (`find_package`/`pkg_check_modules`) and — since packaging manifests need to know about it too — arguably a decision to surface rather than something to reach for silently mid-session. `PairingApprover::approve()` is also still synchronous by shape (its own doc comment already says so: "the real notification-backed approver will run async and is not this shape") — swapping it for the async pattern this session built for `download.probe` (`rpc::TaskActionPort` + the server-layer deferred-reply special-case) is the right shape once there's a real implementation to justify the churn; reshaping the interface with nothing behind it yet would just be churn. Left `EnvAutoApprover` in place rather than build a fragile hand-rolled D-Bus wire client to avoid the missing headers — a broken pairing approver is worse than an honest stub. | `rpc/pairing.hpp` | missing dev headers + an undiscussed new dependency | once `libdbus-1-dev`/`libsystemd-dev` is available and the dependency is approved | | ~~D2~~ | **Closed** — `download.probe` is real on both transports. It's genuinely async (the engine's probe pool, up to the schema's 30s `x-deadlineMs`) and so cannot fit `VeloxDispatcher::on_download_probe`'s synchronous `HandlerResult` return — `uds_server.cpp`/`ws_server.cpp` special-case `"download.probe"` before the generic `dispatch()`, exactly the way they already special-case `session.hello`/`session.subscribe`, and queue the reply whenever the callback fires. `rpc::TaskActionPort::probe_now` (kept in proto/std terms, no `vdm::net::*`, so `veloxd_rpc` never needs `core/include`'s vdm headers) is what both transports call; `sched::Scheduler::probe_now` is the implementation — builds a `vdm::net::ProbeRequest`, runs it on the engine's probe pool, maps a failure to `-32013 ProbeFailed` (with `data.httpStatus` when there was one), and fills `suggestedCategoryId`/`suggestedSaveDir` with a plain extension match against the categories table (not the real rules engine — that's still D3). Verified live: a real probe answers in ~5ms; a bad host maps to `-32013`; a connection issuing a 10s `slow-loris` probe does not block a second connection's `download.list` (answered in ~1ms) — confirms the async design actually keeps the loop free, not just compiles. | `rpc/task_action_port.hpp`, `rpc/{uds_server,ws_server}.{hpp,cpp}`, `sched/scheduler.{cpp,hpp}` | — | done | -| D3 | Stub handlers for the rest: `download.refreshUrl/update`, `rules.*`, `limiter.*`, `schedule.*`, `queue.reorder`, `grabber.*`, `media.*` | `rpc/dispatcher.cpp` | No store/scheduler wiring behind them yet | Per method, as each wires to the store/scheduler | +| D3 | Stub handlers for the rest: `grabber.*`, `media.*` | `rpc/dispatcher.cpp` | HLS/DASH grabber and media-variant support don't exist anywhere in this build yet — a bigger feature than a store-wiring pass | M4 territory, per AGENT-DAEMON.md | +| ~~D3d~~ | **Closed — `rules.list`/`rules.upsert`.** New `store/rules.{hpp,cpp}`: `list()` in priority order, `apply(upsert, remove)` in one transaction (an empty `ruleId` generates one; a reprioritisation and a removal land atomically, per the schema's own "never leaves the table in a half-valid state"). Found and fixed along the way: migration `0001`'s `rules` table had no column for `Rule.name` at all — every `rules.list`/`.upsert` call failed outright ("no such column: name") the first time either ran against a real `Db`, unit tests included, since `:memory:` migrates through the same path. Migration `0004` adds it. | `store/rules.{hpp,cpp}`, `rpc/dispatcher.cpp`, `store/migrations/0004_*.sql` | — | done | +| ~~D3e~~ | **Closed — `queue.reorder`.** New `store::Queues::reorder`: `taskIds` must be an exact permutation of the queue's current membership (compared as sorted sets) or nothing is written and `-32602` names the queue; a valid permutation rewrites every member's `queue_position` in one transaction. | `store/queues.{hpp,cpp}`, `rpc/dispatcher.cpp` | — | done | +| ~~D3f~~ | **Closed — `schedule.get`/`schedule.set`.** Thin wrapper over the `queues.schedule` column that already existed (`Queue.schema.json`'s own field, read since D3b but never independently settable). `nextRunAt` is deliberately left unset: computing it correctly needs the same local-time, DST-aware window logic `sched/schedule_window.hpp`'s `window_open()` only has half of (is-it-open-right-now, not next-transition) — real work, called out rather than approximated. The field is optional; `nullopt` is a legal answer. | `store/queues.{hpp,cpp}`, `rpc/dispatcher.cpp` | `nextRunAt` unset (documented, not silently wrong) | `nextRunAt` is its own pass | +| ~~D3g~~ | **Closed — `limiter.get`/`limiter.set`.** Backed by the same `downloads.speedLimitEnabled`/`downloads.speedLimitBps` settings keys D9 already wired (one bag of truth, not two) — the *new* part is actually reaching the engine: `EnginePort`/`TaskActionPort` gain `set_global_speed_limit(bps)` (`0` = unlimited, `vdm::rate::TokenBucket`'s own convention), wired to `vdm::Engine::rate_limiter().set_global_limit()`. Pushed live on every `limiter.set` *and* on `Scheduler::reload_config()` (so a limit from a previous run isn't silently unlimited again after a restart — nothing else re-derives it from settings the way `connection.*` already does). `applyToRunning` is accepted but has no lever to pull differently: a single shared global bucket has no "next task only" variant, so this always behaves as if it were `true` — documented in `EnginePort::set_global_speed_limit`'s own comment. Also found, not chased further: the schema's "`globalBps: 0` with `enabled: true` means 'stop everything'" is the *opposite* of what `TokenBucket` does with `rate_bps == 0` (unlimited) — a real discrepancy, but the schema itself says the GUI "must not offer" that combination, so nothing sends it in practice. | `sched/engine_port*.hpp`, `rpc/task_action_port.hpp`, `sched/scheduler.{cpp,hpp}`, `rpc/dispatcher.cpp` | the `globalBps:0` semantic clash noted above; `applyToRunning` has no real effect | flagged for whoever owns the schema/CORE conversation next | +| ~~D3h~~ | **Closed — `download.update`.** "Moving `saveDir` or `filename` moves the file on disk in the same operation" (the schema's own words): resolved and root-checked exactly like `download.add`'s destination, then the `.veloxpart`/`.veloxpart.meta` pair (or the finished file, if the task is `complete`) is moved with `std::filesystem::rename`, falling back to copy+remove across filesystems (`EXDEV`) — only if the resolved location actually differs from where the task already is. `categoryId`/`queueId`(appended to the end of the new queue's run order)/`description`/`segments`(1-32)/`bufferBytes`(64 KiB-16 MiB)/`checksum` all apply through a new `store::Tasks::apply_update`. New `store::Tasks::UpdatePatch`/`apply_update` and `Tasks::set_url` (download.refreshUrl's own need, split out since neither `apply_update` nor `set_probe_result` owns the base `url` column). | `store/tasks.{hpp,cpp}`, `rpc/dispatcher.cpp` | see the shared note below (null-clearing) | done | +| ~~D3i~~ | **Closed — `download.refreshUrl`.** Same async reasoning and the same server-layer special-case (`uds_server.cpp`/`ws_server.cpp` intercept before generic `dispatch()`, exactly like `download.probe`) — a real network round trip, same 30s `x-deadlineMs`. Re-probes the new URL, flags `contentChanged` only when size or validator are *both* known and actually differ (an unknown value on either side is never itself a mismatch — "it says so rather than silently restarting" needs a real disagreement, not an absent comparison), persists the new URL and probe result, and — if the task holds a live engine handle — swaps the URL in place via a newly-widened `EnginePort::refresh_url` (now takes headers too, matching `DownloadHandle::refresh_url`'s real signature; the seam had silently dropped them). Verified against real `veloxd` + `tools/testserver`, live-handle swap covered by a `sched_scheduler_test` case (asserts the engine got `refresh_url()`, not a fresh `start()`). | `sched/engine_port*.hpp`, `rpc/task_action_port.hpp`, `sched/scheduler.{cpp,hpp}`, `rpc/{uds_server,ws_server}.{hpp,cpp}` | — | done | +| — | **Shared note across D3h/download.update and D9/settings.set:** the generated parser collapses "field absent" and "field explicitly `null`" to the same `std::optional::nullopt` for every `optional` patch field (`DownloadUpdateParamsPatch`, `Settings`) — there is no second bit on the wire path that survives into `VeloxDispatcher`. Both schemas document "an explicit null clears the field," but neither handler can act on that distinction because the information is already gone by the time either sees the parsed struct. Not something to route around locally (would mean hand-parsing raw JSON past the generated `parse` for a handful of fields) — this is a generator-level gap, PROTO's to close (e.g. `std::optional>`, or a parallel "which fields were present" bitset). Until then: `categoryId`/`queueId`/`description`/`checksum` on `download.update`, and every nullable `Settings` key, can be *set* through these RPCs but never explicitly cleared back to null. | `contracts/` (generator), affects `rpc/dispatcher.cpp` | the wire distinction the schema documents doesn't survive to the handler | PROTO's generator | | ~~D9~~ | **Closed — `settings.get`/`settings.set`.** The field <-> `SettingKey` <-> JSON-type mapping is four pointer-to-member tables in `dispatcher.cpp` (one per C++ field type: bool, ranged int, plain string, string array) plus five enum-typed keys handled individually (`parse_XXX` already validates those); every one of the 43 `SettingKey`s now has a real default (`store::Settings::kDefaults` grew from 14 entries to 43 — `capture.monitoredExtensions`'s default is the union of every builtin category's extensions, so the two never drift apart). `settings.get` honors `keys: null` = everything. `settings.set` validates every field *before* writing any of them (numeric min/max — the schema itself carries none of this, so it's hand-checked against each key's documented range; `-32602` names the offending key, its value, and its bounds) and validates `saveTo.*` paths against `fs::resolve_target`/`canonicalize_root` (`-32011`) — `saveTo.allowedRoots` entries are checked as roots in their own right, `saveTo.defaultDir`/`.tempDir` are checked as paths resolving *inside* the (possibly, in the same call, just-updated) root list. Reports exactly the keys whose *effective* value actually changed (a `set` to the value already in effect reports `changed: []`, not the key), publishes `event.settings.changed` with that same list, and calls `TaskActionPort::apply_settings_reload()` (-> `Scheduler::reload_config()`) when any `connection.*` key took effect, live rather than waiting for a restart. Verified against real `veloxd`: all 43 keys round-trip with sane defaults, a `keys` subset filters correctly, an out-of-range value is rejected with nothing else in the same call landing, `saveTo.defaultDir` outside every allowed root is `-32011`, setting `allowedRoots` and `defaultDir` together cross-validates against the *new* roots, and `event.settings.changed` fires over a live subscription. New `dispatcher_settings_test` covers the same ground without a socket. | `rpc/dispatcher.cpp`, `store/settings.{hpp,cpp}`, `rpc/task_action_port.hpp`, `sched/scheduler.hpp` | — | done | | ~~D3a~~ | **Closed** — `category.upsert`/`category.remove`: `store/categories.hpp` gains `get`/`upsert`/`remove`. `upsert` generates an id when absent (create) and always ignores the payload's `builtin` (preserved from the existing row on replace, false on create — a client can never mint or revoke it); the `saveDir` goes through the same `fs::resolve_target` canonicalize-and-root-check as `download.add` (`-32011` on failure). `remove` refuses a builtin at both layers (dispatcher pre-checks for the `-32602` error text; the store's own `DELETE ... AND builtin = 0` is defense in depth) and reassigns member tasks to `reassignTo` (default `"general"`) inside one transaction before deleting the row. Note: the `categories` table (0001) has no columns for `Category.mimeTypes`/`.sortOrder` — accepted on `upsert` but not persisted. | `store/categories.{hpp,cpp}`, `rpc/dispatcher.cpp` | — | done, `mimeTypes`/`sortOrder` gap noted | | ~~D3b~~ | **Closed** — `queue.upsert`: `store/queues.hpp` gains `get`/`upsert` (`set_state` already existed from D4b). Same create-generates-id pattern as categories; `taskIds` in the payload is ignored (schema's own note) and a create always starts `'stopped'` while a replace keeps the queue's current run state — `queue.upsert` edits config, not run state (that's `queue.start`/`stop`). Also fixed: `on_complete` was a real column since 0001 but `Queues::list`/`get` never projected it onto `Queue.onComplete` — now they do. | `store/queues.{hpp,cpp}`, `rpc/dispatcher.cpp` | — | done | diff --git a/daemon/src/rpc/dispatcher.cpp b/daemon/src/rpc/dispatcher.cpp index c29a4b3..8b780ea 100644 --- a/daemon/src/rpc/dispatcher.cpp +++ b/daemon/src/rpc/dispatcher.cpp @@ -819,8 +819,120 @@ VeloxDispatcher::on_download_start(const proto::DownloadStartParams& params) { return result; } proto::HandlerResult -VeloxDispatcher::on_download_update(const proto::DownloadUpdateParams&) { - return not_implemented("download.update"); +VeloxDispatcher::on_download_update(const proto::DownloadUpdateParams& params) { + store::Tasks tasks(db_); + auto got = tasks.get(params.taskId); + if (!got) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "download.update: " + got.error().message}); + if (!got->has_value()) + return std::unexpected(proto::HandlerError{proto::ErrorCode::TaskNotFound, "no such task", + nlohmann::json{{"taskId", params.taskId}}}); + const store::TaskRow& row = **got; + const auto& patch = params.patch; + + if (patch.segments && (*patch.segments < 1 || *patch.segments > 32)) { + return std::unexpected(proto::HandlerError{ + proto::ErrorCode::InvalidParams, "segments must be between 1 and 32", + nlohmann::json{{"value", *patch.segments}}}); + } + if (patch.bufferBytes && (*patch.bufferBytes < 65536 || *patch.bufferBytes > 16777216)) { + return std::unexpected(proto::HandlerError{ + proto::ErrorCode::InvalidParams, "bufferBytes must be between 65536 and 16777216", + nlohmann::json{{"value", *patch.bufferBytes}}}); + } + + store::Tasks::UpdatePatch update; + + // "Moving saveDir or filename moves the file on disk in the same operation" (the + // schema's own words). Resolved and root-checked exactly like download.add's own + // destination; only actually touches the filesystem if the resolved location differs + // from where the task already is. + if (patch.filename || patch.saveDir) { + store::Settings settings(db_); + std::string dir = expand_tilde(patch.saveDir.value_or(row.save_dir)); + std::string leaf = patch.filename.value_or(row.filename); + std::vector roots; + for (const auto& r : settings.get_string_array("saveTo.allowedRoots")) + if (auto c = fs::canonicalize_root(r)) roots.push_back(*c); + + auto target = fs::resolve_target(dir, leaf, roots); + if (!target) { + return std::unexpected(proto::HandlerError{ + proto::ErrorCode::InvalidPath, target.error().message, + nlohmann::json{{"path", patch.saveDir.value_or(dir)}}}); + } + + if (target->dir != row.save_dir || target->leaf != row.filename) { + namespace fsn = std::filesystem; + const std::string old_base = row.save_dir + "/" + row.filename; + const std::string new_base = target->dir + "/" + target->leaf; + + // move_one: rename, falling back to copy+remove across filesystems (EXDEV). + // Missing source (nothing to move at this path yet) is not an error. + auto move_one = [](const std::string& from, const std::string& to) -> bool { + std::error_code ec; + if (!fsn::exists(from, ec)) return true; + fsn::rename(from, to, ec); + if (!ec) return true; + ec.clear(); + fsn::copy_file(from, to, fsn::copy_options::overwrite_existing, ec); + if (ec) return false; + fsn::remove(from, ec); + return true; + }; + + const bool moved = row.state == "complete" + ? move_one(old_base, new_base) + : (move_one(old_base + ".veloxpart", new_base + ".veloxpart") && + move_one(old_base + ".veloxpart.meta", new_base + ".veloxpart.meta")); + if (!moved) { + return std::unexpected(proto::HandlerError{ + proto::ErrorCode::InternalError, "could not move the file to its new location", + nlohmann::json{{"path", new_base}}}); + } + update.save_dir = target->dir; + update.filename = target->leaf; + } + } + + if (patch.categoryId) update.category_id = patch.categoryId; + if (patch.queueId) { + update.queue_id = patch.queueId; + // Appended to the end of the new queue's run order. + auto page = tasks.list( + [&] { + proto::TaskFilter f; + f.queueId = *patch.queueId; + return f; + }(), + std::nullopt, 0, 10000); + std::int64_t next_pos = 0; + if (page) + for (const auto& r : page->rows) + if (r.queue_position) next_pos = std::max(next_pos, *r.queue_position + 1); + update.queue_position = next_pos; + } + if (patch.description) update.description = patch.description; + if (patch.segments) update.req_segments = patch.segments; + if (patch.bufferBytes) update.req_buffer_bytes = patch.bufferBytes; + if (patch.checksum) { + update.checksum_algo = std::string(proto::to_string(patch.checksum->algorithm)); + update.checksum_value = patch.checksum->value; + } + + if (auto r = tasks.apply_update(params.taskId, update); !r) { + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "download.update: " + r.error().message}); + } + + auto after = tasks.get(params.taskId); + if (!after || !after->has_value()) { + return std::unexpected( + proto::HandlerError{proto::ErrorCode::InternalError, "download.update: row vanished"}); + } + if (on_mutation_) on_mutation_(); + return store::to_summary(**after); } proto::HandlerResult VeloxDispatcher::on_grabber_harvest(const proto::GrabberHarvestParams&) { @@ -835,10 +947,24 @@ VeloxDispatcher::on_grabber_status(const proto::GrabberStatusParams&) { return not_implemented("grabber.status"); } proto::HandlerResult VeloxDispatcher::on_limiter_get(const proto::LimiterGetParams&) { - return not_implemented("limiter.get"); + store::Settings settings(db_); + proto::Limiter r; + r.enabled = settings.get_bool("downloads.speedLimitEnabled"); + r.globalBps = settings.get_int("downloads.speedLimitBps"); + return r; } -proto::HandlerResult VeloxDispatcher::on_limiter_set(const proto::Limiter&) { - return not_implemented("limiter.set"); +proto::HandlerResult VeloxDispatcher::on_limiter_set(const proto::Limiter& params) { + store::Settings settings(db_); + (void)settings.set_raw("downloads.speedLimitEnabled", params.enabled ? "true" : "false"); + (void)settings.set_raw("downloads.speedLimitBps", std::to_string(params.globalBps)); + + // The bucket's own convention: 0 == unlimited. `enabled: false` means "no limit" + // regardless of what globalBps happens to hold (the GUI is expected to keep it around + // for when the user re-enables it, not to reset it to 0). + if (actions_) actions_->set_global_speed_limit(params.enabled ? static_cast(params.globalBps) : 0); + + if (on_mutation_) on_mutation_(); + return params; } proto::HandlerResult VeloxDispatcher::on_media_addVariant(const proto::MediaAddVariantParams&) { @@ -860,8 +986,33 @@ VeloxDispatcher::on_queue_list(const proto::QueueListParams&) { return r; } proto::HandlerResult -VeloxDispatcher::on_queue_reorder(const proto::QueueReorderParams&) { - return not_implemented("queue.reorder"); +VeloxDispatcher::on_queue_reorder(const proto::QueueReorderParams& params) { + store::Queues queues(db_); + auto exists = queues.get(params.queueId); + if (!exists) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "queue.reorder: " + exists.error().message}); + if (!exists->has_value()) + return std::unexpected(proto::HandlerError{ + proto::ErrorCode::InvalidParams, "no such queue", + nlohmann::json{{"queueId", params.queueId}}}); + + auto applied = queues.reorder(params.queueId, params.taskIds); + if (!applied) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "queue.reorder: " + applied.error().message}); + if (!*applied) { + return std::unexpected(proto::HandlerError{ + proto::ErrorCode::InvalidParams, + "taskIds must be an exact permutation of the queue's current membership", + nlohmann::json{{"queueId", params.queueId}}}); + } + + auto after = queues.get(params.queueId); + proto::QueueReorderResult r; + if (after && after->has_value()) r.queue = **after; + if (on_mutation_) on_mutation_(); + return r; } proto::HandlerResult VeloxDispatcher::on_queue_start(const proto::QueueStartParams& params) { @@ -926,19 +1077,87 @@ VeloxDispatcher::on_queue_upsert(const proto::QueueUpsertParams& params) { } proto::HandlerResult VeloxDispatcher::on_rules_list(const proto::RulesListParams&) { - return not_implemented("rules.list"); + store::Rules rules(db_); + auto items = rules.list(); + if (!items) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "rules.list: " + items.error().message}); + proto::RulesListResult r; + r.items = std::move(*items); + return r; } proto::HandlerResult -VeloxDispatcher::on_rules_upsert(const proto::RulesUpsertParams&) { - return not_implemented("rules.upsert"); +VeloxDispatcher::on_rules_upsert(const proto::RulesUpsertParams& params) { + store::Rules rules(db_); + auto items = rules.apply(params.upsert, params.remove.value_or(std::vector{})); + if (!items) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "rules.upsert: " + items.error().message}); + proto::RulesUpsertResult r; + r.items = std::move(*items); + return r; } proto::HandlerResult -VeloxDispatcher::on_schedule_get(const proto::ScheduleGetParams&) { - return not_implemented("schedule.get"); +VeloxDispatcher::on_schedule_get(const proto::ScheduleGetParams& params) { + store::Queues queues(db_); + proto::ScheduleGetResult r; + if (params.queueId) { + auto q = queues.get(*params.queueId); + if (!q) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "schedule.get: " + q.error().message}); + if (q->has_value()) { + proto::ScheduleGetResultItemsItem item; + item.queueId = (*q)->queueId; + item.schedule = (*q)->schedule; + r.items.push_back(std::move(item)); + } + // No such queue: an empty items list rather than an error — same "the caller asked + // about something specific and there's nothing to say about it" shape download.get + // uses -32010 for, but schedule.get's own x-errors lists only -32003, so an empty + // result (not "every schedule", just this one queue's, which doesn't exist) is the + // faithful answer within what the schema actually allows. + return r; + } + + auto all = queues.list(); + if (!all) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "schedule.get: " + all.error().message}); + for (const auto& q : *all) { + proto::ScheduleGetResultItemsItem item; + item.queueId = q.queueId; + item.schedule = q.schedule; + r.items.push_back(std::move(item)); + } + return r; } proto::HandlerResult -VeloxDispatcher::on_schedule_set(const proto::ScheduleSetParams&) { - return not_implemented("schedule.set"); +VeloxDispatcher::on_schedule_set(const proto::ScheduleSetParams& params) { + store::Queues queues(db_); + auto exists = queues.get(params.queueId); + if (!exists) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "schedule.set: " + exists.error().message}); + if (!exists->has_value()) + return std::unexpected(proto::HandlerError{ + proto::ErrorCode::InvalidParams, "no such queue", + nlohmann::json{{"queueId", params.queueId}}}); + + auto applied = queues.set_schedule(params.queueId, params.schedule); + if (!applied) + return std::unexpected(proto::HandlerError{proto::ErrorCode::InternalError, + "schedule.set: " + applied.error().message}); + + proto::ScheduleSetResult r; + r.queueId = params.queueId; + r.schedule = params.schedule; + // nextRunAt is left unset: computing it correctly needs the same local-time, + // DST-aware window logic sched/schedule_window.hpp's window_open() already has half + // of (open-right-now, not next-transition) — worth building as its own pass rather + // than an approximate guess here. The field is optional; nullopt is a legal answer. + if (on_mutation_) on_mutation_(); + return r; } proto::HandlerResult VeloxDispatcher::on_settings_get(const proto::SettingsGetParams& params) { diff --git a/daemon/src/rpc/task_action_port.hpp b/daemon/src/rpc/task_action_port.hpp index f0aa6c8..8089277 100644 --- a/daemon/src/rpc/task_action_port.hpp +++ b/daemon/src/rpc/task_action_port.hpp @@ -67,6 +67,19 @@ public: std::function)> done) = 0; + // download.refreshUrl ("IDM's 'Refresh Download Address'"): same async reasoning and + // the same server-layer special-case as probe_now — a real network round trip, up to + // the schema's own 30s x-deadlineMs. Re-probes the new URL, compares size/validator + // against what the task already has on record (contentChanged), persists the new URL + // and probe result, and — if the task holds a live engine handle — swaps its URL + // in-flight without losing progress. + virtual void refresh_url( + const std::string& wire_id, const std::string& url, + const std::optional& headers, + const std::optional>& cookies, + std::function)> + done) = 0; + // settings.set of a connection.* key: re-read connection.maxConcurrentDownloads / // maxActiveSegments and the per-host cap table, push the new caps to the engine and // the governor (Scheduler::reload_config's own doc comment names this exact trigger). @@ -74,6 +87,10 @@ public: // sched::Scheduler's own already-public reload_config() (returns DbResult, // consumed by main.cpp and a unit test — kept as-is rather than reshaped to fit here). virtual void apply_settings_reload() = 0; + + // limiter.set: pushed straight to the engine's shared global token bucket. 0 means + // unlimited (the bucket's own convention). + virtual void set_global_speed_limit(std::uint64_t bps) = 0; }; } // namespace velox::daemon::rpc diff --git a/daemon/src/rpc/uds_server.cpp b/daemon/src/rpc/uds_server.cpp index aaf0ac4..2362ad5 100644 --- a/daemon/src/rpc/uds_server.cpp +++ b/daemon/src/rpc/uds_server.cpp @@ -207,6 +207,10 @@ void UdsServer::handle_line(Conn& c, const std::string& line) { handle_download_probe(c, req); return; } + if (method == "download.refreshUrl") { + handle_download_refreshUrl(c, req); + return; + } // Everything else: the generated router. It returns a null json for a notification // that needs no reply. @@ -244,6 +248,37 @@ void UdsServer::handle_download_probe(Conn& c, const json& request) { }); } +void UdsServer::handle_download_refreshUrl(Conn& c, const json& request) { + const json id = request.contains("id") ? request.at("id") : json(nullptr); + const json params_json = request.contains("params") ? request.at("params") : json::object(); + + auto parsed = proto::parse(params_json, "params"); + if (!parsed) { + queue_reply(c, rpc_error(id, proto::ErrorCode::InvalidParams, parsed.error().message, + json{{"path", parsed.error().path}})); + return; + } + if (!actions_) { + queue_reply(c, rpc_error(id, proto::ErrorCode::InternalError, + "not implemented in this build: download.refreshUrl")); + return; + } + + const int fd = c.fd; + actions_->refresh_url( + parsed->taskId, parsed->url, parsed->headers, parsed->cookies, + [this, fd, id](proto::HandlerResult r) { + auto it = conns_.find(fd); + if (it == conns_.end()) return; // client gone while the probe was outstanding + if (r) { + queue_reply(*it->second, proto::make_result(id, *r)); + } else { + queue_reply(*it->second, + rpc_error(id, r.error().code, r.error().message, r.error().data)); + } + }); +} + bool UdsServer::handle_session_method(Conn& c, const std::string& method, const json& request, json& reply) { const json id = request.contains("id") ? request.at("id") : json(nullptr); diff --git a/daemon/src/rpc/uds_server.hpp b/daemon/src/rpc/uds_server.hpp index d76f7a8..a196c0a 100644 --- a/daemon/src/rpc/uds_server.hpp +++ b/daemon/src/rpc/uds_server.hpp @@ -76,6 +76,9 @@ private: // Queues the reply itself, later, when actions_->probe_now()'s callback fires; does // nothing if the connection is gone by then (client disconnected mid-probe). void handle_download_probe(Conn& c, const nlohmann::json& request); + // Same reasoning as handle_download_probe — a real network round trip, same 30s + // x-deadlineMs. + void handle_download_refreshUrl(Conn& c, const nlohmann::json& request); void queue_reply(Conn& c, const nlohmann::json& reply); void flush(Conn& c); diff --git a/daemon/src/rpc/ws_server.cpp b/daemon/src/rpc/ws_server.cpp index 875bee5..25c46b2 100644 --- a/daemon/src/rpc/ws_server.cpp +++ b/daemon/src/rpc/ws_server.cpp @@ -267,6 +267,10 @@ void WsServer::handle_rpc(Conn& c, const std::string& text) { handle_download_probe(c, req); return; } + if (method == "download.refreshUrl") { + handle_download_refreshUrl(c, req); + return; + } json reply = proto::dispatch(dispatcher_, proto::Transport::Ws, req); if (!reply.is_null()) send_text(c, reply); @@ -302,6 +306,37 @@ void WsServer::handle_download_probe(Conn& c, const json& request) { }); } +void WsServer::handle_download_refreshUrl(Conn& c, const json& request) { + const json id = request.contains("id") ? request.at("id") : json(nullptr); + const json params_json = request.contains("params") ? request.at("params") : json::object(); + + auto parsed = proto::parse(params_json, "params"); + if (!parsed) { + send_text(c, rpc_error(id, proto::ErrorCode::InvalidParams, parsed.error().message, + json{{"path", parsed.error().path}})); + return; + } + if (!actions_) { + send_text(c, rpc_error(id, proto::ErrorCode::InternalError, + "not implemented in this build: download.refreshUrl")); + return; + } + + const int fd = c.fd; + actions_->refresh_url( + parsed->taskId, parsed->url, parsed->headers, parsed->cookies, + [this, fd, id](proto::HandlerResult r) { + auto it = conns_.find(fd); + if (it == conns_.end()) return; // client gone while the probe was outstanding + if (r) { + send_text(*it->second, proto::make_result(id, *r)); + } else { + send_text(*it->second, + rpc_error(id, r.error().code, r.error().message, r.error().data)); + } + }); +} + bool WsServer::handle_session_ws(Conn& c, const std::string& method, const json& request, json& reply) { const json id = request.contains("id") ? request.at("id") : json(nullptr); diff --git a/daemon/src/rpc/ws_server.hpp b/daemon/src/rpc/ws_server.hpp index bc71864..ce4d78e 100644 --- a/daemon/src/rpc/ws_server.hpp +++ b/daemon/src/rpc/ws_server.hpp @@ -81,6 +81,7 @@ private: // See UdsServer::handle_download_probe — same reasoning, same pattern, duplicated per // transport because each owns its own Conn/send mechanics. void handle_download_probe(Conn& c, const nlohmann::json& request); + void handle_download_refreshUrl(Conn& c, const nlohmann::json& request); void send_text(Conn& c, const nlohmann::json& value); void send_frame(Conn& c, WsOpcode op, std::string_view payload); diff --git a/daemon/src/sched/engine_port.hpp b/daemon/src/sched/engine_port.hpp index b487eb7..7c7ec87 100644 --- a/daemon/src/sched/engine_port.hpp +++ b/daemon/src/sched/engine_port.hpp @@ -46,7 +46,8 @@ public: virtual void provide_auth(vdm::TaskId, const std::string& username, const std::string& password, bool remember) = 0; virtual void decide(vdm::TaskId, vdm::task::Decision) = 0; - virtual void refresh_url(vdm::TaskId, const std::string& url) = 0; + virtual void refresh_url(vdm::TaskId, const std::string& url, + const std::vector& headers = {}) = 0; // The daemon is done with this task (it went terminal). Drop the handle. Idempotent. virtual void release(vdm::TaskId) = 0; @@ -59,6 +60,12 @@ public: virtual void set_task_order(const std::vector& order) = 0; virtual void set_max_active_segments(std::uint32_t n) = 0; virtual void set_host_segment_cap(const std::string& host, std::uint32_t cap) = 0; + + // limiter.set: 0 means unlimited (vdm::rate::TokenBucket's own convention), applied + // across every active transfer immediately — there is no "next task only" variant for + // a single shared global bucket, so `applyToRunning` on the wire has nothing to select + // between; it is accepted for schema compliance and always behaves as if true. + virtual void set_global_speed_limit(std::uint64_t bps) = 0; }; } // namespace velox::daemon::sched diff --git a/daemon/src/sched/engine_port_core.hpp b/daemon/src/sched/engine_port_core.hpp index 7c6fbe4..7084995 100644 --- a/daemon/src/sched/engine_port_core.hpp +++ b/daemon/src/sched/engine_port_core.hpp @@ -45,8 +45,9 @@ public: void decide(vdm::TaskId id, vdm::task::Decision d) override { if (auto* h = find(id)) h->decide(d); } - void refresh_url(vdm::TaskId id, const std::string& url) override { - if (auto* h = find(id)) h->refresh_url(url); + void refresh_url(vdm::TaskId id, const std::string& url, + const std::vector& headers) override { + if (auto* h = find(id)) h->refresh_url(url, headers); } void release(vdm::TaskId id) override { handles_.erase(id); } std::optional progress(vdm::TaskId id) const override { @@ -64,6 +65,9 @@ public: void set_host_segment_cap(const std::string& host, std::uint32_t cap) override { engine_.segment_budget().set_host_segment_cap(host, cap); } + void set_global_speed_limit(std::uint64_t bps) override { + engine_.rate_limiter().set_global_limit(bps); + } private: vdm::task::DownloadHandle* find(vdm::TaskId id) { diff --git a/daemon/src/sched/fake_engine_port.hpp b/daemon/src/sched/fake_engine_port.hpp index 1e88d81..8027b2a 100644 --- a/daemon/src/sched/fake_engine_port.hpp +++ b/daemon/src/sched/fake_engine_port.hpp @@ -61,7 +61,16 @@ public: void cancel(vdm::TaskId id, bool discard) override { cancelled.emplace_back(id, discard); } void provide_auth(vdm::TaskId, const std::string&, const std::string&, bool) override {} void decide(vdm::TaskId, vdm::task::Decision) override {} - void refresh_url(vdm::TaskId, const std::string&) override {} + void refresh_url(vdm::TaskId id, const std::string& url, + const std::vector& headers) override { + refreshed_urls.emplace_back(id, url, headers); + } + struct RefreshCall { + vdm::TaskId id; + std::string url; + std::vector headers; + }; + std::vector refreshed_urls; void release(vdm::TaskId id) override { released.push_back(id); } std::optional progress(vdm::TaskId id) const override { auto it = fake_progress.find(id.value); @@ -75,6 +84,9 @@ public: void set_host_segment_cap(const std::string& h, std::uint32_t c) override { host_caps.emplace_back(h, c); } + void set_global_speed_limit(std::uint64_t bps) override { global_speed_limits.push_back(bps); } + + std::vector global_speed_limits; const std::vector& last_order() const { return orders.back(); } diff --git a/daemon/src/sched/scheduler.cpp b/daemon/src/sched/scheduler.cpp index ec3705a..d13b772 100644 --- a/daemon/src/sched/scheduler.cpp +++ b/daemon/src/sched/scheduler.cpp @@ -183,6 +183,16 @@ store::DbResult Scheduler::reload_config() { governor_.set_config(cfg); engine_.set_max_active_segments( static_cast(std::max(cfg.max_active_segments, 1))); + + // The global speed limit persists across a restart the same as any other setting, but + // (unlike connection.* above) nothing re-derives it into engine state on its own — + // limiter.set is the only other place that calls set_global_speed_limit, and that only + // fires on an explicit RPC in a running daemon. Push it here too so a limit set in a + // previous run is not silently unlimited again after a restart. + const bool limit_enabled = settings.get_bool("downloads.speedLimitEnabled"); + const std::int64_t limit_bps = settings.get_int("downloads.speedLimitBps"); + engine_.set_global_speed_limit(limit_enabled ? static_cast(std::max(limit_bps, 0)) + : 0); return {}; } @@ -634,4 +644,77 @@ void Scheduler::probe_now( }); } +void Scheduler::refresh_url( + const std::string& wire_id, const std::string& url, + const std::optional& headers, + const std::optional>& cookies, + std::function)> done) { + auto got = store::Tasks(db_).get(wire_id); + if (!got || !got->has_value()) { + done(std::unexpected(proto::HandlerError{proto::ErrorCode::TaskNotFound, "no such task", + nlohmann::json{{"taskId", wire_id}}})); + return; + } + const store::TaskRow row = **got; // copied: read again by the async callback below + + vdm::net::ProbeRequest req; + req.url = url; + if (headers) + for (const auto& [k, v] : *headers) req.headers.push_back({k, v}); + if (cookies) + for (const auto& c : *cookies) req.cookies.push_back({c.name, c.value}); + + engine_.probe(req, [this, wire_id, row, url, headers, done](vdm::Result pr) { + deps_.post_to_loop([this, wire_id, row, url, headers, done, pr]() { + if (!pr) { + nlohmann::json data; + if (pr.error().http_status != 0) data["httpStatus"] = pr.error().http_status; + done(std::unexpected(proto::HandlerError{proto::ErrorCode::ProbeFailed, + pr.error().context, data})); + return; + } + + // "if they do not [match], it says so rather than silently restarting" (the + // schema's own words) — comparison only fires when both sides actually have a + // value; an unknown size/validator on either end is not itself a mismatch. + bool content_changed = false; + if (row.size_bytes && pr->total_size && + *row.size_bytes != static_cast(*pr->total_size)) + content_changed = true; + if (row.etag && !row.etag->empty() && !pr->etag.empty() && *row.etag != pr->etag) + content_changed = true; + if (row.last_modified && !row.last_modified->empty() && !pr->last_modified.empty() && + *row.last_modified != pr->last_modified) + content_changed = true; + + store::Tasks tasks(db_); + store::Tasks::ProbeFields fields; + if (pr->total_size) fields.size_bytes = static_cast(*pr->total_size); + fields.resumable = pr->resumable; + if (!pr->etag.empty()) fields.etag = pr->etag; + if (!pr->last_modified.empty()) fields.last_modified = pr->last_modified; + if (!pr->mime.empty()) fields.content_type = pr->mime; + const std::string effective = pr->effective_url.empty() ? url : pr->effective_url; + fields.effective_url = effective; + (void)tasks.set_probe_result(wire_id, fields); + (void)tasks.set_url(wire_id, url); + + if (auto eid = engine_id_of(wire_id)) { + std::vector hdrs; + if (headers) + for (const auto& [k, v] : *headers) hdrs.push_back({k, v}); + engine_.refresh_url(*eid, url, hdrs); + } + + proto::DownloadRefreshUrlResult r; + r.ok = true; + r.resumable = pr->resumable; + r.contentChanged = content_changed; + if (pr->total_size) r.sizeBytes = static_cast(*pr->total_size); + r.effectiveUrl = effective; + done(r); + }); + }); +} + } // namespace velox::daemon::sched diff --git a/daemon/src/sched/scheduler.hpp b/daemon/src/sched/scheduler.hpp index fc68e53..afa4afd 100644 --- a/daemon/src/sched/scheduler.hpp +++ b/daemon/src/sched/scheduler.hpp @@ -141,6 +141,8 @@ public: // port, which has no caller that wants the DbError). void apply_settings_reload() override { (void)reload_config(); } + void set_global_speed_limit(std::uint64_t bps) override { engine_.set_global_speed_limit(bps); } + // rpc::TaskActionPort. Builds a vdm::net::ProbeRequest from `params`, runs it on the // engine's probe pool (outside the segment budget, ADR 0011 §5), and converts the // result back to proto terms — including the suggestedCategoryId/-SaveDir guess (a @@ -153,6 +155,13 @@ public: std::function)> done) override; + void refresh_url( + const std::string& wire_id, const std::string& url, + const std::optional& headers, + const std::optional>& cookies, + std::function)> + done) override; + // Diagnostics / tests. std::optional wire_id_of(vdm::TaskId id) const; std::optional engine_id_of(const std::string& wire_id) const; diff --git a/daemon/src/store/migrations/0004_rules_name.sql b/daemon/src/store/migrations/0004_rules_name.sql new file mode 100644 index 0000000..759e253 --- /dev/null +++ b/daemon/src/store/migrations/0004_rules_name.sql @@ -0,0 +1,10 @@ +-- Migration 0004 — rules gets a name column. +-- +-- 0001's rules table had no column for Rule.name (Rule.schema.json's own optional, +-- maxLength-64 label field) — store/rules.cpp discovered this the hard way building +-- rules.list/rules.upsert: every rules.list call failed outright ("no such column: +-- name") because the SELECT it needs to project onto proto::Rule names a column that was +-- never there. A plain ALTER TABLE ADD COLUMN suffices here (no CHECK constraint to +-- rebuild around, unlike 0002/0003). + +ALTER TABLE rules ADD COLUMN name TEXT; diff --git a/daemon/src/store/queues.cpp b/daemon/src/store/queues.cpp index 8db8771..ffe8769 100644 --- a/daemon/src/store/queues.cpp +++ b/daemon/src/store/queues.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -85,6 +86,56 @@ DbResult Queues::set_state(std::string_view queue_id, std::string_view sta return sqlite3_changes(db_.raw()) > 0; } +DbResult Queues::set_schedule(std::string_view queue_id, + const std::optional& schedule) { + auto st = db_.prepare("UPDATE queues SET schedule = ?2 WHERE queue_id = ?1"); + if (!st) return std::unexpected(st.error()); + if (auto b = st->bind(1, queue_id); !b) return std::unexpected(b.error()); + if (auto r = schedule ? st->bind(2, std::string_view(nlohmann::json(*schedule).dump())) + : st->bind_null(2); + !r) + return std::unexpected(r.error()); + if (auto r = st->step(); !r) return std::unexpected(r.error()); + return sqlite3_changes(db_.raw()) > 0; +} + +DbResult Queues::reorder(std::string_view queue_id, const std::vector& task_ids) { + std::vector current; + { + auto st = db_.prepare( + "SELECT task_id FROM tasks WHERE queue_id = ?1 ORDER BY queue_position"); + if (!st) return std::unexpected(st.error()); + if (auto b = st->bind(1, queue_id); !b) return std::unexpected(b.error()); + for (;;) { + auto row = st->step(); + if (!row) return std::unexpected(row.error()); + if (!*row) break; + current.push_back(st->column_text(0)); + } + } + + // Exact permutation: same size, same members, order aside. + std::vector a = current, b = task_ids; + std::sort(a.begin(), a.end()); + std::sort(b.begin(), b.end()); + if (a != b) return false; + + auto txn = db_.transaction([&]() -> DbResult { + for (std::size_t i = 0; i < task_ids.size(); ++i) { + auto st = db_.prepare("UPDATE tasks SET queue_position = ?2 WHERE task_id = ?1"); + if (!st) return std::unexpected(st.error()); + if (auto bd = st->bind(1, std::string_view(task_ids[i])); !bd) + return std::unexpected(bd.error()); + if (auto bd = st->bind(2, static_cast(i)); !bd) + return std::unexpected(bd.error()); + if (auto r = st->step(); !r) return std::unexpected(r.error()); + } + return {}; + }); + if (!txn) return std::unexpected(txn.error()); + return true; +} + DbResult Queues::upsert(proto::Queue queue) { if (queue.queueId.empty()) { std::random_device rd; diff --git a/daemon/src/store/queues.hpp b/daemon/src/store/queues.hpp index a284aea..6b36599 100644 --- a/daemon/src/store/queues.hpp +++ b/daemon/src/store/queues.hpp @@ -27,6 +27,18 @@ public: // id doesn't exist. DbResult set_state(std::string_view queue_id, std::string_view state); + // schedule.set: nullopt clears it (NULL = manual control, per the schema's own + // words). false if the queue doesn't exist. + DbResult set_schedule(std::string_view queue_id, + const std::optional& schedule); + + // queue.reorder: `task_ids` must be an exact permutation of the queue's current + // membership (the schema's own words — "anything else is -32602 rather than a + // partial reorder, so a stale drag from an out-of-date view cannot quietly reshuffle + // the queue"). false (no write at all) if it isn't; true and queue_position rewritten + // to match `task_ids`'s order if it is. + DbResult reorder(std::string_view queue_id, const std::vector& task_ids); + // "Omit queueId to create" (queue.upsert's own words) — an empty id generates one. // taskIds is ignored (membership changes only through download.update / queue.reorder, // per the schema's own note); a create defaults to 'stopped', a replace keeps the diff --git a/daemon/src/store/tasks.cpp b/daemon/src/store/tasks.cpp index 50b49a1..480c91a 100644 --- a/daemon/src/store/tasks.cpp +++ b/daemon/src/store/tasks.cpp @@ -318,6 +318,88 @@ DbResult Tasks::count() { return (*row) ? st->column_int(0) : 0; } +DbResult Tasks::apply_update(std::string_view task_id, const UpdatePatch& patch) { + // One UPDATE per present field: simplest thing that's obviously correct for a + // single-row edit with ~9 independent optional fields, and it means a field the + // caller didn't touch is never rewritten with its own unchanged value (matters for + // no-op-detection callers, though download.update doesn't currently need that). + bool touched_any = false; + auto run = [&](const char* sql, auto&& binder) -> DbResult { + auto st = db_.prepare(sql); + if (!st) return std::unexpected(st.error()); + if (auto r = st->bind(1, task_id); !r) return std::unexpected(r.error()); + if (auto r = binder(*st); !r) return std::unexpected(r.error()); + if (auto r = st->step(); !r) return std::unexpected(r.error()); + touched_any = true; + return {}; + }; + + if (patch.save_dir && patch.filename) { + if (auto r = run("UPDATE tasks SET save_dir = ?2, filename = ?3 WHERE task_id = ?1", + [&](Stmt& s) { + if (auto b = s.bind(2, std::string_view(*patch.save_dir)); !b) return b; + return s.bind(3, std::string_view(*patch.filename)); + }); + !r) + return std::unexpected(r.error()); + } + if (patch.category_id) { + if (auto r = run("UPDATE tasks SET category_id = ?2 WHERE task_id = ?1", + [&](Stmt& s) { return s.bind(2, std::string_view(*patch.category_id)); }); + !r) + return std::unexpected(r.error()); + } + if (patch.queue_id) { + if (auto r = run("UPDATE tasks SET queue_id = ?2, queue_position = ?3 WHERE task_id = ?1", + [&](Stmt& s) { + if (auto b = s.bind(2, std::string_view(*patch.queue_id)); !b) return b; + return patch.queue_position ? s.bind(3, *patch.queue_position) + : s.bind_null(3); + }); + !r) + return std::unexpected(r.error()); + } + if (patch.description) { + if (auto r = run("UPDATE tasks SET description = ?2 WHERE task_id = ?1", + [&](Stmt& s) { return s.bind(2, std::string_view(*patch.description)); }); + !r) + return std::unexpected(r.error()); + } + if (patch.req_segments) { + if (auto r = run("UPDATE tasks SET req_segments = ?2 WHERE task_id = ?1", + [&](Stmt& s) { return s.bind(2, *patch.req_segments); }); + !r) + return std::unexpected(r.error()); + } + if (patch.req_buffer_bytes) { + if (auto r = run("UPDATE tasks SET req_buffer_bytes = ?2 WHERE task_id = ?1", + [&](Stmt& s) { return s.bind(2, *patch.req_buffer_bytes); }); + !r) + return std::unexpected(r.error()); + } + if (patch.checksum_algo && patch.checksum_value) { + if (auto r = run( + "UPDATE tasks SET checksum_algo = ?2, checksum_value = ?3 WHERE task_id = ?1", + [&](Stmt& s) { + if (auto b = s.bind(2, std::string_view(*patch.checksum_algo)); !b) return b; + return s.bind(3, std::string_view(*patch.checksum_value)); + }); + !r) + return std::unexpected(r.error()); + } + + return touched_any; +} + +DbResult Tasks::set_url(std::string_view task_id, std::string_view url) { + auto st = db_.prepare("UPDATE tasks SET url = ?2 WHERE task_id = ?1"); + if (!st) return std::unexpected(st.error()); + if (auto r = st->bind(1, task_id); !r) return std::unexpected(r.error()); + if (auto r = st->bind(2, url); !r) return std::unexpected(r.error()); + if (auto r = st->step(); !r) return std::unexpected(r.error()); + return sqlite3_changes(db_.raw()) > 0; +} + DbResult Tasks::has_active_duplicate(std::string_view url) { auto st = db_.prepare( "SELECT 1 FROM tasks WHERE url = ?1 " diff --git a/daemon/src/store/tasks.hpp b/daemon/src/store/tasks.hpp index d0c1ef7..5c71696 100644 --- a/daemon/src/store/tasks.hpp +++ b/daemon/src/store/tasks.hpp @@ -85,6 +85,34 @@ public: // is almost always a mistake, e.g. two tabs triggering the same link). DbResult has_active_duplicate(std::string_view url); + // download.update's patch, already resolved by the caller (new save_dir/filename + // canonicalized and root-checked, any file already moved on disk — this only writes + // the row). Every field is applied when present; queue_position is written alongside + // queue_id (nullopt leaves the existing position alone — the caller decides what + // "moved into a queue" should set it to). Note: the generated parser collapses "field + // absent" and "field explicitly null" to the same nullopt (DownloadUpdateParamsPatch + // has no way to tell them apart on the wire as generated), so this — like the RPC + // layer above it — can only ever set category_id/queue_id/description/checksum, never + // clear them back to NULL through this call. + struct UpdatePatch { + std::optional save_dir; + std::optional filename; + std::optional category_id; + std::optional queue_id; + std::optional queue_position; + std::optional description; + std::optional req_segments; + std::optional req_buffer_bytes; + std::optional checksum_algo; + std::optional checksum_value; + }; + DbResult apply_update(std::string_view task_id, const UpdatePatch& patch); + + // download.refreshUrl: point the task at a freshly-issued URL. Separate from + // apply_update/set_probe_result since neither owns the base `url` column — refreshUrl + // is the one caller that changes it after creation. + DbResult set_url(std::string_view task_id, std::string_view url); + // Byte-counter update from an engine progress tick — cheaper than a full row rewrite, // and keeps download.list / download.get current between state transitions. DbResult update_progress(std::string_view task_id, std::int64_t downloaded_bytes, diff --git a/daemon/tests/CMakeLists.txt b/daemon/tests/CMakeLists.txt index 561d59e..17ece28 100644 --- a/daemon/tests/CMakeLists.txt +++ b/daemon/tests/CMakeLists.txt @@ -26,3 +26,4 @@ veloxd_test(store_categories_queues LIBS veloxd_store) veloxd_test(single_instance LIBS veloxd_rpc) veloxd_test(dispatcher_settings LIBS veloxd_rpc veloxd_store) veloxd_test(capture_offer LIBS veloxd_rpc veloxd_store) +veloxd_test(dispatcher_misc LIBS veloxd_rpc veloxd_store) diff --git a/daemon/tests/dispatcher_misc_test.cpp b/daemon/tests/dispatcher_misc_test.cpp new file mode 100644 index 0000000..afbb7a3 --- /dev/null +++ b/daemon/tests/dispatcher_misc_test.cpp @@ -0,0 +1,244 @@ +// rules.list/upsert, queue.reorder, schedule.get/set, limiter.get/set, download.update: +// the rest of D3, called directly against an in-memory store (no socket). + +#include + +#include "check.hpp" +#include "rpc/dispatcher.hpp" +#include "rpc/event_hub.hpp" +#include "store/migrations.hpp" +#include "store/queues.hpp" +#include "store/sqlite.hpp" +#include "store/tasks.hpp" +#include "velox_proto.hpp" + +using namespace velox::daemon; +namespace proto = velox::proto; + +namespace { + +store::TaskRow task(std::string id, std::optional queue = std::nullopt, + std::int64_t pos = 0) { + store::TaskRow r; + r.task_id = std::move(id); + r.url = "https://cdn.example/" + r.task_id; + r.save_dir = "/tmp"; + r.filename = r.task_id + ".bin"; + r.state = "queued"; + r.created_at = "2026-09-12T00:00:00Z"; + r.queue_id = std::move(queue); + if (r.queue_id) r.queue_position = pos; + return r; +} + +} // namespace + +void run() { + auto db = store::Db::open(":memory:"); + CHECK(db.has_value()); + if (!db) return; + CHECK(store::migrate_to_head(*db).has_value()); + + rpc::EventHub hub; + rpc::VeloxDispatcher dispatcher(*db, hub); + + // --- rules.list starts empty; rules.upsert creates, replaces, and removes -------- + { + auto listed = dispatcher.on_rules_list({}); + CHECK(listed.has_value()); + if (listed) CHECK(listed->items.empty()); + + proto::RulesUpsertParams up; + proto::Rule r; + r.name = "ISOs to a queue"; + r.enabled = true; + r.priority = 5; + r.match.extensions = std::vector{"iso"}; + r.action.categoryId = "programs"; + up.upsert = {r}; + auto created = dispatcher.on_rules_upsert(up); + CHECK(created.has_value()); + if (created) { + CHECK_EQ(created->items.size(), std::size_t{1}); + CHECK(!created->items[0].ruleId.empty()); + } + + // Replace: same ruleId, new priority. + if (!created || created->items.empty()) return; + std::string rule_id = created->items[0].ruleId; + proto::RulesUpsertParams replace; + proto::Rule r2 = created->items[0]; + r2.priority = 1; + replace.upsert = {r2}; + auto replaced = dispatcher.on_rules_upsert(replace); + CHECK(replaced.has_value()); + if (replaced) { + CHECK_EQ(replaced->items.size(), std::size_t{1}); + CHECK_EQ(replaced->items[0].priority, std::int64_t{1}); + } + + // Remove. + proto::RulesUpsertParams rm; + rm.remove = {rule_id}; + auto removed = dispatcher.on_rules_upsert(rm); + CHECK(removed.has_value()); + if (removed) CHECK(removed->items.empty()); + } + + // --- queue.reorder: a real permutation applies; a non-permutation is -32602 ------ + { + store::Tasks tasks(*db); + CHECK(tasks.insert(task("q0", "main", 0)).has_value()); + CHECK(tasks.insert(task("q1", "main", 1)).has_value()); + CHECK(tasks.insert(task("q2", "main", 2)).has_value()); + + proto::QueueReorderParams p; + p.queueId = "main"; + p.taskIds = {"q2", "q0", "q1"}; + auto r = dispatcher.on_queue_reorder(p); + CHECK(r.has_value()); + if (r) { + CHECK(r->queue.taskIds.has_value()); + if (r->queue.taskIds) { + const auto& ids = *r->queue.taskIds; + CHECK_EQ(ids.size(), std::size_t{3}); + if (ids.size() == 3) { + CHECK_EQ(ids[0], std::string("q2")); + CHECK_EQ(ids[1], std::string("q0")); + CHECK_EQ(ids[2], std::string("q1")); + } + } + } + + proto::QueueReorderParams bad; + bad.queueId = "main"; + bad.taskIds = {"q0", "q1"}; // missing q2: not a permutation + auto bad_r = dispatcher.on_queue_reorder(bad); + CHECK(!bad_r.has_value()); + if (!bad_r) CHECK(bad_r.error().code == proto::ErrorCode::InvalidParams); + + proto::QueueReorderParams unknown_queue; + unknown_queue.queueId = "does-not-exist"; + unknown_queue.taskIds = {}; + auto unknown_r = dispatcher.on_queue_reorder(unknown_queue); + CHECK(!unknown_r.has_value()); + + tasks.remove("q0"); + tasks.remove("q1"); + tasks.remove("q2"); + } + + // --- schedule.get / schedule.set --------------------------------------------------- + { + proto::ScheduleGetParams g; + g.queueId = std::string("main"); + auto before = dispatcher.on_schedule_get(g); + CHECK(before.has_value()); + if (before) { + CHECK_EQ(before->items.size(), std::size_t{1}); + if (!before->items.empty()) CHECK(!before->items[0].schedule.has_value()); + } + + proto::ScheduleSetParams set; + set.queueId = "main"; + proto::Schedule sched; + sched.enabled = true; + sched.mode = proto::ScheduleMode::Periodic; + sched.startTime = std::string("22:00"); + sched.stopTime = std::string("06:00"); + set.schedule = sched; + auto set_r = dispatcher.on_schedule_set(set); + CHECK(set_r.has_value()); + if (set_r) { + CHECK(set_r->schedule.has_value()); + CHECK(!set_r->nextRunAt.has_value()); // documented gap, not computed + } + + auto after = dispatcher.on_schedule_get(g); + CHECK(after.has_value()); + if (after && !after->items.empty()) + CHECK(after->items[0].schedule.has_value()); + + // Clearing (the wire-level "explicit null" is unreachable through the generated + // parser's collapsing of absent/null — see UpdatePatch's own comment; this test + // only proves set-a-value works, not clear). + + proto::ScheduleGetParams all; + auto all_r = dispatcher.on_schedule_get(all); + CHECK(all_r.has_value()); + if (all_r) CHECK(!all_r->items.empty()); + + proto::ScheduleGetParams missing; + missing.queueId = std::string("does-not-exist"); + auto missing_r = dispatcher.on_schedule_get(missing); + CHECK(missing_r.has_value()); + if (missing_r) CHECK(missing_r->items.empty()); + } + + // --- limiter.get / limiter.set ----------------------------------------------------- + { + auto before = dispatcher.on_limiter_get({}); + CHECK(before.has_value()); + if (before) { + CHECK(!before->enabled); + CHECK_EQ(before->globalBps, std::int64_t{0}); + } + + proto::Limiter set; + set.enabled = true; + set.globalBps = 2'000'000; + auto set_r = dispatcher.on_limiter_set(set); + CHECK(set_r.has_value()); + if (set_r) { + CHECK(set_r->enabled); + CHECK_EQ(set_r->globalBps, std::int64_t{2'000'000}); + } + + auto after = dispatcher.on_limiter_get({}); + CHECK(after.has_value()); + if (after) { + CHECK(after->enabled); + CHECK_EQ(after->globalBps, std::int64_t{2'000'000}); + } + } + + // --- download.update: metadata fields, range validation, not-found --------------- + { + store::Tasks tasks(*db); + CHECK(tasks.insert(task("u0")).has_value()); + + proto::DownloadUpdateParams p; + p.taskId = "u0"; + p.patch.description = std::string("a note"); + p.patch.segments = 4; + auto r = dispatcher.on_download_update(p); + CHECK(r.has_value()); + if (r) { + CHECK_EQ(r->taskId, std::string("u0")); + } + auto row = tasks.get("u0"); + CHECK(row.has_value() && row->has_value()); + if (row && *row) { + CHECK_EQ((*row)->description.value_or(""), std::string("a note")); + CHECK_EQ((*row)->req_segments.value_or(-1), std::int64_t{4}); + } + + proto::DownloadUpdateParams bad_range; + bad_range.taskId = "u0"; + bad_range.patch.segments = 999; + auto bad_r = dispatcher.on_download_update(bad_range); + CHECK(!bad_r.has_value()); + if (!bad_r) CHECK(bad_r.error().code == proto::ErrorCode::InvalidParams); + + proto::DownloadUpdateParams missing; + missing.taskId = "does-not-exist"; + missing.patch.description = std::string("x"); + auto missing_r = dispatcher.on_download_update(missing); + CHECK(!missing_r.has_value()); + if (!missing_r) CHECK(missing_r.error().code == proto::ErrorCode::TaskNotFound); + + tasks.remove("u0"); + } +} + +TEST_MAIN() diff --git a/daemon/tests/sched_scheduler_test.cpp b/daemon/tests/sched_scheduler_test.cpp index 8f90977..b515748 100644 --- a/daemon/tests/sched_scheduler_test.cpp +++ b/daemon/tests/sched_scheduler_test.cpp @@ -406,6 +406,75 @@ void run() { if (got2 && !*got2) CHECK(got2->error().code == velox::proto::ErrorCode::ProbeFailed); } + + // --- refresh_url: content unchanged, content changed, and a live-handle swap ----- + { + FakeEnginePort engine; + Scheduler sched(*db, engine, + Governor(GovernorConfig{.max_concurrent_downloads = 10, + .max_active_segments = 32})); + store::TaskRow r; + r.task_id = "ru0"; + r.url = "https://cdn.example/old-signed-url"; + r.save_dir = "/tmp"; + r.filename = "ru0.bin"; + r.state = "queued"; + r.created_at = "2026-09-12T00:00:00Z"; + r.size_bytes = 1000; + r.etag = "\"same\""; + CHECK(tasks.insert(r).has_value()); + CHECK(sched.tick().has_value()); + CHECK_EQ(engine.starts.size(), 1u); + const auto live_id = engine.starts[0].id; + + // Same size and etag: not changed. + vdm::net::ProbeResult pr; + pr.effective_url = "https://cdn.example/new-signed-url"; + pr.total_size = 1000; + pr.etag = "\"same\""; + pr.resumable = true; + engine.auto_probe_result = vdm::Result{pr}; + + std::optional> got; + sched.refresh_url("ru0", "https://cdn.example/new-signed-url", std::nullopt, std::nullopt, + [&](auto r2) { got = std::move(r2); }); + CHECK(got.has_value()); + CHECK(got->has_value()); + if (got && *got) { + CHECK((*got)->ok); + CHECK(!(*got)->contentChanged); + CHECK((*got)->resumable); + } + // The live handle got its URL swapped, not a fresh start(). + CHECK_EQ(engine.starts.size(), 1u); + CHECK_EQ(engine.refreshed_urls.size(), std::size_t{1}); + if (!engine.refreshed_urls.empty()) { + CHECK_EQ(engine.refreshed_urls[0].id.value, live_id.value); + CHECK_EQ(engine.refreshed_urls[0].url, std::string("https://cdn.example/new-signed-url")); + } + auto row = tasks.get("ru0").value().value(); + CHECK_EQ(row.url, std::string("https://cdn.example/new-signed-url")); + + // A different size: content changed. + pr.total_size = 2000; + engine.auto_probe_result = vdm::Result{pr}; + std::optional> got2; + sched.refresh_url("ru0", "https://cdn.example/another-url", std::nullopt, std::nullopt, + [&](auto r2) { got2 = std::move(r2); }); + CHECK(got2.has_value() && got2->has_value()); + if (got2 && *got2) CHECK((*got2)->contentChanged); + + // Unknown task: TaskNotFound, no probe issued. + const auto probes_before = engine.probe_requests.size(); + std::optional> got3; + sched.refresh_url("does-not-exist", "https://x/y", std::nullopt, std::nullopt, + [&](auto r2) { got3 = std::move(r2); }); + CHECK(got3.has_value() && !got3->has_value()); + if (got3 && !*got3) CHECK(got3->error().code == velox::proto::ErrorCode::TaskNotFound); + CHECK_EQ(engine.probe_requests.size(), probes_before); + + tasks.remove("ru0"); + } } TEST_MAIN()