mirror of
https://github.com/penpot/penpot.git
synced 2026-11-07 02:20:36 +00:00
* 🎉 Move backend jobs to the unified job table substrate Replace the generic 'task' table with a unified 'job' table as the durable substrate for all backend jobs, and migrate every producer and consumer to it. The legacy 'task' table stops receiving writes but stays dormant (still cleaned by tasks-gc); upload-session chunking and the other post-branch upstream changes are preserved. New 0154 migration creates the 'job' table: dispatch and lifecycle columns (name, queue, priority, scheduled_at, retries, status, timestamps, props) plus optional user-facing ledger columns (profile_id, target, progress, error, result, resource_id, expires_at). No modified_at trigger: the application updates it on every write for lease and orphan detection. Partial indexes cover the dispatcher claim query, orphan scanning, and per-profile listing. New app.jobs public API: submit!/invoke!, heartbeats and progress reporting, a precompiled job-def registry (name, schema, handler, decoder, validator), an ephemeral request/response mode, and a management API for external workers. The dispatcher claims new/retry rows (FOR UPDATE SKIP LOCKED) with a plain JSON redis payload and marks lease-expired running rows as orphaned; the runner executes per-queue with the same retry conventions. New jobs GC (daily): deletes expired rows and retains internal terminal rows per :jobs-retention, touching referenced storage objects so storage-gc-touched reclaims them. Job resources live in a dedicated job-resource bucket with a reclaim branch in storage-gc-touched. All production call-sites (sendmail, delete-object, demo-purge, file-gc, offload, webhooks, quotes, nitrate bulk delete, snapshots, telemetry) and the ten cron tasks now submit through app.jobs; cron is a pure scheduler and the legacy worker registry, task-table inserts, and per-namespace handlers are removed. Includes migrations, dispatcher, runner, jobs, request/response, jobs GC, management API, and cron test suites. AI-assisted-by: glm-5.3-flash AI-assisted-by: muse-spark-1.3-contributor AI-assisted-by: kimi-k3 AI-assisted-by: mimo-v2.5 * ✨ Address review comments on jobs substrate Resolves the four open review threads on the jobs PR. Orphaned jobs are now marked aborted, a system-side terminal state with no retry: the dispatcher sweep stores the end event with outcome aborted in a single bulk insert in the same transaction, records the terminal outcome next to the orphaned counter, and reports an error log; orphans keep alerting through the log and the counter, with no user notification. The aborted state is terminal everywhere: the status CHECK, the sweep, the lifecycle writers, the jobs GC retention, the backlog gauges and the user status mapping, with tests covering each transition. The claim comment, the 2.20 doc version and the legacy task table notes are corrected, and objects-gc heartbeats per chunk. AI-assisted-by: muse-spark-1.3-contributor
223 lines
7.7 KiB
Clojure
223 lines
7.7 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
|
|
|
(ns backend-tests.rpc-file-snapshot-test
|
|
(:require
|
|
[app.common.features :as cfeat]
|
|
[app.common.pprint :as pp]
|
|
[app.common.thumbnails :as thc]
|
|
[app.common.time :as ct]
|
|
[app.common.types.shape :as cts]
|
|
[app.common.uuid :as uuid]
|
|
[app.config :as cf]
|
|
[app.db :as db]
|
|
[app.db.sql :as sql]
|
|
[app.http :as http]
|
|
[app.rpc :as-alias rpc]
|
|
[app.storage :as sto]
|
|
[backend-tests.helpers :as th]
|
|
[clojure.test :as t]
|
|
[cuerdas.core :as str]))
|
|
|
|
(t/use-fixtures :once th/state-init)
|
|
(t/use-fixtures :each th/database-reset)
|
|
|
|
(defn- update-file!
|
|
[& {:keys [profile-id file-id changes revn] :or {revn 0}}]
|
|
(let [params {::th/type :update-file
|
|
::rpc/profile-id profile-id
|
|
:id file-id
|
|
:session-id (uuid/random)
|
|
:revn revn
|
|
:vern 0
|
|
:features cfeat/supported-features
|
|
:changes changes}
|
|
out (th/command! params)]
|
|
;; (th/print-result! out)
|
|
(t/is (nil? (:error out)))
|
|
(:result out)))
|
|
|
|
(t/deftest snapshots-crud
|
|
(let [profile (th/create-profile* 1 {:is-active true})
|
|
team-id (:default-team-id profile)
|
|
proj-id (:default-project-id profile)
|
|
|
|
file (th/create-file* 1 {:profile-id (:id profile)
|
|
:project-id proj-id
|
|
:is-shared false})
|
|
snapshot-id (volatile! nil)]
|
|
|
|
(t/testing "create snapshot"
|
|
(let [params {::th/type :create-file-snapshot
|
|
::rpc/profile-id (:id profile)
|
|
:file-id (:id file)
|
|
:label "label1"}
|
|
out (th/command! params)]
|
|
;; (th/print-result! out)
|
|
|
|
(t/is (nil? (:error out)))
|
|
(let [result (:result out)]
|
|
(t/is (= "label1" (:label result)))
|
|
(t/is (uuid? (:id result)))
|
|
(vswap! snapshot-id (constantly (:id result))))))
|
|
|
|
(t/testing "list snapshots"
|
|
(let [params {::th/type :get-file-snapshots
|
|
::rpc/profile-id (:id profile)
|
|
:file-id (:id file)}
|
|
out (th/command! params)]
|
|
;; (th/print-result! out)
|
|
(t/is (nil? (:error out)))
|
|
(let [[row :as result] (:result out)]
|
|
(t/is (= 1 (count result)))
|
|
(t/is (= "label1" (:label row)))
|
|
(t/is (uuid? (:id row)))
|
|
(t/is (= @snapshot-id (:id row)))
|
|
(t/is (= 0 (:revn row)))
|
|
(t/is (= (:id profile) (:profile-id row))))))
|
|
|
|
(t/testing "restore snapshot"
|
|
(let [params {::th/type :restore-file-snapshot
|
|
::rpc/profile-id (:id profile)
|
|
:file-id (:id file)
|
|
:id @snapshot-id}
|
|
out (th/command! params)]
|
|
|
|
;; (th/print-result! out)
|
|
(t/is (nil? (:error out)))
|
|
(t/is (nil? (:result out))))
|
|
|
|
(let [[row1 row2 :as rows]
|
|
(th/db-query :file-change
|
|
{:file-id (:id file)}
|
|
{:order-by [:created-at]})]
|
|
|
|
(t/is (= 2 (count rows)))
|
|
(t/is (= "user" (:created-by row1)))
|
|
(t/is (= "system" (:created-by row2)))))
|
|
|
|
(t/testing "delete snapshot"
|
|
(let [[row1 row2 :as rows]
|
|
(th/db-query :file-change
|
|
{:file-id (:id file)}
|
|
{:order-by [:created-at]})]
|
|
|
|
(t/testing "delete user created snapshot"
|
|
(let [params {::th/type :delete-file-snapshot
|
|
::rpc/profile-id (:id profile)
|
|
:file-id (:id file)
|
|
:id (:id row1)}
|
|
out (th/command! params)]
|
|
|
|
;; (th/print-result! out)
|
|
(t/is (nil? (:error out)))
|
|
(t/is (true? (:result out)))))
|
|
|
|
(t/testing "delete system created snapshot"
|
|
(let [params {::th/type :delete-file-snapshot
|
|
::rpc/profile-id (:id profile)
|
|
:file-id (:id file)
|
|
:id (:id row2)}
|
|
out (th/command! params)]
|
|
|
|
;; (th/print-result! out)
|
|
(let [error (:error out)
|
|
data (ex-data error)]
|
|
(t/is (th/ex-info? error))
|
|
(t/is (= (:type data) :validation))
|
|
(t/is (= (:code data) :system-snapshots-cant-be-deleted)))))
|
|
|
|
;; this will run pending task triggered by deleting user snapshot
|
|
(th/run-pending-jobs)
|
|
|
|
(binding [ct/*clock* (ct/fixed-clock (ct/in-future {:days 8}))]
|
|
(let [res (th/run-task! :objects-gc {})]
|
|
;; delete 2 snapshots and 2 file data entries
|
|
(t/is (= 4 (:processed res)))))))))
|
|
|
|
(t/deftest snapshots-locking
|
|
(let [profile-1 (th/create-profile* 1 {:is-active true})
|
|
profile-2 (th/create-profile* 2 {:is-active true})
|
|
|
|
team
|
|
(th/create-team* 1 {:profile-id (:id profile-1)})
|
|
|
|
project
|
|
(th/create-project* 1 {:profile-id (:id profile-1)
|
|
:team-id (:id team)})
|
|
|
|
file
|
|
(th/create-file* 1 {:profile-id (:id profile-1)
|
|
:project-id (:id project)
|
|
:is-shared false})
|
|
|
|
snapshot
|
|
(let [params {::th/type :create-file-snapshot
|
|
::rpc/profile-id (:id profile-1)
|
|
:file-id (:id file)
|
|
:label "label1"}
|
|
out (th/command! params)]
|
|
;; (th/print-result! out)
|
|
|
|
(t/is (nil? (:error out)))
|
|
(:result out))]
|
|
|
|
;; Add the secont profile to the team
|
|
(th/create-team-role* {:team-id (:id team)
|
|
:profile-id (:id profile-2)
|
|
:role :admin})
|
|
|
|
(t/testing "lock snapshot"
|
|
(let [params {::th/type :lock-file-snapshot
|
|
::rpc/profile-id (:id profile-1)
|
|
:file-id (:id file)
|
|
:id (:id snapshot)}
|
|
out (th/command! params)]
|
|
;; (th/print-result! out)
|
|
(t/is (nil? (:error out)))
|
|
(t/is (true? (:result out)))
|
|
|
|
(let [snapshot (th/db-get :file-change {:id (:id snapshot)} {::db/remove-deleted false})]
|
|
(t/is (= (:id profile-1) (:locked-by snapshot))))))
|
|
|
|
(t/testing "delete locked snapshot"
|
|
(let [params {::th/type :delete-file-snapshot
|
|
::rpc/profile-id (:id profile-2)
|
|
:file-id (:id file)
|
|
:id (:id snapshot)}
|
|
out (th/command! params)]
|
|
|
|
;; (th/print-result! out)
|
|
(let [error (:error out)
|
|
data (ex-data error)]
|
|
(t/is (th/ex-info? error))
|
|
(t/is (= (:type data) :validation))
|
|
(t/is (= (:code data) :snapshot-is-locked)))))
|
|
|
|
(t/testing "unlock snapshot"
|
|
(let [params {::th/type :unlock-file-snapshot
|
|
::rpc/profile-id (:id profile-1)
|
|
:file-id (:id file)
|
|
:id (:id snapshot)}
|
|
out (th/command! params)]
|
|
;; (th/print-result! out)
|
|
(t/is (nil? (:error out)))
|
|
(t/is (true? (:result out)))
|
|
|
|
(let [snapshot (th/db-get :file-change {:id (:id snapshot)})]
|
|
(t/is (= nil (:locked-by snapshot))))))
|
|
|
|
(t/testing "delete locked snapshot"
|
|
(let [params {::th/type :delete-file-snapshot
|
|
::rpc/profile-id (:id profile-2)
|
|
:file-id (:id file)
|
|
:id (:id snapshot)}
|
|
out (th/command! params)]
|
|
|
|
;; (th/print-result! out)
|
|
(t/is (nil? (:error out)))
|
|
(t/is (true? (:result out)))))))
|