1- import { Component , Input , OnDestroy } from '@angular/core' ;
1+ import { Component , Input , OnDestroy , OnInit } from '@angular/core' ;
22import { ScriptDataService } from '../../../../core/data/processes/script-data.service' ;
33import { ContentSource } from '../../../../core/shared/content-source.model' ;
44import { ProcessDataService } from '../../../../core/data/processes/process-data.service' ;
55import {
6- getAllCompletedRemoteData ,
76 getAllSucceededRemoteDataPayload ,
87 getFirstCompletedRemoteData ,
98 getFirstSucceededRemoteDataPayload
109} from '../../../../core/shared/operators' ;
1110import { filter , map , switchMap , tap } from 'rxjs/operators' ;
12- import { hasValue , hasValueOperator } from '../../../../shared/empty.util' ;
11+ import { hasValue } from '../../../../shared/empty.util' ;
1312import { ProcessStatus } from '../../../../process-page/processes/process-status.model' ;
1413import { BehaviorSubject , Observable , Subscription } from 'rxjs' ;
1514import { RequestService } from '../../../../core/data/request.service' ;
@@ -30,7 +29,7 @@ import { ContentSourceSetSerializer } from '../../../../core/shared/content-sour
3029 styleUrls : [ './collection-source-controls.component.scss' ] ,
3130 templateUrl : './collection-source-controls.component.html' ,
3231} )
33- export class CollectionSourceControlsComponent implements OnDestroy {
32+ export class CollectionSourceControlsComponent implements OnInit , OnDestroy {
3433
3534 /**
3635 * Should the controls be enabled.
@@ -49,6 +48,7 @@ export class CollectionSourceControlsComponent implements OnDestroy {
4948
5049 contentSource$ : Observable < ContentSource > ;
5150 private subs : Subscription [ ] = [ ] ;
51+ private autoRefreshIDs : string [ ] = [ ] ;
5252
5353 testConfigRunning$ = new BehaviorSubject ( false ) ;
5454 importRunning$ = new BehaviorSubject ( false ) ;
@@ -95,36 +95,28 @@ export class CollectionSourceControlsComponent implements OnDestroy {
9595 } ) ,
9696 // filter out responses that aren't successful since the pinging of the process only needs to happen when the invocation was successful.
9797 filter ( ( rd ) => rd . hasSucceeded && hasValue ( rd . payload ) ) ,
98- switchMap ( ( rd ) => this . processDataService . findById ( rd . payload . processId , false ) ) ,
99- getAllCompletedRemoteData ( ) ,
100- filter ( ( rd ) => ! rd . isStale && ( rd . hasSucceeded || rd . hasFailed ) ) ,
101- map ( ( rd ) => rd . payload ) ,
102- hasValueOperator ( ) ,
98+ switchMap ( ( rd ) => {
99+ this . autoRefreshIDs . push ( rd . payload . processId ) ;
100+ return this . processDataService . autoRefreshUntilCompletion ( rd . payload . processId ) ;
101+ } ) ,
102+ map ( ( rd ) => rd . payload )
103103 ) . subscribe ( ( process : Process ) => {
104- if ( process . processStatus . toString ( ) !== ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) &&
105- process . processStatus . toString ( ) !== ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
106- // Ping the current process state every 5s
107- setTimeout ( ( ) => {
108- this . requestService . setStaleByHrefSubstring ( process . _links . self . href ) ;
109- } , 5000 ) ;
110- }
111- if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
112- this . notificationsService . error ( this . translateService . get ( 'collection.source.controls.test.failed' ) ) ;
113- this . testConfigRunning$ . next ( false ) ;
114- }
115- if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) ) {
116- this . bitstreamService . findByHref ( process . _links . output . href ) . pipe ( getFirstSucceededRemoteDataPayload ( ) ) . subscribe ( ( bitstream ) => {
117- this . httpClient . get ( bitstream . _links . content . href , { responseType : 'text' } ) . subscribe ( ( data : any ) => {
118- const output = data . replaceAll ( new RegExp ( '.*\\@(.*)' , 'g' ) , '$1' )
119- . replaceAll ( 'The script has started' , '' )
120- . replaceAll ( 'The script has completed' , '' ) ;
121- this . notificationsService . info ( this . translateService . get ( 'collection.source.controls.test.completed' ) , output ) ;
122- } ) ;
104+ if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
105+ this . notificationsService . error ( this . translateService . get ( 'collection.source.controls.test.failed' ) ) ;
106+ this . testConfigRunning$ . next ( false ) ;
107+ }
108+ if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) ) {
109+ this . bitstreamService . findByHref ( process . _links . output . href ) . pipe ( getFirstSucceededRemoteDataPayload ( ) ) . subscribe ( ( bitstream ) => {
110+ this . httpClient . get ( bitstream . _links . content . href , { responseType : 'text' } ) . subscribe ( ( data : any ) => {
111+ const output = data . replaceAll ( new RegExp ( '.*\\@(.*)' , 'g' ) , '$1' )
112+ . replaceAll ( 'The script has started' , '' )
113+ . replaceAll ( 'The script has completed' , '' ) ;
114+ this . notificationsService . info ( this . translateService . get ( 'collection.source.controls.test.completed' ) , output ) ;
123115 } ) ;
124- this . testConfigRunning$ . next ( false ) ;
125- }
116+ } ) ;
117+ this . testConfigRunning$ . next ( false ) ;
126118 }
127- ) ) ;
119+ } ) ) ;
128120 }
129121
130122 /**
@@ -147,31 +139,22 @@ export class CollectionSourceControlsComponent implements OnDestroy {
147139 }
148140 } ) ,
149141 filter ( ( rd ) => rd . hasSucceeded && hasValue ( rd . payload ) ) ,
150- switchMap ( ( rd ) => this . processDataService . findById ( rd . payload . processId , false ) ) ,
151- getAllCompletedRemoteData ( ) ,
152- filter ( ( rd ) => ! rd . isStale && ( rd . hasSucceeded || rd . hasFailed ) ) ,
153- map ( ( rd ) => rd . payload ) ,
154- hasValueOperator ( ) ,
142+ switchMap ( ( rd ) => {
143+ this . autoRefreshIDs . push ( rd . payload . processId ) ;
144+ return this . processDataService . autoRefreshUntilCompletion ( rd . payload . processId ) ;
145+ } ) ,
146+ map ( ( rd ) => rd . payload )
155147 ) . subscribe ( ( process ) => {
156- if ( process . processStatus . toString ( ) !== ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) &&
157- process . processStatus . toString ( ) !== ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
158- // Ping the current process state every 5s
159- setTimeout ( ( ) => {
160- this . requestService . setStaleByHrefSubstring ( process . _links . self . href ) ;
161- this . requestService . setStaleByHrefSubstring ( this . collection . _links . self . href ) ;
162- } , 5000 ) ;
163- }
164- if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
165- this . notificationsService . error ( this . translateService . get ( 'collection.source.controls.import.failed' ) ) ;
166- this . importRunning$ . next ( false ) ;
167- }
168- if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) ) {
169- this . notificationsService . success ( this . translateService . get ( 'collection.source.controls.import.completed' ) ) ;
170- this . requestService . setStaleByHrefSubstring ( this . collection . _links . self . href ) ;
171- this . importRunning$ . next ( false ) ;
172- }
148+ if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
149+ this . notificationsService . error ( this . translateService . get ( 'collection.source.controls.import.failed' ) ) ;
150+ this . importRunning$ . next ( false ) ;
151+ }
152+ if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) ) {
153+ this . notificationsService . success ( this . translateService . get ( 'collection.source.controls.import.completed' ) ) ;
154+ this . requestService . setStaleByHrefSubstring ( this . collection . _links . self . href ) ;
155+ this . importRunning$ . next ( false ) ;
173156 }
174- ) ) ;
157+ } ) ) ;
175158 }
176159
177160 /**
@@ -194,31 +177,22 @@ export class CollectionSourceControlsComponent implements OnDestroy {
194177 }
195178 } ) ,
196179 filter ( ( rd ) => rd . hasSucceeded && hasValue ( rd . payload ) ) ,
197- switchMap ( ( rd ) => this . processDataService . findById ( rd . payload . processId , false ) ) ,
198- getAllCompletedRemoteData ( ) ,
199- filter ( ( rd ) => ! rd . isStale && ( rd . hasSucceeded || rd . hasFailed ) ) ,
200- map ( ( rd ) => rd . payload ) ,
201- hasValueOperator ( ) ,
180+ switchMap ( ( rd ) => {
181+ this . autoRefreshIDs . push ( rd . payload . processId ) ;
182+ return this . processDataService . autoRefreshUntilCompletion ( rd . payload . processId ) ;
183+ } ) ,
184+ map ( ( rd ) => rd . payload )
202185 ) . subscribe ( ( process ) => {
203- if ( process . processStatus . toString ( ) !== ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) &&
204- process . processStatus . toString ( ) !== ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
205- // Ping the current process state every 5s
206- setTimeout ( ( ) => {
207- this . requestService . setStaleByHrefSubstring ( process . _links . self . href ) ;
208- this . requestService . setStaleByHrefSubstring ( this . collection . _links . self . href ) ;
209- } , 5000 ) ;
210- }
211- if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
212- this . notificationsService . error ( this . translateService . get ( 'collection.source.controls.reset.failed' ) ) ;
213- this . reImportRunning$ . next ( false ) ;
214- }
215- if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) ) {
216- this . notificationsService . success ( this . translateService . get ( 'collection.source.controls.reset.completed' ) ) ;
217- this . requestService . setStaleByHrefSubstring ( this . collection . _links . self . href ) ;
218- this . reImportRunning$ . next ( false ) ;
219- }
186+ if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . FAILED ] . toString ( ) ) {
187+ this . notificationsService . error ( this . translateService . get ( 'collection.source.controls.reset.failed' ) ) ;
188+ this . reImportRunning$ . next ( false ) ;
220189 }
221- ) ) ;
190+ if ( process . processStatus . toString ( ) === ProcessStatus [ ProcessStatus . COMPLETED ] . toString ( ) ) {
191+ this . notificationsService . success ( this . translateService . get ( 'collection.source.controls.reset.completed' ) ) ;
192+ this . requestService . setStaleByHrefSubstring ( this . collection . _links . self . href ) ;
193+ this . reImportRunning$ . next ( false ) ;
194+ }
195+ } ) ) ;
222196 }
223197
224198 ngOnDestroy ( ) : void {
@@ -227,5 +201,9 @@ export class CollectionSourceControlsComponent implements OnDestroy {
227201 sub . unsubscribe ( ) ;
228202 }
229203 } ) ;
204+
205+ this . autoRefreshIDs . forEach ( ( id ) => {
206+ this . processDataService . stopAutoRefreshing ( id ) ;
207+ } ) ;
230208 }
231209}
0 commit comments