22
33import { UserFacingError , getErrorMessage , userError } from '../user-error' ;
44import {
5- DeployedProcessInfo ,
65 deployProcess as _deployProcess ,
76 getDeployments as fetchDeployments ,
87 getDeployment as fetchDeployment ,
98 getProcessImageFromMachine ,
109 removeDeploymentFromMachines ,
10+ changeDeploymentActivation as _changeDeploymentActivation ,
11+ DeployedProcessInfo ,
1112} from './deployment' ;
1213import { Engine , SpaceEngine } from './machines' ;
1314import { savedEnginesToEngines } from './saved-engines-helpers' ;
14- import { getCurrentEnvironment , getCurrentUser } from '@/components/auth' ;
15+ import { getCurrentEnvironment } from '@/components/auth' ;
1516import { enableUseDB } from 'FeatureFlags' ;
1617import { getDbEngines , getDbEngineByAddress } from '@/lib/data/db/engines' ;
1718import { asyncFilter , asyncMap , asyncForEach } from '../helpers/javascriptHelpers' ;
@@ -46,7 +47,6 @@ import {
4647import { getFileFromMachine , submitFileToMachine , updateVariablesOnMachine } from './instances' ;
4748import { getProcessIds , getVariablesFromElementById } from '@proceed/bpmn-helper' ;
4849import { Variable } from '@proceed/bpmn-helper/src/getters' ;
49- import { getUsersInSpace } from '../data/db/iam/memberships' ;
5050import Ability from '../ability/abilityHelper' ;
5151import { getUserById } from '../data/db/iam/users' ;
5252
@@ -107,7 +107,51 @@ export async function deployProcess(
107107
108108 if ( engines . length === 0 ) throw new UserFacingError ( 'No fitting engine found.' ) ;
109109
110- await _deployProcess ( definitionId , versionId , spaceId , method , engines ) ;
110+ const processAlreadyDeployedInfo = await asyncMap ( engines , async ( engine ) => {
111+ let deployment ;
112+ try {
113+ deployment = await fetchDeployment ( engine , definitionId ) ;
114+ // ignore not found errors on engines that don't have a deployment of the process
115+ } catch ( err ) {
116+ deployment = undefined ;
117+ }
118+ return [ engine , deployment ] as const ;
119+ } ) ;
120+
121+ function withDeployment (
122+ info : ( typeof processAlreadyDeployedInfo ) [ number ] ,
123+ ) : info is readonly [ Engine , DeployedProcessInfo ] {
124+ return ! ! info [ 1 ] ;
125+ }
126+ const enginesWithDeployment = processAlreadyDeployedInfo . filter ( withDeployment ) ;
127+
128+ // check if the version is already deployed to some engine since we don't
129+ // need to redeploy it in that case
130+ if (
131+ ! _forceEngine &&
132+ enginesWithDeployment . some ( ( [ _ , deployment ] ) =>
133+ deployment . versions . some ( ( version ) => version . versionId === versionId ) ,
134+ )
135+ ) {
136+ return ;
137+ }
138+
139+ if ( ! _forceEngine && enginesWithDeployment . length ) {
140+ // check if an engine already has another version in which case that engine is selected
141+ engines = enginesWithDeployment . map ( ( [ engine ] ) => engine ) ;
142+ }
143+
144+ const deployedTo = await _deployProcess ( definitionId , versionId , spaceId , method , engines ) ;
145+
146+ // deactivate the process on all engines that have a deployment but which were not target of the
147+ // new deployment
148+ await Promise . allSettled (
149+ enginesWithDeployment . map ( async ( [ engine ] ) => {
150+ if ( ! deployedTo . some ( ( dE ) => dE . id === engine . id ) ) {
151+ await _changeDeploymentActivation ( engine , definitionId , undefined , false ) ;
152+ }
153+ } ) ,
154+ ) ;
111155 } catch ( e ) {
112156 const message = getErrorMessage ( e ) ;
113157 return userError ( message ) ;
@@ -131,6 +175,33 @@ export async function removeDeployment(definitionId: string, spaceId: string) {
131175 }
132176}
133177
178+ export async function changeDeploymentActivation (
179+ definitionId : string ,
180+ spaceId : string ,
181+ version : string ,
182+ value : boolean ,
183+ ) {
184+ try {
185+ const engines = await getCorrectTargetEngines ( spaceId , false , async ( engine : Engine ) => {
186+ const deployments = await fetchDeployments ( [ engine ] ) ;
187+
188+ return deployments . some (
189+ ( deployment ) =>
190+ deployment . definitionId === definitionId &&
191+ deployment . versions . some ( ( v ) => v . versionId === version ) ,
192+ ) ;
193+ } ) ;
194+
195+ if ( ! engines . length )
196+ throw new Error ( 'There is no available engine with the requested process version.' ) ;
197+
198+ await _changeDeploymentActivation ( engines [ 0 ] , definitionId , version , value ) ;
199+ } catch ( e ) {
200+ const message = getErrorMessage ( e ) ;
201+ return userError ( message ) ;
202+ }
203+ }
204+
134205export async function getAvailableTaskListEntries ( spaceId : string , engines : Engine [ ] ) {
135206 try {
136207 if ( ! enableUseDB )
0 commit comments