@nocobase/app-plugin-releases 0.1.0-beta.18 → 0.1.0-beta.19

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.
Files changed (92) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +18 -0
  3. package/dist/client/components/release-badges.d.ts.map +1 -1
  4. package/dist/client/components/release-badges.js +10 -1
  5. package/dist/client/components/release-badges.js.map +1 -1
  6. package/dist/client/components/route-overlay.d.ts +4 -1
  7. package/dist/client/components/route-overlay.d.ts.map +1 -1
  8. package/dist/client/components/route-overlay.js +32 -3
  9. package/dist/client/components/route-overlay.js.map +1 -1
  10. package/dist/client/components/use-route-overlay.d.ts +2 -9
  11. package/dist/client/components/use-route-overlay.d.ts.map +1 -1
  12. package/dist/client/components/use-route-overlay.js +5 -3
  13. package/dist/client/components/use-route-overlay.js.map +1 -1
  14. package/dist/client/hooks/use-app-summary.d.ts +1 -1
  15. package/dist/client/hooks/use-app-summary.d.ts.map +1 -1
  16. package/dist/client/hooks/use-app-summary.js +4 -1
  17. package/dist/client/hooks/use-app-summary.js.map +1 -1
  18. package/dist/client/lib/app-state.d.ts.map +1 -1
  19. package/dist/client/lib/app-state.js +1 -1
  20. package/dist/client/lib/app-state.js.map +1 -1
  21. package/dist/client/lib/errors.d.ts.map +1 -1
  22. package/dist/client/lib/errors.js +7 -0
  23. package/dist/client/lib/errors.js.map +1 -1
  24. package/dist/client/locales/de-DE.d.ts.map +1 -1
  25. package/dist/client/locales/de-DE.js +9 -0
  26. package/dist/client/locales/de-DE.js.map +1 -1
  27. package/dist/client/locales/en-US.d.ts +9 -0
  28. package/dist/client/locales/en-US.d.ts.map +1 -1
  29. package/dist/client/locales/en-US.js +9 -0
  30. package/dist/client/locales/en-US.js.map +1 -1
  31. package/dist/client/locales/es-ES.d.ts.map +1 -1
  32. package/dist/client/locales/es-ES.js +9 -0
  33. package/dist/client/locales/es-ES.js.map +1 -1
  34. package/dist/client/locales/fr-FR.d.ts.map +1 -1
  35. package/dist/client/locales/fr-FR.js +9 -0
  36. package/dist/client/locales/fr-FR.js.map +1 -1
  37. package/dist/client/locales/id-ID.d.ts.map +1 -1
  38. package/dist/client/locales/id-ID.js +9 -0
  39. package/dist/client/locales/id-ID.js.map +1 -1
  40. package/dist/client/locales/ja-JP.d.ts.map +1 -1
  41. package/dist/client/locales/ja-JP.js +9 -0
  42. package/dist/client/locales/ja-JP.js.map +1 -1
  43. package/dist/client/locales/pt-BR.d.ts.map +1 -1
  44. package/dist/client/locales/pt-BR.js +9 -0
  45. package/dist/client/locales/pt-BR.js.map +1 -1
  46. package/dist/client/locales/vi-VN.d.ts.map +1 -1
  47. package/dist/client/locales/vi-VN.js +9 -0
  48. package/dist/client/locales/vi-VN.js.map +1 -1
  49. package/dist/client/locales/zh-CN.d.ts.map +1 -1
  50. package/dist/client/locales/zh-CN.js +9 -0
  51. package/dist/client/locales/zh-CN.js.map +1 -1
  52. package/dist/client/locales/zh-TW.d.ts.map +1 -1
  53. package/dist/client/locales/zh-TW.js +9 -0
  54. package/dist/client/locales/zh-TW.js.map +1 -1
  55. package/dist/client/pages/app-page.js +12 -7
  56. package/dist/client/pages/app-page.js.map +1 -1
  57. package/dist/client/routes.d.ts.map +1 -1
  58. package/dist/client/routes.js +15 -0
  59. package/dist/client/routes.js.map +1 -1
  60. package/dist/server/config.d.ts +9 -1
  61. package/dist/server/config.d.ts.map +1 -1
  62. package/dist/server/drivers/host/driver.d.ts +10 -2
  63. package/dist/server/drivers/host/driver.d.ts.map +1 -1
  64. package/dist/server/drivers/host/driver.js +128 -17
  65. package/dist/server/drivers/host/driver.js.map +1 -1
  66. package/dist/server/drivers/types.d.ts +8 -1
  67. package/dist/server/drivers/types.d.ts.map +1 -1
  68. package/dist/server/drivers/types.js.map +1 -1
  69. package/dist/server/providers/releases.d.ts.map +1 -1
  70. package/dist/server/providers/releases.js +4 -0
  71. package/dist/server/providers/releases.js.map +1 -1
  72. package/dist/server/routes/api.d.ts +1 -1
  73. package/dist/server/routes/api.d.ts.map +1 -1
  74. package/dist/server/routes/api.js +37 -9
  75. package/dist/server/routes/api.js.map +1 -1
  76. package/dist/server/routes/index.d.ts.map +1 -1
  77. package/dist/server/routes/index.js +10 -2
  78. package/dist/server/routes/index.js.map +1 -1
  79. package/dist/server/routes/schemas.d.ts.map +1 -1
  80. package/dist/server/routes/schemas.js +19 -1
  81. package/dist/server/routes/schemas.js.map +1 -1
  82. package/dist/server/services/releases.d.ts +13 -1
  83. package/dist/server/services/releases.d.ts.map +1 -1
  84. package/dist/server/services/releases.js +320 -53
  85. package/dist/server/services/releases.js.map +1 -1
  86. package/dist/server/tokens.d.ts +9 -0
  87. package/dist/server/tokens.d.ts.map +1 -1
  88. package/dist/server/tokens.js.map +1 -1
  89. package/dist/shared/releases.d.ts +9 -2
  90. package/dist/shared/releases.d.ts.map +1 -1
  91. package/dist/shared/releases.js.map +1 -1
  92. package/package.json +3 -3
@@ -33,6 +33,8 @@ export function archiveFileName(appId, version) {
33
33
  }
34
34
  export class ReleasesService {
35
35
  options;
36
+ dispatch = new Map();
37
+ progress = new Map();
36
38
  diagnostic;
37
39
  disk;
38
40
  artifacts;
@@ -492,8 +494,6 @@ export class ReleasesService {
492
494
  createdVia: caller.kind,
493
495
  createdAt: new Date(),
494
496
  };
495
- if (input.deploy)
496
- await this.requireNoPendingDeployment(appId);
497
497
  // A release whose required variables are not all set is kept, and not deployed: once they are set, it
498
498
  // deploys by its ID without another upload.
499
499
  const missing = input.deploy
@@ -511,8 +511,6 @@ export class ReleasesService {
511
511
  contentType: 'application/gzip',
512
512
  });
513
513
  await this.options.database.transaction(async (connection) => {
514
- if (deployment)
515
- await this.requireNoPendingDeployment(appId, connection);
516
514
  await connection.query
517
515
  .insertInto('relReleases')
518
516
  .values(encodeRelease(release))
@@ -568,8 +566,16 @@ export class ReleasesService {
568
566
  });
569
567
  if (missing.length > 0)
570
568
  throw variablesMissing(current, release, missing);
571
- if (deployment)
569
+ if (deployment) {
570
+ try {
571
+ await this.replaceQueued(deployment);
572
+ }
573
+ catch (error) {
574
+ await this.markFailed(deployment, error);
575
+ throw error;
576
+ }
572
577
  this.schedule(deployment);
578
+ }
573
579
  return { ...view, reused: false, deploymentId: deployment?.id ?? null };
574
580
  });
575
581
  }
@@ -708,6 +714,7 @@ export class ReleasesService {
708
714
  throw new ReleasesError('Configuration content is required.', 'INVALID_CONFIG_FILE', 'INVALID_ARGUMENT');
709
715
  assertConfigSize(content);
710
716
  return await this.withLock(appId, async () => {
717
+ await this.waitForAppDeployments(appId);
711
718
  const app = await this.requireApp(appId);
712
719
  await this.options.guard.requireApp(caller, 'configure', app);
713
720
  const deployment = await this.currentDeployment(app);
@@ -831,12 +838,12 @@ export class ReleasesService {
831
838
  .offset((page - 1) * pageSize)
832
839
  .execute();
833
840
  return {
834
- items: rows.map((row) => deploymentView(decodeDeployment(row), typeof row.releaseVersion === 'string'
841
+ items: rows.map((row) => this.progressView(deploymentView(decodeDeployment(row), typeof row.releaseVersion === 'string'
835
842
  ? {
836
843
  version: row.releaseVersion,
837
844
  checksum: String(row.releaseChecksum),
838
845
  }
839
- : null)),
846
+ : null))),
840
847
  total,
841
848
  page,
842
849
  pageSize,
@@ -852,7 +859,14 @@ export class ReleasesService {
852
859
  await this.options.guard.requireApp(caller, 'deploy', app);
853
860
  const deployment = await this.requireDeployment(appId, deploymentId);
854
861
  const release = await this.findRelease(appId, deployment.releaseId);
855
- return deploymentView(deployment, release ? { version: release.version, checksum: release.checksum } : null);
862
+ if (['queued', 'deploying', 'cancelling'].includes(deployment.status)) {
863
+ const progress = await (await this.session(app.environmentId)).progress?.(deployment.id, app.id);
864
+ if (progress)
865
+ this.progress.set(deployment.id, progress);
866
+ }
867
+ return this.progressView(deploymentView(deployment, release
868
+ ? { version: release.version, checksum: release.checksum }
869
+ : null));
856
870
  }
857
871
  /** Resolves once the deployment has finished, or with its current state at the timeout. */
858
872
  async waitForDeployment(deploymentId, timeoutMs = 120_000) {
@@ -866,7 +880,7 @@ export class ReleasesService {
866
880
  if (!row)
867
881
  throw notFound('Deployment', 'DEPLOYMENT_NOT_FOUND', deploymentId);
868
882
  const deployment = decodeDeployment(row);
869
- const finished = deployment.status !== 'queued' && deployment.status !== 'deploying';
883
+ const finished = !['queued', 'deploying', 'cancelling'].includes(deployment.status);
870
884
  if (finished || Date.now() >= deadline) {
871
885
  // A finished deployment's run still announces it (events, the request it settled); wait for that too, so a
872
886
  // waiter never sees the deployment done but its consequences pending.
@@ -921,6 +935,7 @@ export class ReleasesService {
921
935
  // --- Lifecycle ----------------------------------------------------------------------------------------------
922
936
  async start(caller, appId) {
923
937
  return await this.withLock(appId, async () => {
938
+ await this.waitForAppDeployments(appId);
924
939
  const app = await this.requireApp(appId);
925
940
  await this.options.guard.requireApp(caller, 'operate', app);
926
941
  const deployment = await this.currentDeployment(app);
@@ -941,6 +956,7 @@ export class ReleasesService {
941
956
  }
942
957
  async stop(caller, appId) {
943
958
  return await this.withLock(appId, async () => {
959
+ await this.waitForAppDeployments(appId);
944
960
  const app = await this.requireApp(appId);
945
961
  await this.options.guard.requireApp(caller, 'operate', app);
946
962
  if (!(await this.currentDeployment(app)))
@@ -958,6 +974,7 @@ export class ReleasesService {
958
974
  }
959
975
  async restart(caller, appId) {
960
976
  return await this.withLock(appId, async () => {
977
+ await this.waitForAppDeployments(appId);
961
978
  const app = await this.requireApp(appId);
962
979
  await this.options.guard.requireApp(caller, 'operate', app);
963
980
  if (!(await this.currentDeployment(app)))
@@ -984,14 +1001,17 @@ export class ReleasesService {
984
1001
  const interrupted = (await this.query()
985
1002
  .selectFrom('relDeployments')
986
1003
  .selectAll()
987
- .where('status', 'in', ['queued', 'deploying'])
1004
+ .where('status', 'in', ['queued', 'deploying', 'cancelling'])
1005
+ .orderBy('createdAt', 'asc')
1006
+ .orderBy('id', 'asc')
988
1007
  .execute()).map(decodeDeployment);
989
1008
  // A deployment the target recorded (the App Host's operation log) is finalised from what it reports; every other
990
1009
  // one was cut short.
991
1010
  const resumed = new Set();
992
1011
  const recorded = new Map();
993
1012
  for (const deployment of interrupted) {
994
- if (deployment.status !== 'deploying')
1013
+ if (deployment.status !== 'deploying' &&
1014
+ deployment.status !== 'cancelling')
995
1015
  continue;
996
1016
  const app = await this.findApp(deployment.appId);
997
1017
  if (!app)
@@ -1016,7 +1036,7 @@ export class ReleasesService {
1016
1036
  .set({
1017
1037
  status: 'failed',
1018
1038
  phase: 'completed',
1019
- error: 'Deployment was interrupted by a restart.',
1039
+ error: 'Deployment was interrupted by a restart. Deploy the release again.',
1020
1040
  finishedAt: new Date(),
1021
1041
  })
1022
1042
  .where('id', 'in', cutShort.map((deployment) => deployment.id))
@@ -1549,7 +1569,6 @@ export class ReleasesService {
1549
1569
  }
1550
1570
  // --- Internals ----------------------------------------------------------------------------------------------
1551
1571
  async startDeployment(caller, app, release, options, config, idempotency) {
1552
- await this.requireNoPendingDeployment(app.id);
1553
1572
  const deployment = await this.buildDeploymentRecord(app, release, caller, {
1554
1573
  kind: options.kind,
1555
1574
  rollbackTargetDeploymentId: options.rollbackTargetDeploymentId ?? null,
@@ -1558,7 +1577,6 @@ export class ReleasesService {
1558
1577
  });
1559
1578
  try {
1560
1579
  await this.options.database.transaction(async (connection) => {
1561
- await this.requireNoPendingDeployment(app.id, connection);
1562
1580
  await connection.query
1563
1581
  .insertInto('relDeployments')
1564
1582
  .values(encodeDeployment(deployment))
@@ -1585,6 +1603,13 @@ export class ReleasesService {
1585
1603
  await rm(deployment.configPath, { force: true });
1586
1604
  throw error;
1587
1605
  }
1606
+ try {
1607
+ await this.replaceQueued(deployment);
1608
+ }
1609
+ catch (error) {
1610
+ await this.markFailed(deployment, error);
1611
+ throw error;
1612
+ }
1588
1613
  this.schedule(deployment);
1589
1614
  return {
1590
1615
  ...deploymentView(deployment, {
@@ -1674,6 +1699,7 @@ export class ReleasesService {
1674
1699
  }
1675
1700
  async removeApp(app, caller) {
1676
1701
  await this.withLock(`publish:${app.id}`, () => this.withLock(app.id, async () => {
1702
+ await this.waitForAppDeployments(app.id);
1677
1703
  const current = await this.findApp(app.id);
1678
1704
  if (!current)
1679
1705
  return;
@@ -1739,10 +1765,10 @@ export class ReleasesService {
1739
1765
  .execute(),
1740
1766
  this.query()
1741
1767
  .selectFrom('relDeployments')
1742
- .select('appId')
1743
- .distinct()
1768
+ .select(['id', 'appId', 'status', 'createdAt'])
1744
1769
  .where('appId', 'in', ids)
1745
- .where('status', 'in', ['queued', 'deploying'])
1770
+ .orderBy('createdAt', 'asc')
1771
+ .where('status', 'in', ['queued', 'deploying', 'cancelling'])
1746
1772
  .execute(),
1747
1773
  this.query()
1748
1774
  .selectFrom('relApps')
@@ -1754,6 +1780,13 @@ export class ReleasesService {
1754
1780
  ]);
1755
1781
  const released = new Set(releases.map((row) => String(row.appId)));
1756
1782
  const pendingApps = new Set(pending.map((row) => String(row.appId)));
1783
+ const pendingByApp = new Map();
1784
+ for (const row of pending) {
1785
+ const previous = pendingByApp.get(String(row.appId));
1786
+ if (!previous ||
1787
+ (previous.status === 'queued' && row.status !== 'queued'))
1788
+ pendingByApp.set(String(row.appId), row);
1789
+ }
1757
1790
  const versions = new Map(currentReleases.map((row) => [String(row.appId), String(row.version)]));
1758
1791
  const environments = new Map();
1759
1792
  const statuses = new Map();
@@ -1790,6 +1823,15 @@ export class ReleasesService {
1790
1823
  currentVersion: versions.get(app.id) ?? null,
1791
1824
  hasReleases: released.has(app.id),
1792
1825
  hasPendingDeployment: pendingApps.has(app.id),
1826
+ pendingDeployment: (() => {
1827
+ const row = pendingByApp.get(app.id);
1828
+ return row
1829
+ ? {
1830
+ status: row.status,
1831
+ queueAhead: this.progress.get(String(row.id))?.queueAhead ?? null,
1832
+ }
1833
+ : null;
1834
+ })(),
1793
1835
  };
1794
1836
  });
1795
1837
  }
@@ -2021,7 +2063,7 @@ export class ReleasesService {
2021
2063
  }
2022
2064
  schedule(deployment) {
2023
2065
  this.logDeployment(deployment, 'queued', 'Deployment queued');
2024
- const run = this.withLock(deployment.appId, () => this.runDeployment(deployment.id))
2066
+ const run = this.dispatchDeployment(deployment)
2025
2067
  .catch((error) => this.diagnostic.error(`Deployment ${deployment.id} failed unexpectedly`, error))
2026
2068
  .finally(() => {
2027
2069
  this.running.delete(run);
@@ -2031,30 +2073,65 @@ export class ReleasesService {
2031
2073
  this.running.add(run);
2032
2074
  this.runs.set(deployment.id, run);
2033
2075
  }
2034
- async runDeployment(deploymentId) {
2076
+ async dispatchDeployment(deployment) {
2077
+ const app = await this.requireApp(deployment.appId);
2078
+ const session = await this.session(app.environmentId);
2079
+ // Drivers without a native queue keep the previous per-App serialization.
2080
+ if (!session.progress)
2081
+ return this.withLock(app.id, () => this.runDeployment(deployment.id, deployment.appId));
2082
+ const lifecycle = this.locks.get(app.id);
2083
+ if (lifecycle)
2084
+ await lifecycle.catch(() => undefined);
2085
+ return this.runDeployment(deployment.id, deployment.appId);
2086
+ }
2087
+ /** Lifecycle operations keep the deployment result current before reading its configuration. New dispatches wait on their App lock. */
2088
+ async waitForAppDeployments(appId) {
2089
+ const runs = [...this.dispatch]
2090
+ .filter(([, value]) => value.appId === appId)
2091
+ .map(([id]) => this.runs.get(id))
2092
+ .filter((run) => Boolean(run));
2093
+ await Promise.allSettled(runs);
2094
+ }
2095
+ async runDeployment(deploymentId, appId) {
2096
+ const dispatch = { appId, submitted: false, cancelled: false };
2097
+ this.dispatch.set(deploymentId, dispatch);
2035
2098
  const deployment = await this.requireDeploymentById(deploymentId);
2099
+ if (deployment.status !== 'queued') {
2100
+ this.dispatch.delete(deploymentId);
2101
+ return;
2102
+ }
2036
2103
  const app = await this.requireApp(deployment.appId);
2037
2104
  const release = await this.requireRelease(app.id, deployment.releaseId);
2038
- const previous = await this.currentDeployment(app);
2105
+ let settlement;
2039
2106
  let phaseWrites = Promise.resolve();
2040
2107
  let lastSequence = 0;
2108
+ let lastPhase;
2109
+ let switchStarted = false;
2041
2110
  let phaseError;
2042
- await this.updateDeployment(deploymentId, {
2043
- status: 'deploying',
2044
- phase: 'resolving',
2045
- startedAt: new Date(),
2046
- error: null,
2047
- previousDeploymentId: app.currentDeploymentId,
2048
- });
2049
2111
  let outcome;
2050
2112
  try {
2051
- this.logDeployment(deployment, 'resolving', 'Deployment started');
2052
2113
  const session = await this.session(app.environmentId);
2053
2114
  const logsEnabled = this.options.logging?.deployments?.enabled !== false;
2054
2115
  const spec = await this.deploymentSpec({ ...app, enabled: true }, deployment, 'running');
2055
2116
  // A driver that runs no images runs the archive; one that does says what it ran (`DeploymentEvent.artifact`).
2056
2117
  if (spec.images === undefined)
2057
2118
  await this.updateDeployment(deploymentId, artifactColumns({ kind: 'tarball' }));
2119
+ if (dispatch.cancelled) {
2120
+ await this.markCancelled(deployment);
2121
+ return;
2122
+ }
2123
+ if (!session.progress) {
2124
+ this.logDeployment(deployment, 'resolving', 'Deployment started');
2125
+ await this.updateDeployment(deploymentId, {
2126
+ status: 'deploying',
2127
+ phase: 'resolving',
2128
+ startedAt: new Date(),
2129
+ });
2130
+ }
2131
+ dispatch.submitted = true;
2132
+ // Reserve in runtime submission order without holding up Host admission or cancellation.
2133
+ settlement = this.reserveSettlement(app.id);
2134
+ let started = false;
2058
2135
  const observed = await session.apply(spec, (entry) => {
2059
2136
  if (typeof entry.sequence === 'number') {
2060
2137
  if (entry.sequence <= lastSequence)
@@ -2071,8 +2148,43 @@ export class ReleasesService {
2071
2148
  });
2072
2149
  if (typeof entry.phase === 'string') {
2073
2150
  const phase = entry.phase;
2151
+ const first = !started &&
2152
+ phase !== 'queued' &&
2153
+ phase !== 'cancelled' &&
2154
+ phase !== 'cancelling';
2155
+ if (first)
2156
+ started = true;
2157
+ if (['switching', 'starting', 'health_check'].includes(phase))
2158
+ switchStarted = true;
2159
+ this.progress.set(deploymentId, {
2160
+ queueAhead: phase === 'queued' && typeof entry.queueAhead === 'number'
2161
+ ? entry.queueAhead
2162
+ : null,
2163
+ cancellable: !switchStarted &&
2164
+ !['completed', 'cancelled', 'cancelling'].includes(phase),
2165
+ });
2166
+ const changed = lastPhase !== phase;
2167
+ lastPhase = phase;
2074
2168
  phaseWrites = phaseWrites
2075
- .then(() => this.updateDeployment(deploymentId, { phase }))
2169
+ .then(async () => {
2170
+ // Queue-position events are transient; they cause no database writes.
2171
+ if (phase === 'queued' || !changed)
2172
+ return;
2173
+ await this.query()
2174
+ .updateTable('relDeployments')
2175
+ .set({
2176
+ phase,
2177
+ ...(first
2178
+ ? {
2179
+ status: 'deploying',
2180
+ startedAt: new Date(),
2181
+ }
2182
+ : {}),
2183
+ })
2184
+ .where('id', '=', deploymentId)
2185
+ .where('status', 'in', ['queued', 'deploying'])
2186
+ .execute();
2187
+ })
2076
2188
  .catch((error) => {
2077
2189
  phaseError =
2078
2190
  error instanceof Error ? error : new Error(String(error));
@@ -2091,24 +2203,64 @@ export class ReleasesService {
2091
2203
  await phaseWrites;
2092
2204
  if (phaseError)
2093
2205
  throw phaseError;
2206
+ if (observed.state === 'cancelled') {
2207
+ await this.markCancelled(deployment);
2208
+ return;
2209
+ }
2094
2210
  if (observed.state === 'failed' || observed.state === 'unknown')
2095
2211
  throw new Error(observed.error ?? 'The runtime did not report the deployment.');
2096
- await this.markSucceeded(deployment, app.id, previous);
2097
2212
  outcome = { ok: true };
2098
2213
  }
2099
2214
  catch (error) {
2100
2215
  await phaseWrites.catch(() => undefined);
2101
2216
  outcome = { ok: false, error: errorMessage(error) };
2102
- await this.markFailed(deployment, error);
2103
2217
  }
2104
2218
  finally {
2105
- const policy = this.options.logging?.deployments;
2106
- await pruneJournals(path.dirname(this.deploymentLogPath(app.id, deployment.id)), {
2107
- retentionDays: policy?.retentionDays ?? 30,
2108
- maxSizeMB: policy?.maxTotalSizeMB ?? 1024,
2109
- }, `${deployment.id}.log`).catch((error) => reportLoggingFailure('Deployment log cleanup failed', error));
2219
+ const settle = settlement ?? this.reserveSettlement(app.id);
2220
+ const result = settle(async () => {
2221
+ if (!outcome)
2222
+ return;
2223
+ // The predecessor is read only after its writes, cleanup and outcome event have finished.
2224
+ const previous = await this.currentDeployment(await this.requireApp(app.id));
2225
+ if (outcome.ok) {
2226
+ try {
2227
+ await this.markSucceeded(deployment, app.id, previous);
2228
+ }
2229
+ catch (error) {
2230
+ outcome = { ok: false, error: errorMessage(error) };
2231
+ }
2232
+ }
2233
+ if (!outcome.ok)
2234
+ await this.markFailed(deployment, new Error(outcome.error), previous);
2235
+ await this.emitOutcome(deployment, app, release, outcome);
2236
+ });
2237
+ try {
2238
+ // A queued cancellation is already terminal; it must not wait for the running predecessor.
2239
+ if (outcome)
2240
+ await result;
2241
+ }
2242
+ finally {
2243
+ this.dispatch.delete(deploymentId);
2244
+ this.progress.delete(deploymentId);
2245
+ const policy = this.options.logging?.deployments;
2246
+ await pruneJournals(path.dirname(this.deploymentLogPath(app.id, deployment.id)), {
2247
+ retentionDays: policy?.retentionDays ?? 30,
2248
+ maxSizeMB: policy?.maxTotalSizeMB ?? 1024,
2249
+ }, `${deployment.id}.log`).catch((error) => reportLoggingFailure('Deployment log cleanup failed', error));
2250
+ }
2110
2251
  }
2111
- await this.emitOutcome(deployment, app, release, outcome);
2252
+ }
2253
+ /** Reuse the local lock chain to order results while the Host continues executing its queue. */
2254
+ reserveSettlement(appId) {
2255
+ let finish;
2256
+ const ready = new Promise((resolve) => {
2257
+ finish = resolve;
2258
+ });
2259
+ const result = this.withLock(`deployment-result:${appId}`, async () => (await ready)());
2260
+ return (work) => {
2261
+ finish(work);
2262
+ return result;
2263
+ };
2112
2264
  }
2113
2265
  async markSucceeded(deployment, appId, previous) {
2114
2266
  this.logDeployment(deployment, 'completed', 'Deployment succeeded');
@@ -2116,7 +2268,12 @@ export class ReleasesService {
2116
2268
  await this.options.database.transaction(async (connection) => {
2117
2269
  await connection.query
2118
2270
  .updateTable('relDeployments')
2119
- .set({ status: 'succeeded', phase: 'completed', finishedAt })
2271
+ .set({
2272
+ status: 'succeeded',
2273
+ phase: 'completed',
2274
+ finishedAt,
2275
+ previousDeploymentId: previous?.id ?? null,
2276
+ })
2120
2277
  .where('id', '=', deployment.id)
2121
2278
  .execute();
2122
2279
  await connection.query
@@ -2133,7 +2290,7 @@ export class ReleasesService {
2133
2290
  if (previous?.configPath && previous.id !== deployment.id)
2134
2291
  await rm(previous.configPath, { force: true }).catch(() => undefined);
2135
2292
  }
2136
- async markFailed(deployment, error) {
2293
+ async markFailed(deployment, error, previous) {
2137
2294
  this.logDeployment(deployment, 'completed', 'Deployment failed', error);
2138
2295
  const finishedAt = new Date();
2139
2296
  await this.options.database.transaction(async (connection) => {
@@ -2144,6 +2301,9 @@ export class ReleasesService {
2144
2301
  phase: 'completed',
2145
2302
  error: errorMessage(error),
2146
2303
  finishedAt,
2304
+ ...(previous !== undefined
2305
+ ? { previousDeploymentId: previous?.id ?? null }
2306
+ : {}),
2147
2307
  })
2148
2308
  .where('id', '=', deployment.id)
2149
2309
  .execute();
@@ -2211,15 +2371,17 @@ export class ReleasesService {
2211
2371
  };
2212
2372
  }
2213
2373
  const current = await this.requireDeploymentById(deployment.id);
2214
- if (current.status !== 'deploying' && current.status !== 'queued')
2374
+ if (!['deploying', 'queued', 'cancelling'].includes(current.status))
2215
2375
  return;
2216
2376
  const app = await this.requireApp(deployment.appId);
2217
2377
  const release = await this.requireRelease(app.id, deployment.releaseId);
2218
2378
  let outcome;
2379
+ if (status?.state === 'cancelled') {
2380
+ await this.markCancelled(deployment);
2381
+ return;
2382
+ }
2219
2383
  if (status?.state === 'running') {
2220
- const previous = deployment.previousDeploymentId
2221
- ? await this.requireDeployment(app.id, deployment.previousDeploymentId).catch(() => null)
2222
- : null;
2384
+ const previous = await this.currentDeployment(app);
2223
2385
  this.logDeployment(deployment, 'switching', 'The runtime reports the deployment finished');
2224
2386
  await this.markSucceeded(deployment, app.id, previous);
2225
2387
  outcome = { ok: true };
@@ -2227,7 +2389,7 @@ export class ReleasesService {
2227
2389
  else {
2228
2390
  const error = status
2229
2391
  ? (status.error ?? 'The deployment failed.')
2230
- : 'Deployment was interrupted by a restart.';
2392
+ : 'Deployment was interrupted by a restart. Deploy the release again.';
2231
2393
  await this.markFailed(deployment, new Error(error));
2232
2394
  outcome = { ok: false, error };
2233
2395
  }
@@ -2243,15 +2405,120 @@ export class ReleasesService {
2243
2405
  this.runs.set(deployment.id, run);
2244
2406
  return run;
2245
2407
  }
2246
- async requireNoPendingDeployment(appId, connection) {
2247
- const pending = await (connection?.query ?? this.query())
2408
+ progressView(view) {
2409
+ const progress = this.progress.get(view.id);
2410
+ return {
2411
+ ...view,
2412
+ queueAhead: progress?.queueAhead ?? null,
2413
+ cancellable: ['queued', 'deploying'].includes(view.status) &&
2414
+ (progress?.cancellable ?? view.phase === 'queued'),
2415
+ };
2416
+ }
2417
+ async cancelDeployment(caller, appId, deploymentId) {
2418
+ const app = await this.requireApp(appId);
2419
+ await this.options.guard.requireApp(caller, 'deploy', app);
2420
+ await this.withLock(`cancel:${deploymentId}`, async () => {
2421
+ const deployment = await this.requireDeployment(appId, deploymentId);
2422
+ if (deployment.status === 'cancelled' ||
2423
+ deployment.status === 'cancelling')
2424
+ return;
2425
+ if (!['queued', 'deploying'].includes(deployment.status))
2426
+ throw new ReleasesError('The deployment has finished.', 'DEPLOYMENT_NOT_CANCELLABLE', 'FAILED_PRECONDITION');
2427
+ const result = await this.requestCancellation(deployment, false);
2428
+ if (result === 'tooLate' || result === 'notFound')
2429
+ throw new ReleasesError('The deployment has started switching or has finished.', 'DEPLOYMENT_NOT_CANCELLABLE', 'FAILED_PRECONDITION');
2430
+ });
2431
+ return this.getDeployment(caller, appId, deploymentId);
2432
+ }
2433
+ async replaceQueued(replacement) {
2434
+ const rows = await this.query()
2248
2435
  .selectFrom('relDeployments')
2249
- .select('id')
2250
- .where('appId', '=', appId)
2251
- .where('status', 'in', ['queued', 'deploying'])
2252
- .executeTakeFirst();
2253
- if (pending)
2254
- throw new ReleasesError('A deployment is already in progress.', 'DEPLOYMENT_IN_PROGRESS', 'FAILED_PRECONDITION');
2436
+ .selectAll()
2437
+ .where('appId', '=', replacement.appId)
2438
+ .where('status', '=', 'queued')
2439
+ .where('id', '!=', replacement.id)
2440
+ .execute();
2441
+ for (const row of rows)
2442
+ await this.requestCancellation(decodeDeployment(row), true, replacement.id);
2443
+ }
2444
+ async requestCancellation(deployment, queuedOnly, replacedBy) {
2445
+ const dispatch = this.dispatch.get(deployment.id);
2446
+ let result;
2447
+ if ((dispatch && !dispatch.submitted) ||
2448
+ (!dispatch && deployment.status === 'queued')) {
2449
+ if (dispatch)
2450
+ dispatch.cancelled = true;
2451
+ result = 'cancelled';
2452
+ }
2453
+ else {
2454
+ const app = await this.requireApp(deployment.appId);
2455
+ const session = await this.session(app.environmentId);
2456
+ if (!session.cancel)
2457
+ throw new ReleasesError('The deployment driver does not support cancellation.', 'CANCELLATION_UNSUPPORTED', 'FAILED_PRECONDITION');
2458
+ result = await session.cancel(deployment.id, app.id, queuedOnly);
2459
+ }
2460
+ if (result === 'cancelled')
2461
+ await this.markCancelled(deployment, replacedBy);
2462
+ else if (result === 'cancelling') {
2463
+ this.logDeployment(deployment, 'cancelling', 'Cancellation requested; waiting for the current step to finish');
2464
+ await this.query()
2465
+ .updateTable('relDeployments')
2466
+ .set({ status: 'cancelling' })
2467
+ .where('id', '=', deployment.id)
2468
+ .where('status', 'in', ['queued', 'deploying'])
2469
+ .execute();
2470
+ }
2471
+ return result;
2472
+ }
2473
+ markCancelled(deployment, replacedBy) {
2474
+ return this.withLock(`settle:${deployment.id}`, () => this.markCancelledUnlocked(deployment, replacedBy));
2475
+ }
2476
+ async markCancelledUnlocked(deployment, replacedBy) {
2477
+ const current = await this.requireDeploymentById(deployment.id);
2478
+ if (!['queued', 'deploying', 'cancelling'].includes(current.status))
2479
+ return;
2480
+ const reason = replacedBy
2481
+ ? 'Replaced by a newer deployment.'
2482
+ : 'Deployment cancelled.';
2483
+ const finishedAt = new Date();
2484
+ if (deployment.configPath)
2485
+ await rm(deployment.configPath, { force: true });
2486
+ await this.options.database.transaction(async (connection) => {
2487
+ await connection.query
2488
+ .updateTable('relDeployments')
2489
+ .set({
2490
+ status: 'cancelled',
2491
+ phase: 'cancelled',
2492
+ error: reason,
2493
+ finishedAt,
2494
+ })
2495
+ .where('id', '=', deployment.id)
2496
+ .where('status', 'in', ['queued', 'deploying', 'cancelling'])
2497
+ .execute();
2498
+ await this.settleRequest(connection, deployment, 'cancelled', finishedAt);
2499
+ });
2500
+ if (this.options.logging?.deployments?.enabled !== false)
2501
+ this.appendDeploymentLog(deployment, {
2502
+ time: finishedAt.toISOString(),
2503
+ level: 'info',
2504
+ appId: deployment.appId,
2505
+ deploymentId: deployment.id,
2506
+ phase: 'cancelled',
2507
+ msg: reason,
2508
+ reason: replacedBy ? 'superseded' : 'requested',
2509
+ ...(replacedBy ? { replacedBy } : {}),
2510
+ });
2511
+ const app = await this.requireApp(deployment.appId);
2512
+ const release = await this.requireRelease(app.id, deployment.releaseId);
2513
+ await this.options.events
2514
+ .emit({
2515
+ type: 'deployment.cancelled',
2516
+ app: appView(app),
2517
+ release: releaseView(release),
2518
+ deployment: deploymentView(await this.requireDeploymentById(deployment.id), { version: release.version, checksum: release.checksum }),
2519
+ actor: { userId: deployment.actorId, kind: deployment.actorKind },
2520
+ })
2521
+ .catch((error) => this.diagnostic.error('Failed to announce deployment cancellation', error));
2255
2522
  }
2256
2523
  async findRelease(appId, releaseId) {
2257
2524
  const row = await this.query()
@@ -2628,7 +2895,7 @@ function runtimeStatus(status, appId) {
2628
2895
  };
2629
2896
  return {
2630
2897
  available: true,
2631
- state: item.state,
2898
+ state: item.state === 'cancelled' ? 'failed' : item.state,
2632
2899
  version: item.version,
2633
2900
  startedAt: item.startedAt,
2634
2901
  error: item.error,