@whyour/qinglong 2.21.0 → 2.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +13 -0
- package/.env.example +7 -0
- package/AGENTS.md +2 -2
- package/README-en.md +24 -0
- package/README.md +24 -0
- package/back/protos/api.proto +9 -1
- package/back/protos/cron.proto +8 -1
- package/deploy/kubernetes/README.md +81 -0
- package/deploy/kubernetes/base/kustomization.yaml +5 -0
- package/deploy/kubernetes/base/qinglong.yaml +95 -0
- package/deploy/kubernetes/overlays/example/kustomization.yaml +10 -0
- package/docker/Dockerfile +31 -12
- package/docker/Dockerfile.310 +31 -12
- package/docker/Dockerfile.debian +22 -14
- package/docker/Dockerfile.debian310 +24 -14
- package/docker/build-manifest.cjs +28 -0
- package/docker/verify-build.cjs +49 -0
- package/docs/development/node-cron-migration.md +49 -0
- package/docs/development/task-startup.md +78 -0
- package/docs/releases/2.22.0-validation.md +49 -0
- package/docs/releases/2.22.0.md +62 -0
- package/docs/releases/2.23.0.md +33 -0
- package/docs/script-history.md +53 -0
- package/ecosystem.config.js +10 -1
- package/package.json +19 -13
- package/sample/config.sample.sh +17 -2
- package/sample/notify.js +118 -0
- package/sample/notify.py +93 -0
- package/scripts/benchmark-execution.cjs +96 -0
- package/scripts/benchmark-script-history.cjs +39 -0
- package/scripts/publish-static.sh +34 -0
- package/scripts/sync-static.sh +20 -0
- package/scripts/wait-for-mirror.sh +214 -0
- package/scripts/write-build-info.cjs +27 -0
- package/shell/api.sh +96 -83
- package/shell/check.sh +59 -19
- package/shell/lang/en.sh +6 -0
- package/shell/lang/zh.sh +6 -0
- package/shell/node_path_cache.sh +136 -0
- package/shell/otask.sh +17 -10
- package/shell/preload/sitecustomize.js +11 -3
- package/shell/preload/sitecustomize.py +4 -2
- package/shell/share.sh +27 -10
- package/shell/start.sh +6 -3
- package/shell/task-timeout.sh +63 -0
- package/shell/task.sh +21 -9
- package/shell/update.sh +104 -98
- package/shell/upgrade.cjs +8 -0
- package/static/build/api/clientIp.js +42 -0
- package/static/build/api/config.js +9 -21
- package/static/build/api/cron.js +39 -3
- package/static/build/api/dashboard.js +50 -6
- package/static/build/api/env.js +17 -16
- package/static/build/api/health.js +2 -2
- package/static/build/api/index.js +4 -0
- package/static/build/api/log.js +39 -19
- package/static/build/api/open.js +2 -1
- package/static/build/api/retention.js +63 -0
- package/static/build/api/script.js +172 -25
- package/static/build/api/subscription.js +19 -8
- package/static/build/api/system.js +66 -12
- package/static/build/api/user.js +75 -15
- package/static/build/app.js +63 -54
- package/static/build/bootstrap/database.js +51 -0
- package/static/build/config/const.js +2 -0
- package/static/build/config/grpcCerts.js +93 -44
- package/static/build/config/index.js +0 -4
- package/static/build/config/share.js +5 -4
- package/static/build/config/subscription.js +19 -11
- package/static/build/config/util.js +142 -45
- package/static/build/data/cron.js +1 -0
- package/static/build/data/notify.js +18 -1
- package/static/build/data/sock.js +1 -0
- package/static/build/loaders/app.js +8 -6
- package/static/build/loaders/db.js +5 -35
- package/static/build/loaders/deps.js +30 -22
- package/static/build/loaders/express.js +46 -32
- package/static/build/loaders/initData.js +26 -33
- package/static/build/loaders/initFile.js +1 -1
- package/static/build/loaders/initTask.js +14 -3
- package/static/build/loaders/server.js +1 -1
- package/static/build/loaders/sock.js +72 -6
- package/static/build/middlewares/monitoring.js +12 -33
- package/static/build/middlewares/protectedPathCase.js +17 -0
- package/static/build/middlewares/uniqueConstraintError.js +15 -0
- package/static/build/protos/api.js +78 -2
- package/static/build/protos/cron.js +83 -5
- package/static/build/protos/health.js +1 -1
- package/static/build/schedule/addCron.js +67 -19
- package/static/build/schedule/api.js +5 -2
- package/static/build/schedule/client.js +56 -6
- package/static/build/schedule/delCron.js +11 -1
- package/static/build/schedule/health.js +72 -7
- package/static/build/schedule/setConcurrency.js +27 -0
- package/static/build/services/config.js +5 -43
- package/static/build/services/cron.js +389 -174
- package/static/build/services/dependence.js +66 -18
- package/static/build/services/env.js +1 -1
- package/static/build/services/grpc.js +4 -3
- package/static/build/services/health.js +4 -7
- package/static/build/services/http.js +9 -3
- package/static/build/services/log.js +2 -3
- package/static/build/services/metrics.js +46 -14
- package/static/build/services/notify.js +73 -5
- package/static/build/services/open.js +106 -31
- package/static/build/services/retention.js +123 -0
- package/static/build/services/schedule.js +77 -43
- package/static/build/services/script.js +4 -5
- package/static/build/services/scriptHistory.js +30 -0
- package/static/build/services/sshKey.js +39 -14
- package/static/build/services/subscription.js +161 -87
- package/static/build/services/system.js +116 -43
- package/static/build/services/user.js +255 -37
- package/static/build/shared/auth.js +22 -3
- package/static/build/shared/authMutation.js +17 -0
- package/static/build/shared/childProcess.js +82 -0
- package/static/build/shared/clientIp.js +27 -0
- package/static/build/shared/cronCommand.js +17 -0
- package/static/build/shared/cronSchedule.js +101 -0
- package/static/build/shared/cronScheduler.js +73 -0
- package/static/build/shared/errors.js +9 -0
- package/static/build/shared/fileAccess.js +60 -0
- package/static/build/shared/i18n.js +63 -4
- package/static/build/shared/logPath.js +15 -0
- package/static/build/shared/logReader.js +85 -0
- package/static/build/shared/logStreamManager.js +80 -60
- package/static/build/shared/loginThrottle.js +45 -0
- package/static/build/shared/pLimit.js +22 -8
- package/static/build/shared/password.js +30 -0
- package/static/build/shared/retention.js +58 -0
- package/static/build/shared/runCron.js +39 -17
- package/static/build/shared/schedulerMutationLock.js +44 -0
- package/static/build/shared/schedulerReadiness.js +82 -0
- package/static/build/shared/schemaMigrations.js +58 -0
- package/static/build/shared/scriptHistory.js +445 -0
- package/static/build/shared/shellQuote.js +11 -0
- package/static/build/shared/startupProcess.js +48 -0
- package/static/build/shared/store.js +7 -0
- package/static/build/shared/subscriptionMutationLock.js +34 -0
- package/static/build/shared/subscriptionPath.js +41 -0
- package/static/build/shared/subscriptionStorage.js +98 -0
- package/static/build/shared/trustProxy.js +119 -0
- package/static/build/shared/utils.js +2 -1
- package/static/build/token.js +3 -14
- package/static/build/validation/schedule.js +6 -12
- package/static/dist/{1885.e1ea09f6.async.js → 1885.b8f427bf.async.js} +1 -1
- package/static/dist/2031.6af40025.async.js +1 -0
- package/static/dist/2693.51c2636b.async.js +1 -0
- package/static/dist/{2890.9e373f5b.async.js → 2890.add55e85.async.js} +1 -1
- package/static/dist/2901.2ca95295.async.js +1 -0
- package/static/dist/292.264757f0.async.js +1 -0
- package/static/dist/3512.95e41255.async.js +1 -0
- package/static/dist/445.a65ff63b.async.js +1 -0
- package/static/dist/4736.1d2b5cb6.async.js +1 -0
- package/static/dist/{6013.4e38de36.async.js → 6013.db535339.async.js} +1 -1
- package/static/dist/6212.9e6d9913.async.js +1 -0
- package/static/dist/{6339.ab305e96.async.js → 6431.4603ad0e.async.js} +1 -1
- package/static/dist/{6872.42f939f1.async.js → 6872.a503ef55.async.js} +1 -1
- package/static/dist/7231.f7e5b668.async.js +1 -0
- package/static/dist/{7355.f9f263d8.async.js → 7355.fca746f5.async.js} +1 -1
- package/static/dist/{7508.7dcee91c.async.js → 7508.d90eac63.async.js} +1 -1
- package/static/dist/{7984.594296a5.async.js → 7984.eaac7402.async.js} +1 -1
- package/static/dist/8844.a0fef08e.async.js +1 -0
- package/static/dist/index.html +2 -2
- package/static/dist/layouts__index.633d3553.async.js +1 -0
- package/static/dist/preload_helper.01fcd534.js +1 -0
- package/static/dist/{src__pages__config__index.a22ff7dc.async.js → src__pages__config__index.9aba4054.async.js} +1 -1
- package/static/dist/src__pages__crontab__detail.86b2eebb.async.js +1 -0
- package/static/dist/src__pages__crontab__index.ae4e06f2.async.js +1 -0
- package/static/dist/{src__pages__crontab__logModal.0b8cce8c.async.js → src__pages__crontab__logModal.930535e2.async.js} +1 -1
- package/static/dist/src__pages__crontab__modal.a2c8fdcc.async.js +1 -0
- package/static/dist/src__pages__crontab__viewCreateModal.7b4f9cbe.async.js +1 -0
- package/static/dist/src__pages__crontab__viewManageModal.cd454ea0.async.js +1 -0
- package/static/dist/src__pages__dashboard__index.243c5a35.async.js +1 -0
- package/static/dist/src__pages__dashboard__taskResultModal.7795d6e1.async.js +1 -0
- package/static/dist/src__pages__dependence__index.874d501f.async.js +1 -0
- package/static/dist/src__pages__dependence__logModal.154efe7a.async.js +1 -0
- package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
- package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
- package/static/dist/src__pages__env__editNameModal.c13d47d2.async.js +1 -0
- package/static/dist/src__pages__env__index.fd6fd2b0.async.js +1 -0
- package/static/dist/src__pages__env__modal.4d71adb3.async.js +1 -0
- package/static/dist/src__pages__error__index.aae50c94.async.js +1 -0
- package/static/dist/src__pages__initialization__index.82a2e967.async.js +1 -0
- package/static/dist/src__pages__log__index.71250e0d.async.js +1 -0
- package/static/dist/{src__pages__login__index.8c813eb1.async.js → src__pages__login__index.a0fd5ed8.async.js} +1 -1
- package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
- package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
- package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
- package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
- package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
- package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
- package/static/dist/src__pages__script__renameModal.0ab029e7.async.js +1 -0
- package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
- package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
- package/static/dist/src__pages__script__setting.35ad4f02.async.js +1 -0
- package/static/dist/src__pages__setting__appModal.7eaeb9d3.async.js +1 -0
- package/static/dist/src__pages__setting__checkUpdate.d175b154.async.js +1 -0
- package/static/dist/src__pages__setting__clientIp.3a8711ed.async.js +1 -0
- package/static/dist/src__pages__setting__dependence.9c0f749b.async.js +1 -0
- package/static/dist/src__pages__setting__index.cb397741.async.js +1 -0
- package/static/dist/src__pages__setting__loginLog.e0a1f54d.async.js +1 -0
- package/static/dist/src__pages__setting__notification.2ffe4e5a.async.js +1 -0
- package/static/dist/src__pages__setting__other.3d0b8492.async.js +1 -0
- package/static/dist/{src__pages__setting__progress.8823ac90.async.js → src__pages__setting__progress.2ccae3d7.async.js} +1 -1
- package/static/dist/src__pages__setting__security.e2d71ebb.async.js +1 -0
- package/static/dist/src__pages__setting__systemLog.b3046955.async.js +1 -0
- package/static/dist/src__pages__subscription__index.43086a06.async.js +1 -0
- package/static/dist/src__pages__subscription__logModal.cdba5139.async.js +1 -0
- package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
- package/static/dist/{umi.ca04a019.js → umi.3a24a9fa.js} +1 -1
- package/test/back/advisory-input-security.test.cjs +416 -0
- package/test/back/auth-security.test.cjs +74 -0
- package/test/back/build-provenance.test.cjs +150 -0
- package/test/back/code-scanning-security.test.cjs +189 -0
- package/test/back/cron-log-name.test.cjs +110 -0
- package/test/back/cron-macros.test.cjs +51 -0
- package/test/back/cron-schedule-compatibility.test.cjs +69 -0
- package/test/back/cron-scheduler-routing.test.cjs +59 -0
- package/test/back/cron-validation.test.cjs +45 -0
- package/test/back/dashboard-failures.test.cjs +173 -0
- package/test/back/database-bootstrap.test.cjs +193 -0
- package/test/back/dependence-cache-status.test.cjs +125 -0
- package/test/back/deprecated-file-routes.test.cjs +82 -0
- package/test/back/draft-path-security.test.cjs +235 -0
- package/test/back/ecosystem.test.cjs +28 -0
- package/test/back/env-import-security.test.cjs +91 -0
- package/test/back/env-name-parsing.test.cjs +96 -0
- package/test/back/execution-lifecycle.test.cjs +236 -0
- package/test/back/file-access-scope.test.cjs +180 -0
- package/test/back/file-access-security.test.cjs +47 -0
- package/test/back/fileTreeListing.test.cjs +57 -0
- package/test/back/grpc-cron-response.test.cjs +60 -0
- package/test/back/grpc-mtls-security.test.cjs +136 -0
- package/test/back/http-exclusive-listen.test.cjs +31 -0
- package/test/back/http-security.test.cjs +313 -0
- package/test/back/legacy-cron-recovery.test.cjs +76 -0
- package/test/back/linux-mirror-result.test.cjs +176 -0
- package/test/back/listen-security.test.cjs +68 -0
- package/test/back/log-path-security.test.cjs +158 -0
- package/test/back/logReader.test.cjs +72 -0
- package/test/back/manual-execution.test.cjs +85 -0
- package/test/back/manual-stop-claim.test.cjs +220 -0
- package/test/back/metrics.test.cjs +34 -0
- package/test/back/mirror-status.test.cjs +257 -0
- package/test/back/monitoring.test.cjs +73 -0
- package/test/back/node-cron-migration.test.cjs +65 -0
- package/test/back/node-path-cache.test.cjs +202 -0
- package/test/back/node-path-lock.test.cjs +182 -0
- package/test/back/npm-publication.test.cjs +70 -0
- package/test/back/preload-lazy-notify.test.cjs +168 -0
- package/test/back/primary-apm.test.cjs +24 -0
- package/test/back/process-exit-state.test.cjs +81 -0
- package/test/back/process-stop-signal.test.cjs +77 -0
- package/test/back/protected-path-case.test.cjs +58 -0
- package/test/back/qlServiceLogCommand.test.cjs +128 -0
- package/test/back/queued-cron-invalidation.test.cjs +98 -0
- package/test/back/restart-instance-recovery.test.cjs +116 -0
- package/test/back/retention.test.cjs +131 -0
- package/test/back/role-loading.test.cjs +118 -0
- package/test/back/schedule-health.test.cjs +86 -0
- package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
- package/test/back/scheduler-concurrency.test.cjs +411 -0
- package/test/back/scheduler-file-lock.test.cjs +51 -0
- package/test/back/scheduler-mutation.test.cjs +312 -0
- package/test/back/scheduler-readiness.test.cjs +85 -0
- package/test/back/scheduler-reconciliation.test.cjs +206 -0
- package/test/back/scheduler-rpc-timeout.test.cjs +49 -0
- package/test/back/schema-migrations.test.cjs +127 -0
- package/test/back/script-file-access.test.cjs +210 -0
- package/test/back/script-history-api.test.cjs +551 -0
- package/test/back/script-history.test.cjs +383 -0
- package/test/back/shell-api-parsing.test.cjs +184 -0
- package/test/back/shell-translation-output.test.cjs +16 -0
- package/test/back/shell-upgrade.test.cjs +312 -0
- package/test/back/shellApiToken.test.cjs +151 -0
- package/test/back/sock-polling.test.cjs +297 -0
- package/test/back/sock-security.test.cjs +72 -0
- package/test/back/ssh-config-home.test.cjs +113 -0
- package/test/back/start-env-preservation.test.cjs +53 -0
- package/test/back/static-mirror-trigger.test.cjs +441 -0
- package/test/back/static-mirror.test.cjs +179 -0
- package/test/back/stop-failure-feedback.test.cjs +158 -0
- package/test/back/stop-race.test.cjs +171 -0
- package/test/back/subscription-cleanup.test.cjs +53 -0
- package/test/back/subscription-mutation-lock.test.cjs +83 -0
- package/test/back/subscription-review-regression.test.cjs +607 -0
- package/test/back/subscription-stop-state.test.cjs +66 -0
- package/test/back/subscription-storage-regression.test.cjs +553 -0
- package/test/back/system-dependence-cache.test.cjs +127 -0
- package/test/back/system-export-history.test.cjs +119 -0
- package/test/back/systemTokenRenewal.test.cjs +497 -0
- package/test/back/task-shell-lifecycle.test.cjs +194 -0
- package/test/back/task-time.test.cjs +116 -0
- package/test/back/unique-constraint-error.test.cjs +72 -0
- package/test/back/upgrade-guards.test.cjs +285 -0
- package/test/back/upgrade-lifecycle.test.cjs +288 -0
- package/test/back/upgrade-transaction.test.cjs +133 -0
- package/test/back/user-api.test.cjs +99 -0
- package/test/back/user-security.test.cjs +349 -0
- package/test/back/worker-apm.test.cjs +84 -0
- package/test/back/wpush-grpc.test.cjs +116 -0
- package/test/fixtures/legacy-cron.json +1371 -0
- package/test/front/cron-log-pagination.test.cjs +214 -0
- package/test/front/diff-file-routing.test.cjs +126 -0
- package/test/front/http-auth.test.cjs +152 -0
- package/test/front/http-error.test.cjs +29 -0
- package/test/front/script-history-save.test.cjs +121 -0
- package/test/helpers/load-security-module.cjs +42 -0
- package/test/helpers/upgrade-fixture.cjs +30 -0
- package/version.yaml +14 -34
- package/CLAUDE.md +0 -43
- package/static/dist/1147.c420132e.async.js +0 -1
- package/static/dist/134.54df9e38.async.js +0 -1
- package/static/dist/2096.6d98fd12.async.js +0 -1
- package/static/dist/2208.f7ba3dfa.async.js +0 -1
- package/static/dist/2286.164bb089.async.js +0 -1
- package/static/dist/3191.63263871.async.js +0 -1
- package/static/dist/4859.6592cebb.async.js +0 -1
- package/static/dist/4902.555f92ab.async.js +0 -1
- package/static/dist/5520.ff2cfd6b.async.js +0 -1
- package/static/dist/6431.c50479be.async.js +0 -1
- package/static/dist/6707.801b886b.async.js +0 -1
- package/static/dist/8826.2447a104.async.js +0 -1
- package/static/dist/layouts__index.a7a2bfe0.async.js +0 -1
- package/static/dist/preload_helper.0431c0f3.js +0 -1
- package/static/dist/src__pages__crontab__detail.b9c36808.async.js +0 -1
- package/static/dist/src__pages__crontab__index.fed99fd0.async.js +0 -1
- package/static/dist/src__pages__crontab__modal.f7041c7c.async.js +0 -1
- package/static/dist/src__pages__crontab__viewCreateModal.4d589f66.async.js +0 -1
- package/static/dist/src__pages__crontab__viewManageModal.0e317746.async.js +0 -1
- package/static/dist/src__pages__dashboard__index.b30f2f47.async.js +0 -1
- package/static/dist/src__pages__dependence__index.3d87ceac.async.js +0 -1
- package/static/dist/src__pages__dependence__logModal.0681830b.async.js +0 -1
- package/static/dist/src__pages__dependence__modal.11124896.async.js +0 -1
- package/static/dist/src__pages__diff__index.013b7c75.async.js +0 -1
- package/static/dist/src__pages__env__editNameModal.5d264b25.async.js +0 -1
- package/static/dist/src__pages__env__index.baa27d4e.async.js +0 -1
- package/static/dist/src__pages__env__modal.7f2ef1bc.async.js +0 -1
- package/static/dist/src__pages__error__index.f156b45e.async.js +0 -1
- package/static/dist/src__pages__initialization__index.e96d4ba8.async.js +0 -1
- package/static/dist/src__pages__log__index.e0978bae.async.js +0 -1
- package/static/dist/src__pages__script__editModal.cbf4ec0e.async.js +0 -1
- package/static/dist/src__pages__script__editNameModal.3e7e100d.async.js +0 -1
- package/static/dist/src__pages__script__index.2765d1b8.async.js +0 -1
- package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
- package/static/dist/src__pages__script__renameModal.5e987ef5.async.js +0 -1
- package/static/dist/src__pages__script__saveModal.3f9d23d6.async.js +0 -1
- package/static/dist/src__pages__script__setting.a535793a.async.js +0 -1
- package/static/dist/src__pages__setting__appModal.251cd14f.async.js +0 -1
- package/static/dist/src__pages__setting__checkUpdate.46efe057.async.js +0 -1
- package/static/dist/src__pages__setting__dependence.8a7a9529.async.js +0 -1
- package/static/dist/src__pages__setting__index.cc85fdfb.async.js +0 -1
- package/static/dist/src__pages__setting__loginLog.cea6056c.async.js +0 -1
- package/static/dist/src__pages__setting__notification.390fc905.async.js +0 -1
- package/static/dist/src__pages__setting__other.b22d2165.async.js +0 -1
- package/static/dist/src__pages__setting__security.598720a8.async.js +0 -1
- package/static/dist/src__pages__setting__systemLog.67406721.async.js +0 -1
- package/static/dist/src__pages__subscription__index.d0ab2abc.async.js +0 -1
- package/static/dist/src__pages__subscription__logModal.3864b37f.async.js +0 -1
- package/static/dist/src__pages__subscription__modal.3562c670.async.js +0 -1
- /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const grpc = require('@grpc/grpc-js');
|
|
4
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
5
|
+
const { SchedulerReadiness } = require('../../back/shared/schedulerReadiness');
|
|
6
|
+
|
|
7
|
+
for (const [name, rows, delay, succeeds] of [
|
|
8
|
+
['large distinct snapshot', Array.from({length:10000}, (_, i) => ({id:String(i), schedule:`${i%60} ${Math.floor(i/60)%60} ${Math.floor(i/3600)} * * *`})), 10000, true],
|
|
9
|
+
['extra schedules', [{id:'extra', schedule:'* * * * *', extra_schedules:Array.from({length:2000},()=>({schedule:'0 * * * *'}))}], 10000, true],
|
|
10
|
+
['unresponsive small write', [{id:'small', schedule:'* * * * *'}], 30000, false],
|
|
11
|
+
['unresponsive large write remains bounded', Array.from({length:30000},()=>({schedule:'* * * * *'})), 180000, false],
|
|
12
|
+
]) {
|
|
13
|
+
test(`recovery deadline: ${name}`, async (t) => {
|
|
14
|
+
t.mock.timers.enable({apis:['Date','setTimeout'], now:0});
|
|
15
|
+
let calls=0, deadline;
|
|
16
|
+
const fake={waitForReady:(_,cb)=>cb(), addCron:(_req,_meta,options,cb)=>{
|
|
17
|
+
calls++; deadline=options.deadline;
|
|
18
|
+
const budget=deadline-Date.now();
|
|
19
|
+
setTimeout(()=>cb(delay<=budget ? null : Object.assign(Error('deadline'),{code:grpc.status.DEADLINE_EXCEEDED}), {}), Math.min(delay,budget));
|
|
20
|
+
}};
|
|
21
|
+
const client=load('back/schedule/client.ts', {
|
|
22
|
+
'../protos/cron':{CronClient:class{constructor(){return fake}}},
|
|
23
|
+
'../config':{grpcPort:5500},
|
|
24
|
+
'../config/grpcCerts':{getGrpcCerts:()=>({caCert:'ca',clientKey:'key',clientCert:'cert'})},
|
|
25
|
+
'@grpc/grpc-js':{...grpc,credentials:{createSsl:()=>({})}},
|
|
26
|
+
}).default;
|
|
27
|
+
const readiness=new SchedulerReadiness(async()=>{}, 60000);
|
|
28
|
+
t.after(()=>clearTimeout(readiness.retry));
|
|
29
|
+
client.readiness=readiness;
|
|
30
|
+
readiness.configure(()=>client.addCron(rows,true));
|
|
31
|
+
const recovering=readiness.recover();
|
|
32
|
+
await new Promise(setImmediate);
|
|
33
|
+
assert.equal(calls,1);
|
|
34
|
+
assert.ok(deadline<=120000);
|
|
35
|
+
t.mock.timers.tick(Math.min(delay,deadline));
|
|
36
|
+
assert.equal(await recovering,succeeds);
|
|
37
|
+
assert.equal(calls,1,'uncertain writes are not replayed inline');
|
|
38
|
+
assert.equal(await readiness.check(),succeeds);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const os = require('node:os');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const grpc = require('@grpc/grpc-js');
|
|
7
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
8
|
+
const logger = { info() {}, debug() {}, warn() {}, error() {} };
|
|
9
|
+
const tick = () => new Promise(setImmediate);
|
|
10
|
+
function createLimit() {
|
|
11
|
+
return load('back/shared/pLimit.ts', {
|
|
12
|
+
'../data/system': {
|
|
13
|
+
AuthDataType: { systemConfig: 'systemConfig' },
|
|
14
|
+
SystemModel: {
|
|
15
|
+
sync: async () => {},
|
|
16
|
+
findOne: async () => ({ info: { cronConcurrency: 1 } }),
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
'../loaders/logger': logger,
|
|
20
|
+
'../services/notify': class {},
|
|
21
|
+
'../shared/i18n': { t: (x) => x, tf: (x) => x },
|
|
22
|
+
'../config': {},
|
|
23
|
+
'../config/grpcCerts': { getGrpcCerts: () => null },
|
|
24
|
+
'../protos/api': {},
|
|
25
|
+
}).default;
|
|
26
|
+
}
|
|
27
|
+
function createSystem(client, limit, options = {}) {
|
|
28
|
+
const store = options.store || {
|
|
29
|
+
state: {
|
|
30
|
+
id: 1,
|
|
31
|
+
type: 'systemConfig',
|
|
32
|
+
info: { cronConcurrency: 1, timezone: 'Asia/Shanghai' },
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const writes = [];
|
|
36
|
+
const Service = load('back/services/system.ts', {
|
|
37
|
+
'../config': options.config || {},
|
|
38
|
+
'../shared/schedulerMutationLock': options.config
|
|
39
|
+
? load('back/shared/schedulerMutationLock.ts', {
|
|
40
|
+
'../config': options.config,
|
|
41
|
+
})
|
|
42
|
+
: { withSchedulerMutation: (operation) => operation() },
|
|
43
|
+
'../config/const': {},
|
|
44
|
+
'../config/util': {},
|
|
45
|
+
'../data/dependence': {},
|
|
46
|
+
'../data/notify': {},
|
|
47
|
+
'../data/system': {
|
|
48
|
+
AuthDataType: { systemConfig: 'systemConfig' },
|
|
49
|
+
SystemModel: {
|
|
50
|
+
findOne: async () => ({
|
|
51
|
+
...store.state,
|
|
52
|
+
get: () => ({ ...store.state }),
|
|
53
|
+
}),
|
|
54
|
+
update: async (values) => {
|
|
55
|
+
writes.push(values);
|
|
56
|
+
store.state = { ...store.state, ...values };
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
'../shared/pLimit': limit,
|
|
61
|
+
'../schedule/client': client,
|
|
62
|
+
'../shared/i18n': { t: (x) => x },
|
|
63
|
+
'./notify': class {},
|
|
64
|
+
'./schedule': class {},
|
|
65
|
+
'./sock': class {},
|
|
66
|
+
}).default;
|
|
67
|
+
return {
|
|
68
|
+
service: new Service(logger, {}, {}),
|
|
69
|
+
writes,
|
|
70
|
+
state: () => store.state,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async function rpcFixture(t) {
|
|
74
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-concurrency-rpc-'));
|
|
75
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
76
|
+
const certs = await load('back/config/grpcCerts.ts', {
|
|
77
|
+
'./index': { configPath: root },
|
|
78
|
+
'./util': { fileExist: async (p) => fs.existsSync(p) },
|
|
79
|
+
'../loaders/logger': logger,
|
|
80
|
+
}).initGrpcCerts();
|
|
81
|
+
const schedulerLimit = createLimit(),
|
|
82
|
+
apiLimit = createLimit();
|
|
83
|
+
await tick();
|
|
84
|
+
const handler = load('back/schedule/setConcurrency.ts', {
|
|
85
|
+
'../shared/pLimit': schedulerLimit,
|
|
86
|
+
});
|
|
87
|
+
let port;
|
|
88
|
+
const Grpc = load('back/services/grpc.ts', {
|
|
89
|
+
'../config': { bindHostGrpc: '127.0.0.1', grpcPort: 0 },
|
|
90
|
+
'../config/grpcCerts': { initGrpcCerts: async () => certs },
|
|
91
|
+
'../loaders/logger': logger,
|
|
92
|
+
'./metrics': { metricsService: { record() {} } },
|
|
93
|
+
'../schedule/addCron': {},
|
|
94
|
+
'../schedule/delCron': {},
|
|
95
|
+
'../schedule/api': {},
|
|
96
|
+
'../schedule/health': { check: (_call, cb) => cb(null, { status: 1 }) },
|
|
97
|
+
'../schedule/setConcurrency': handler,
|
|
98
|
+
'@grpc/grpc-js': {
|
|
99
|
+
...grpc,
|
|
100
|
+
Server: class extends grpc.Server {
|
|
101
|
+
bindAsync(address, creds, cb) {
|
|
102
|
+
super.bindAsync(address, creds, (error, boundPort) => {
|
|
103
|
+
port = boundPort;
|
|
104
|
+
cb(error, boundPort);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
}).GrpcServerService;
|
|
110
|
+
const server = new Grpc();
|
|
111
|
+
await server.initialize();
|
|
112
|
+
const client = load('back/schedule/client.ts', {
|
|
113
|
+
'../config': { grpcPort: port },
|
|
114
|
+
'../config/grpcCerts': { getGrpcCerts: () => certs },
|
|
115
|
+
}).default;
|
|
116
|
+
t.after(async () => {
|
|
117
|
+
client._client?.close();
|
|
118
|
+
await server.shutdown();
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
...createSystem(client, apiLimit),
|
|
122
|
+
schedulerLimit,
|
|
123
|
+
apiLimit,
|
|
124
|
+
client,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function enqueue(t, limit, count) {
|
|
128
|
+
const releases = [],
|
|
129
|
+
runs = [];
|
|
130
|
+
for (let i = 0; i < count; i++) {
|
|
131
|
+
let release;
|
|
132
|
+
const gate = new Promise((resolve) => {
|
|
133
|
+
release = resolve;
|
|
134
|
+
});
|
|
135
|
+
releases.push(release);
|
|
136
|
+
runs.push(limit.runWithCronLimit({ id: String(i) }, () => gate));
|
|
137
|
+
}
|
|
138
|
+
t.after(async () => {
|
|
139
|
+
releases.forEach((release) => release());
|
|
140
|
+
await Promise.all(runs);
|
|
141
|
+
});
|
|
142
|
+
return { releases, runs };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
test('concurrency changes reach the independent scheduler queue over mTLS immediately and preserve running jobs', async (t) => {
|
|
146
|
+
const f = await rpcFixture(t);
|
|
147
|
+
const { releases, runs } = enqueue(t, f.schedulerLimit, 3);
|
|
148
|
+
await tick();
|
|
149
|
+
assert.equal(f.schedulerLimit.cronLimitActiveCount, 1);
|
|
150
|
+
assert.equal(f.schedulerLimit.cronLimitPendingCount, 2);
|
|
151
|
+
assert.equal(
|
|
152
|
+
(await f.service.updateCronConcurrency({ cronConcurrency: 2 })).code,
|
|
153
|
+
200,
|
|
154
|
+
);
|
|
155
|
+
await tick();
|
|
156
|
+
assert.equal(f.schedulerLimit.cronLimitActiveCount, 2);
|
|
157
|
+
assert.equal(f.schedulerLimit.cronLimitPendingCount, 1);
|
|
158
|
+
await f.service.updateCronConcurrency({ cronConcurrency: 1 });
|
|
159
|
+
assert.equal(
|
|
160
|
+
f.schedulerLimit.cronLimitActiveCount,
|
|
161
|
+
2,
|
|
162
|
+
'lowering the limit must not stop active jobs',
|
|
163
|
+
);
|
|
164
|
+
releases[0]();
|
|
165
|
+
await runs[0];
|
|
166
|
+
await tick();
|
|
167
|
+
assert.equal(f.schedulerLimit.cronLimitActiveCount, 1);
|
|
168
|
+
assert.equal(f.schedulerLimit.cronLimitPendingCount, 1);
|
|
169
|
+
releases[1]();
|
|
170
|
+
await runs[1];
|
|
171
|
+
await tick();
|
|
172
|
+
assert.equal(f.schedulerLimit.cronLimitActiveCount, 1);
|
|
173
|
+
assert.equal(f.schedulerLimit.cronLimitPendingCount, 0);
|
|
174
|
+
releases[2]();
|
|
175
|
+
await runs[2];
|
|
176
|
+
assert.equal(f.state().info.cronConcurrency, 1);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('zero, null and omitted concurrency restore the default in both API and scheduler queues without a restart', async (t) => {
|
|
180
|
+
const f = await rpcFixture(t);
|
|
181
|
+
for (const value of [0, null, undefined]) {
|
|
182
|
+
await f.service.updateCronConcurrency({ cronConcurrency: 1 });
|
|
183
|
+
const scheduler = enqueue(t, f.schedulerLimit, 4),
|
|
184
|
+
manual = enqueue(t, f.apiLimit, 4);
|
|
185
|
+
await tick();
|
|
186
|
+
assert.equal(f.schedulerLimit.cronLimitActiveCount, 1);
|
|
187
|
+
assert.equal(f.apiLimit.cronLimitActiveCount, 1);
|
|
188
|
+
await f.service.updateCronConcurrency(
|
|
189
|
+
value === undefined ? {} : { cronConcurrency: value },
|
|
190
|
+
);
|
|
191
|
+
await tick();
|
|
192
|
+
assert.equal(
|
|
193
|
+
f.state().info.cronConcurrency,
|
|
194
|
+
0,
|
|
195
|
+
'persist the same default that both queues apply',
|
|
196
|
+
);
|
|
197
|
+
assert.equal(f.schedulerLimit.cronLimitActiveCount, 4);
|
|
198
|
+
assert.equal(f.apiLimit.cronLimitActiveCount, 4);
|
|
199
|
+
[...scheduler.releases, ...manual.releases].forEach((release) => release());
|
|
200
|
+
await Promise.all([...scheduler.runs, ...manual.runs]);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('invalid concurrency is rejected before configuration writes or queue changes', async () => {
|
|
205
|
+
const mutations = [];
|
|
206
|
+
const f = createSystem(
|
|
207
|
+
{ setConcurrency: async (value) => mutations.push(value) },
|
|
208
|
+
{ setCustomLimit: async (value) => mutations.push(value) },
|
|
209
|
+
);
|
|
210
|
+
for (const value of [-1, 1.5, NaN, Infinity, 2147483648])
|
|
211
|
+
await assert.rejects(
|
|
212
|
+
f.service.updateCronConcurrency({ cronConcurrency: value }),
|
|
213
|
+
{ status: 400 },
|
|
214
|
+
);
|
|
215
|
+
assert.deepEqual(f.writes, []);
|
|
216
|
+
assert.deepEqual(mutations, []);
|
|
217
|
+
const handler = load('back/schedule/setConcurrency.ts', {
|
|
218
|
+
'../shared/pLimit': {
|
|
219
|
+
setCustomLimit: async (value) => mutations.push(value),
|
|
220
|
+
},
|
|
221
|
+
}).setConcurrency;
|
|
222
|
+
for (const value of [-1, 0.5, 2147483648])
|
|
223
|
+
await handler({ request: { concurrency: value } }, (error) =>
|
|
224
|
+
assert.equal(error.code, grpc.status.INVALID_ARGUMENT),
|
|
225
|
+
);
|
|
226
|
+
assert.deepEqual(mutations, []);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
for (const code of [grpc.status.UNAVAILABLE, grpc.status.DEADLINE_EXCEEDED]) {
|
|
230
|
+
test(`concurrency RPC failure ${code} invalidates readiness and is not reported as success or retried`, async () => {
|
|
231
|
+
let writes = 0,
|
|
232
|
+
invalidations = 0;
|
|
233
|
+
const fake = {
|
|
234
|
+
waitForReady: (_deadline, callback) => callback(),
|
|
235
|
+
setConcurrency: (_request, _metadata, options, callback) => {
|
|
236
|
+
assert.ok(options.deadline > Date.now());
|
|
237
|
+
writes++;
|
|
238
|
+
callback(Object.assign(new Error('scheduler unavailable'), { code }));
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
const client = load('back/schedule/client.ts', {
|
|
242
|
+
'../protos/cron': {
|
|
243
|
+
CronClient: class {
|
|
244
|
+
constructor() {
|
|
245
|
+
return fake;
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
'../config': { grpcPort: 1 },
|
|
250
|
+
'../config/grpcCerts': {
|
|
251
|
+
getGrpcCerts: () => ({
|
|
252
|
+
caCert: 'ca',
|
|
253
|
+
clientKey: 'key',
|
|
254
|
+
clientCert: 'cert',
|
|
255
|
+
}),
|
|
256
|
+
},
|
|
257
|
+
'@grpc/grpc-js': { ...grpc, credentials: { createSsl: () => ({}) } },
|
|
258
|
+
}).default;
|
|
259
|
+
client.readiness.invalidate = () => invalidations++;
|
|
260
|
+
const f = createSystem(client, { setCustomLimit: async () => {} });
|
|
261
|
+
await assert.rejects(
|
|
262
|
+
f.service.updateCronConcurrency({ cronConcurrency: 2 }),
|
|
263
|
+
{ status: 503 },
|
|
264
|
+
);
|
|
265
|
+
assert.equal(writes, 1);
|
|
266
|
+
assert.equal(invalidations, 1);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function mutationConfig(t) {
|
|
271
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-concurrency-lock-'));
|
|
272
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
273
|
+
return { crontabFile: path.join(root, 'crontab.list') };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
test('independent API service instances serialize database and scheduler updates with the shared filesystem lock', async (t) => {
|
|
277
|
+
const config = mutationConfig(t);
|
|
278
|
+
const store = { state: { id: 1, info: { cronConcurrency: 1 } } };
|
|
279
|
+
let release, entered;
|
|
280
|
+
const firstEntered = new Promise((resolve) => (entered = resolve));
|
|
281
|
+
const gate = new Promise((resolve) => (release = resolve));
|
|
282
|
+
t.after(() => release());
|
|
283
|
+
let scheduler = 1,
|
|
284
|
+
api = 1;
|
|
285
|
+
const client = {
|
|
286
|
+
setConcurrency: async (value) => {
|
|
287
|
+
if (value === 10) {
|
|
288
|
+
entered();
|
|
289
|
+
await gate;
|
|
290
|
+
}
|
|
291
|
+
scheduler = value;
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
const limit = { setCustomLimit: async (value) => (api = value) };
|
|
295
|
+
const a = createSystem(client, limit, { config, store });
|
|
296
|
+
const b = createSystem(client, limit, { config, store });
|
|
297
|
+
const first = a.service.updateCronConcurrency({ cronConcurrency: 10 });
|
|
298
|
+
await firstEntered;
|
|
299
|
+
const second = b.service.updateCronConcurrency({ cronConcurrency: 1 });
|
|
300
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
301
|
+
assert.equal(
|
|
302
|
+
b.writes.length,
|
|
303
|
+
0,
|
|
304
|
+
'a second writer cannot persist ahead of the first RPC',
|
|
305
|
+
);
|
|
306
|
+
release();
|
|
307
|
+
await Promise.all([first, second]);
|
|
308
|
+
assert.deepEqual(
|
|
309
|
+
[store.state.info.cronConcurrency, api, scheduler],
|
|
310
|
+
[1, 1, 1],
|
|
311
|
+
);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
for (const newerLimit of [undefined, 2]) {
|
|
315
|
+
test(`production recovery restores persisted concurrency before health becomes ready (newer limit: ${newerLimit})`, async (t) => {
|
|
316
|
+
const config = mutationConfig(t);
|
|
317
|
+
const store = { state: { id: 1, info: { cronConcurrency: 1 } } };
|
|
318
|
+
let scheduler = 1,
|
|
319
|
+
api = 1,
|
|
320
|
+
fail = false,
|
|
321
|
+
restoreJobs = 0;
|
|
322
|
+
let unblockRecovery, recoveryEntered;
|
|
323
|
+
const entered = new Promise((resolve) => (recoveryEntered = resolve));
|
|
324
|
+
const gate = new Promise((resolve) => (unblockRecovery = resolve));
|
|
325
|
+
t.after(() => unblockRecovery());
|
|
326
|
+
const fake = {
|
|
327
|
+
waitForReady: (_deadline, callback) => callback(),
|
|
328
|
+
makeUnaryRequest: (...args) => args.at(-1)(null, { status: 1 }),
|
|
329
|
+
setConcurrency: ({ concurrency }, _metadata, _options, callback) => {
|
|
330
|
+
if (fail) {
|
|
331
|
+
fail = false;
|
|
332
|
+
callback(
|
|
333
|
+
Object.assign(new Error('lost RPC'), {
|
|
334
|
+
code: grpc.status.UNAVAILABLE,
|
|
335
|
+
}),
|
|
336
|
+
);
|
|
337
|
+
} else {
|
|
338
|
+
scheduler = concurrency;
|
|
339
|
+
callback(null, {});
|
|
340
|
+
}
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
const client = load('back/schedule/client.ts', {
|
|
344
|
+
'../protos/cron': {
|
|
345
|
+
CronClient: class {
|
|
346
|
+
constructor() {
|
|
347
|
+
return fake;
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
'../config': { grpcPort: 1 },
|
|
352
|
+
'../config/grpcCerts': {
|
|
353
|
+
getGrpcCerts: () => ({
|
|
354
|
+
caCert: 'ca',
|
|
355
|
+
clientKey: 'key',
|
|
356
|
+
clientCert: 'cert',
|
|
357
|
+
}),
|
|
358
|
+
},
|
|
359
|
+
'@grpc/grpc-js': { ...grpc, credentials: { createSsl: () => ({}) } },
|
|
360
|
+
}).default;
|
|
361
|
+
const f = createSystem(
|
|
362
|
+
client,
|
|
363
|
+
{ setCustomLimit: async (value) => (api = value) },
|
|
364
|
+
{ config, store },
|
|
365
|
+
);
|
|
366
|
+
const source = fs.readFileSync('back/loaders/initData.ts', 'utf8');
|
|
367
|
+
const callback = source.match(
|
|
368
|
+
/cronClient\.readiness\.configure\((async \(\) => \{[\s\S]*?\n \})\);/,
|
|
369
|
+
)[1];
|
|
370
|
+
const restore = new Function(
|
|
371
|
+
'cronService',
|
|
372
|
+
'systemService',
|
|
373
|
+
`return ${callback}`,
|
|
374
|
+
)(
|
|
375
|
+
{
|
|
376
|
+
autosave_crontab: async (strict) => {
|
|
377
|
+
assert.equal(strict, true);
|
|
378
|
+
restoreJobs++;
|
|
379
|
+
if (restoreJobs > 1) {
|
|
380
|
+
recoveryEntered();
|
|
381
|
+
await gate;
|
|
382
|
+
}
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
f.service,
|
|
386
|
+
);
|
|
387
|
+
client.readiness.configure(restore);
|
|
388
|
+
assert.equal(await client.readiness.recover(), true);
|
|
389
|
+
fail = true;
|
|
390
|
+
await assert.rejects(
|
|
391
|
+
f.service.updateCronConcurrency({ cronConcurrency: 10 }),
|
|
392
|
+
{ status: 503 },
|
|
393
|
+
);
|
|
394
|
+
await entered;
|
|
395
|
+
assert.equal(await client.readiness.check(), false);
|
|
396
|
+
assert.equal(scheduler, 1);
|
|
397
|
+
// A newer successful request must win over the failed request being recovered.
|
|
398
|
+
if (newerLimit !== undefined)
|
|
399
|
+
await f.service.updateCronConcurrency({ cronConcurrency: newerLimit });
|
|
400
|
+
scheduler = 1; // Simulate the scheduler restarting while recovery is in progress.
|
|
401
|
+
const recovered = client.readiness.recover();
|
|
402
|
+
unblockRecovery();
|
|
403
|
+
assert.equal(await recovered, true);
|
|
404
|
+
const expected = newerLimit ?? 10;
|
|
405
|
+
assert.deepEqual(
|
|
406
|
+
[store.state.info.cronConcurrency, api, scheduler],
|
|
407
|
+
[expected, expected, expected],
|
|
408
|
+
);
|
|
409
|
+
assert.equal(await client.readiness.check(), true);
|
|
410
|
+
});
|
|
411
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('node:fs/promises');
|
|
4
|
+
const os = require('node:os');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
7
|
+
|
|
8
|
+
for (const failOperation of [false, true]) {
|
|
9
|
+
test(`scheduler lock survives nested file writes and releases after ${
|
|
10
|
+
failOperation ? 'failure' : 'success'
|
|
11
|
+
}`, async (t) => {
|
|
12
|
+
// Match production's canonical /ql path even when macOS temp is a symlink.
|
|
13
|
+
const root = await fs.realpath(
|
|
14
|
+
await fs.mkdtemp(path.join(os.tmpdir(), 'ql-nested-lock-')),
|
|
15
|
+
);
|
|
16
|
+
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
|
17
|
+
const config = { crontabFile: path.join(root, 'crontab.list') };
|
|
18
|
+
await fs.writeFile(config.crontabFile, '');
|
|
19
|
+
const { withSchedulerMutation } = load(
|
|
20
|
+
'back/shared/schedulerMutationLock.ts',
|
|
21
|
+
{
|
|
22
|
+
'../config': config,
|
|
23
|
+
},
|
|
24
|
+
);
|
|
25
|
+
const { writeFileWithLock } = load('back/shared/utils.ts', {
|
|
26
|
+
'../config/util': {
|
|
27
|
+
fileExist: async (file) =>
|
|
28
|
+
fs.access(file).then(
|
|
29
|
+
() => true,
|
|
30
|
+
() => false,
|
|
31
|
+
),
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const failure = new Error('registration failed');
|
|
35
|
+
const operation = withSchedulerMutation(async () => {
|
|
36
|
+
await writeFileWithLock(config.crontabFile, 'first');
|
|
37
|
+
await writeFileWithLock(config.crontabFile, 'second');
|
|
38
|
+
if (failOperation) throw failure;
|
|
39
|
+
});
|
|
40
|
+
if (failOperation)
|
|
41
|
+
await assert.rejects(operation, (error) => error === failure);
|
|
42
|
+
else await operation;
|
|
43
|
+
await assert.rejects(fs.stat(`${config.crontabFile}.scheduler.lock`), {
|
|
44
|
+
code: 'ENOENT',
|
|
45
|
+
});
|
|
46
|
+
await withSchedulerMutation(() =>
|
|
47
|
+
writeFileWithLock(config.crontabFile, 'recovered'),
|
|
48
|
+
);
|
|
49
|
+
assert.equal(await fs.readFile(config.crontabFile, 'utf8'), 'recovered');
|
|
50
|
+
});
|
|
51
|
+
}
|