1- import { useEnvironment , useSession } from '@/components/auth-can' ;
1+ import { useEnvironment } from '@/components/auth-can' ;
22import {
33 DeployedProcessInfo ,
44 InstanceInfo ,
55 VersionInfo ,
66 getDeployments ,
77} from '@/lib/engines/deployment' ;
8- import {
9- pauseInstanceOnMachine ,
10- resumeInstanceOnMachine ,
11- startInstanceOnMachine ,
12- stopInstanceOnMachine ,
13- } from '@/lib/engines/instances' ;
14- import { Engine } from '@/lib/engines/types' ;
15- import { getStartFormFromMachine } from '@/lib/engines/tasklist' ;
168import useEngines from '@/lib/engines/use-engines' ;
17- import { asyncFilter , asyncForEach , deepEquals } from '@/lib/helpers/javascriptHelpers' ;
18- import { getErrorMessage , userError } from '@/lib/user-error' ;
9+ import { deepEquals } from '@/lib/helpers/javascriptHelpers' ;
1910import { useQuery } from '@tanstack/react-query' ;
2011import { useCallback } from 'react' ;
2112
@@ -89,7 +80,6 @@ const mergeDeployment = (
8980
9081function useDeployment ( definitionId : string , initialData ?: DeployedProcessInfo ) {
9182 const space = useEnvironment ( ) ;
92- const { data : session } = useSession ( ) ;
9383
9484 const { data : engines } = useEngines ( space , {
9585 key : [ definitionId ] ,
@@ -99,90 +89,6 @@ function useDeployment(definitionId: string, initialData?: DeployedProcessInfo)
9989 } ,
10090 } ) ;
10191
102- const startInstance = async ( versionId : string , variables : { [ key : string ] : any } = { } ) => {
103- if ( ! engines ?. length ) return userError ( 'No fitting engine found' ) ;
104-
105- // TODO: in case of static deployment or different versions on different engines we will have
106- // to check if the engine can actually be used to start an instance
107- return await startInstanceOnMachine ( definitionId , versionId , engines [ 0 ] , variables , {
108- processInitiator : session ?. user . id ,
109- spaceIdOfProcessInitiator : space . spaceId ,
110- } ) ;
111- } ;
112-
113- const activeStates = [ 'PAUSED' , 'RUNNING' , 'READY' , 'DEPLOYMENT-WAITING' , 'WAITING' ] ;
114- async function changeInstanceState (
115- instanceId : string ,
116- stateValidator : ( state : InstanceInfo [ 'instanceState' ] ) => boolean ,
117- stateChangeFunction : typeof resumeInstanceOnMachine ,
118- ) {
119- if ( ! engines ) return ;
120- try {
121- const targetEngines = await asyncFilter ( engines , async ( engine : Engine ) => {
122- const deployments = await getDeployments ( [ engine ] ) ;
123-
124- return deployments . some ( ( deployment ) => {
125- if ( deployment . definitionId !== definitionId ) return false ;
126-
127- const instance = deployment . instances . find (
128- ( instance ) => instance . processInstanceId === instanceId ,
129- ) ;
130- if ( ! instance ) return false ;
131-
132- return stateValidator ( instance . instanceState ) ;
133- } ) ;
134- } ) ;
135-
136- await asyncForEach ( targetEngines , async ( engine ) => {
137- await stateChangeFunction ( definitionId , instanceId , engine ) ;
138- } ) ;
139- } catch ( e ) {
140- const message = getErrorMessage ( e ) ;
141- return userError ( message ) ;
142- }
143- }
144-
145- async function resumeInstance ( instanceId : string ) {
146- // TODO: manage permissions for starting an instance
147- return await changeInstanceState (
148- instanceId ,
149- ( tokenStates ) => tokenStates . some ( ( tokenState ) => tokenState === 'PAUSED' ) ,
150- resumeInstanceOnMachine ,
151- ) ;
152- }
153-
154- async function pauseInstance ( instanceId : string ) {
155- // TODO: manage permissions for starting an instance
156- return await changeInstanceState (
157- instanceId ,
158- ( tokenStates ) =>
159- tokenStates . some ( ( state ) => activeStates . includes ( state ) && state !== 'PAUSED' ) ,
160- pauseInstanceOnMachine ,
161- ) ;
162- }
163-
164- async function stopInstance ( instanceId : string ) {
165- // TODO: manage permissions for starting an instance
166- return await changeInstanceState (
167- instanceId ,
168- ( tokenStates ) => tokenStates . some ( ( state ) => activeStates . includes ( state ) ) ,
169- stopInstanceOnMachine ,
170- ) ;
171- }
172-
173- async function getStartForm ( versionId : string ) {
174- if ( ! engines ?. length ) return userError ( 'No fitting engine found' ) ;
175-
176- try {
177- // TODO: in case of static deployment or different versions on different engines we will have
178- // to check if the engine can actually be used to start an instance
179- return await getStartFormFromMachine ( definitionId , versionId , engines [ 0 ] ) ;
180- } catch ( e ) {
181- const message = getErrorMessage ( e ) ;
182- return userError ( message ) ;
183- }
184- }
185-
18692 const queryFn = useCallback ( async ( ) => {
18793 if ( engines ?. length ) {
18894 // TODO: this only handles situations where we have only a single engine
@@ -212,7 +118,7 @@ function useDeployment(definitionId: string, initialData?: DeployedProcessInfo)
212118 } ,
213119 } ) ;
214120
215- return { ...query , startInstance , resumeInstance , pauseInstance , stopInstance , getStartForm } ;
121+ return { ...query } ;
216122}
217123
218124export default useDeployment ;
0 commit comments