1- import { baiSignedRequestWithPromise , compareNumberWithUnits } from '../helper' ;
1+ import {
2+ SSEEventHandlerTypes ,
3+ baiSignedRequestWithPromise ,
4+ compareNumberWithUnits ,
5+ listenToBackgroundTask ,
6+ } from '../helper' ;
27import { useCurrentDomainValue , useSuspendedBackendaiClient } from '../hooks' ;
38import { useTanMutation } from '../hooks/reactQueryAlias' ;
49import Flex from './Flex' ;
@@ -19,6 +24,12 @@ import { useTranslation } from 'react-i18next';
1924interface ServiceValidationModalProps {
2025 serviceData : any ;
2126}
27+ type BackgroundTaskEvent = {
28+ task_id : string ;
29+ message : { event : string ; session_id : string } ;
30+ current_progress : number ;
31+ total_progress : number ;
32+ } ;
2233
2334const ServiceValidationView : React . FC < ServiceValidationModalProps > = ( {
2435 serviceData,
@@ -126,58 +137,53 @@ const ServiceValidationView: React.FC<ServiceValidationModalProps> = ({
126137
127138 const validationDateTime = dayjs ( ) . format ( 'LLL' ) ;
128139
129- let sse : EventSource | undefined = undefined ;
130-
131140 mutationsToValidateService
132141 . mutateAsync ( serviceData )
133- . then ( ( data : any ) => {
142+ . then ( ( response : any ) => {
134143 setValidationTime ( validationDateTime ) ;
135- // setContainerLogSummary('loading...');
136- const response = data ;
137- sse = baiClient . maintenance . attach_background_task ( response [ 'task_id' ] ) ;
138144
139- // TODO:
140145 const timeoutId = setTimeout ( ( ) => {
141- sse ?. close ( ) ;
142- // something went wrong during validation
143146 setValidationStatus ( 'error' ) ;
144147 message . error ( t ( 'modelService.CannotValidateNow' ) ) ;
145148 } , 10000 ) ;
146149
147- sse ?. addEventListener ( 'bgtask_updated' , async ( e ) => {
148- const data = JSON . parse ( e [ 'data' ] ) ;
149- const msg = JSON . parse ( data . message ) ;
150- if ( [ 'session_started' , 'session_terminated' ] . includes ( msg . event ) ) {
151- const logs = await getLogs ( msg . session_id ) ;
152- setContainerLogSummary ( logs ) ;
153- clearTimeout ( timeoutId ) ;
154- // temporally close sse manually when session is terminated
155- if ( msg . event === 'session_terminated' ) {
156- sse ?. close ( ) ;
157- return ;
150+ const SSEEventHandlers : SSEEventHandlerTypes < BackgroundTaskEvent > = {
151+ onUpdated : async ( data , controller ) => {
152+ const msg = data . message ;
153+ if ( validationStatus === 'error' ) {
154+ clearTimeout ( timeoutId ) ;
155+ controller ?. abort ( ) ;
156+ } else if (
157+ [ 'session_started' , 'session_terminated' ] . includes ( msg . event )
158+ ) {
159+ const logs = await getLogs ( msg . session_id ) ;
160+ setContainerLogSummary ( logs ) ;
161+ clearTimeout ( timeoutId ) ;
162+ controller ?. abort ( ) ;
158163 }
159- }
160- setValidationStatus ( 'processing' ) ;
161- } ) ;
162- sse ?. addEventListener ( 'bgtask_done' , async ( e ) => {
163- setValidationStatus ( 'finished' ) ;
164- clearTimeout ( timeoutId ) ;
165- sse ?. close ( ) ;
166- } ) ;
167- sse ?. addEventListener ( 'bgtask_failed' , async ( e ) => {
168- const data = JSON . parse ( e [ 'data' ] ) ;
169- const msg = JSON . parse ( data . message ) ;
170- const logs = await getLogs ( msg . session_id ) ;
171- setContainerLogSummary ( logs ) ;
172- setValidationStatus ( 'error' ) ;
173- sse ?. close ( ) ;
174- throw new Error ( e [ 'data' ] ) ;
175- } ) ;
176- sse ?. addEventListener ( 'bgtask_cancelled' , async ( e ) => {
177- setValidationStatus ( 'error' ) ;
178- sse ?. close ( ) ;
179- throw new Error ( e [ 'data' ] ) ;
180- } ) ;
164+ setValidationStatus ( 'processing' ) ;
165+ } ,
166+ onDone : ( data ) => {
167+ setValidationStatus ( 'finished' ) ;
168+ clearTimeout ( timeoutId ) ;
169+ } ,
170+ onFailed : async ( data ) => {
171+ const logs = await getLogs ( data . message . session_id ) ;
172+ setContainerLogSummary ( logs ) ;
173+ setValidationStatus ( 'error' ) ;
174+ throw new Error ( data . message . event ) ;
175+ } ,
176+ onTaskCancelled : ( data ) => {
177+ setValidationStatus ( 'error' ) ;
178+ throw new Error ( data . message . event ) ;
179+ } ,
180+ onTaskFailed : ( data ) => {
181+ setValidationStatus ( 'error' ) ;
182+ throw new Error ( data . message . event ) ;
183+ } ,
184+ } ;
185+
186+ listenToBackgroundTask ( response [ 'task_id' ] , SSEEventHandlers ) ;
181187 } )
182188 . catch ( ( error ) => {
183189 message . error (
@@ -190,11 +196,6 @@ const ServiceValidationView: React.FC<ServiceValidationModalProps> = ({
190196 isRunningRef . current = false ;
191197 } ) ;
192198 isRunningRef . current = true ;
193-
194- return ( ) => {
195- sse ?. close ( ) ;
196- } ;
197-
198199 // eslint-disable-next-line react-hooks/exhaustive-deps
199200 } , [ ] ) ;
200201
0 commit comments