1
1
import { v4 as uuidv4 } from 'uuid' ;
2
2
import config from 'src/utils/config' ;
3
- import { CloudJobInfo , CloudJobStatus } from 'src/modules/cloud/job/models/cloud-job-info' ;
3
+ import { CloudJobInfo , CloudJobStatus , CloudJobStep } from 'src/modules/cloud/job/models/cloud-job-info' ;
4
4
import { HttpException , Logger } from '@nestjs/common' ;
5
5
import { ClassType } from 'class-transformer/ClassTransformer' ;
6
6
import { CloudJobAbortedException , wrapCloudJobError } from 'src/modules/cloud/job/exceptions' ;
7
7
import { SessionMetadata } from 'src/common/models' ;
8
8
import { CloudJobName } from 'src/modules/cloud/job/constants' ;
9
9
import { CloudRequestUtm } from 'src/modules/cloud/common/models' ;
10
+ import { debounce } from 'lodash' ;
10
11
11
12
const cloudConfig = config . get ( 'cloud' ) ;
12
13
@@ -31,6 +32,8 @@ export abstract class CloudJob {
31
32
32
33
protected status = CloudJobStatus . Initializing ;
33
34
35
+ protected step = CloudJobStep . Credentials ;
36
+
34
37
protected error ?: HttpException ;
35
38
36
39
protected child ?: CloudJob ;
@@ -41,18 +44,33 @@ export abstract class CloudJob {
41
44
42
45
protected dependencies : any ;
43
46
47
+ private readonly debounce : any ;
48
+
44
49
protected constructor ( options : CloudJobOptions ) {
45
50
this . options = options ;
46
51
47
52
if ( ! this . options . stateCallbacks ) {
48
53
this . options . stateCallbacks = [ ] ;
49
54
}
55
+
56
+ this . debounce = debounce ( ( ) => {
57
+ try {
58
+ ( this . options ?. stateCallbacks || [ ] ) . forEach ( ( cb ) => {
59
+ cb ?.( this ) ?. catch ?.( ( ) => { } ) ;
60
+ } ) ;
61
+ } catch ( e ) {
62
+ // silently ignore callback
63
+ }
64
+ } , 1_000 , {
65
+ maxWait : 1_000 ,
66
+ } ) ;
50
67
}
51
68
52
69
public async run ( ) {
53
70
try {
54
71
this . changeState ( {
55
72
status : CloudJobStatus . Running ,
73
+ step : CloudJobStep . Initializing ,
56
74
} ) ;
57
75
58
76
return await this . iteration ( ) ;
@@ -83,6 +101,7 @@ export abstract class CloudJob {
83
101
result : this . result ,
84
102
error : this . error ? wrapCloudJobError ( this . error ) . getResponse ( ) : undefined ,
85
103
child : this . child ?. getState ( ) ,
104
+ step : this . step ,
86
105
} ;
87
106
}
88
107
@@ -116,13 +135,8 @@ export abstract class CloudJob {
116
135
117
136
protected changeState ( state = { } ) {
118
137
Object . entries ( state ) . forEach ( ( [ key , value ] ) => { this [ key ] = value ; } ) ;
119
- try {
120
- ( this . options ?. stateCallbacks || [ ] ) . forEach ( ( cb ) => {
121
- cb ?.( this ) ?. catch ?.( ( ) => { } ) ;
122
- } ) ;
123
- } catch ( e ) {
124
- // silently ignore callback
125
- }
138
+
139
+ this . debounce ( ) ;
126
140
}
127
141
128
142
protected checkSignal ( ) {
0 commit comments