import { IMineMeldAPIService , IMineMeldAPIResource } from './minemeldapi' ; export interface IMineMeldJob { description : string ; status : string ; returncode : number ; logfile : string ; } export interface IMineMeldJobMonitor { monitor ? : angular . IPromise < any > ; job_group : string ; jobid : string ; } export interface IMineMeldJobsService { monitor ( job_group , jobid ) : angular . IPromise < IMineMeldJob > ; get ( job_group , jobid ) : angular . IPromise < any > ; } export class MineMeldJobsService implements IMineMeldJobsService { MineMeldAPIService : IMineMeldAPIService ; toastr : any ; $interval : angular . IIntervalService ; $q : angular . IQService ; monitors : IMineMeldJobMonitor [ ] = [ ] ; constructor ( toastr , $interval , MineMeldAPIService , $q ) { this . MineMeldAPIService = MineMeldAPIService ; this . toastr = toastr ; this . $interval = $interval ; this . $q = $q ; this . MineMeldAPIService . onLogout ( this . destroyJobsMonitor . bind ( this ) ) ; } monitor ( job_group , jobid ) { var monitor = { job_group : job_group , jobid : jobid } ; var deferred = this . $q . defer ( ) ; monitor . monitor = this . $interval ( this . checkJob . bind ( this ) , 3000 , 1 , false , monitor , job_group , jobid , deferred ) ; this . monitors . push ( monitor ) ; return deferred . promise ; } get ( job_group , jobid ) { var api ; var params = { job_group : job_group , jobid : jobid } ; api = this . MineMeldAPIService . getAPIResource ( '/jobs/:job_group/:jobid' , { } , { get : { method : 'GET' } } ) ; return api . get ( params ) . $promise . then ( ( result ) => { if ( 'result' in result ) { return result . result ; } return undefined ; } ) ; } private destroyJobsMonitor ( ) { angular . forEach ( this . monitors , ( monitor ) => { this . $interval . cancel ( monitor . monitor ) ; } ) ; this . monitors = [ ] ; } private checkJob ( monitor , job_group , jobid , deferred ) { this . get ( job_group , jobid ) . then ( ( result ) => { var joburl ; if ( result . status === 'RUNNING' ) { monitor . monitor = this . $interval ( this . checkJob . bind ( this ) , 3000 , 1 , false , monitor , job_group , jobid , deferred ) ; return ; } joburl = '/jobs/' + job_group + '/' + jobid + '/log' ; if ( result . status === 'DONE' ) { this . toastr . success ( 'JOB ' + result . description + ' COMPLETED SUCCESSFULLY. LOG <a target="_blank" href="' + joburl + '">HERE</a>' , { allowHtml : true } ) ; } if ( result . status === 'ERROR' ) { this . toastr . error ( 'JOB ' + result . description + ' FAILED. LOG <a target="_blank" href="' + joburl + '">HERE</a>' , { allowHtml : true } ) ; } deferred . resolve ( result ) ; } , ( error ) => { this . toastr . error ( 'ERROR CHECKING STATUS OF JOB ' + job_group + ' ' + jobid + ': ' + error . statusText ) ; this . monitors = this . monitors . filter ( ( monitor ) => { if ( monitor . jobid != jobid ) { return true ; } if ( monitor . job_group != job_group ) { return true ; } this . $interval . cancel ( monitor . monitor ) ; return false ; } ) ; deferred . reject ( error ) ; } ) ; } }