Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { shallowRef, watch } from 'vue'
3 :
4 : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
5 : import { reportError } from '@comfyorg/telemetry/reportError'
6 : import { workspaceApi } from '@/platform/workspace/api/workspaceApi'
7 : import type {
8 : DeploymentStartRefusal,
9 : DeploymentStartEvent,
10 : DeploymentStarts
11 : } from '@/platform/workspace/deploymentStartState'
12 : import { reduceDeploymentStarts } from '@/platform/workspace/deploymentStartState'
13 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
14 : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
15 :
16 2355 : const REFUSALS_BY_CODE = new Map<string, DeploymentStartRefusal>([
17 : ['PAYMENT_REQUIRED', 'noCredits'],
18 : ['CONCURRENCY_LIMIT', 'tooManyRunning']
19 : ])
20 :
21 2355 : const UNAVAILABLE_STATUSES = new Set([403, 404, 405, 501])
22 :
23 0 : function reasonFor(err: WorkspaceApiError): DeploymentStartRefusal {
24 0 : const byCode =
25 : err.code === undefined ? undefined : REFUSALS_BY_CODE.get(err.code)
26 0 : if (byCode) return byCode
27 0 : if (err.status !== undefined && UNAVAILABLE_STATUSES.has(err.status)) {
28 0 : return 'unavailable'
29 : }
30 0 : return 'failed'
31 : }
32 :
33 0 : function refusalOf(err: unknown): {
34 : reason: DeploymentStartRefusal
35 : message: string | null
36 : } {
37 0 : if (!(err instanceof WorkspaceApiError)) {
38 0 : return { reason: 'failed', message: null }
39 : }
40 0 : return {
41 : reason: reasonFor(err),
42 : message: err.code === undefined ? null : err.message || null
43 : }
44 : }
45 :
46 0 : function isUnanswered(err: unknown): boolean {
47 0 : return (
48 : !(err instanceof WorkspaceApiError) ||
49 : err.status === undefined ||
50 : err.status >= 500
51 : )
52 : }
53 :
54 : /**
55 : * The Starts this page asked for: each lasts until the listing after it lands,
56 : * and a refusal lasts while the listing says the deployment can still start.
57 : */
58 2355 : export const useDeploymentStartStore = defineStore('deploymentStart', () => {
59 1975 : const pickStore = useDeploymentPickStore()
60 1975 : const workspaceStore = useTeamWorkspaceStore()
61 :
62 1975 : const starts = shallowRef<DeploymentStarts>(new Map())
63 :
64 9 : function dispatch(event: DeploymentStartEvent) {
65 9 : starts.value = reduceDeploymentStarts(starts.value, event)
66 : }
67 :
68 1975 : watch(
69 : () => pickStore.deployments,
70 : (deployments) => dispatch({ type: 'listed', deployments })
71 : )
72 :
73 0 : async function start(deploymentId: string): Promise<void> {
74 0 : const workspaceId = workspaceStore.workspaceId
75 0 : if (!workspaceId) return
76 0 : if (starts.value.get(deploymentId)?.phase === 'requested') return
77 0 : dispatch({ type: 'requested', deploymentId })
78 0 : try {
79 0 : await workspaceApi.startDeployment(workspaceId, deploymentId)
80 : } catch (err) {
81 0 : if (isUnanswered(err)) {
82 0 : reportError(err, {
83 : surface: 'workspace',
84 : errorType: 'error_starting_deployment'
85 : })
86 : }
87 0 : dispatch({ type: 'refused', deploymentId, ...refusalOf(err) })
88 0 : return
89 : }
90 0 : pickStore.followStart(deploymentId)
91 0 : await pickStore.load()
92 0 : dispatch({ type: 'taken', deploymentId })
93 : }
94 :
95 1975 : return { starts, start }
96 : })
|