@@ -15,6 +15,8 @@ export class AplOperator {
15
15
private lastRevision = ''
16
16
private repoPath : string
17
17
private repoUrl : string
18
+ private isApplying = false
19
+ private reconcileInterval = 300_000 // 5 minutes in milliseconds
18
20
19
21
constructor (
20
22
username : string ,
@@ -44,13 +46,7 @@ export class AplOperator {
44
46
try {
45
47
await $ `git clone ${ this . repoUrl } ${ this . repoPath } `
46
48
this . d . info ( `Setting Git safe.directory to ${ this . repoPath } ` )
47
- const listRoot = await $ `ls -la` . nothrow ( )
48
- this . d . log ( 'ls -la:\n' , listRoot . stdout )
49
49
50
- const currentDir = await $ `pwd` . nothrow ( )
51
- this . d . log ( 'pwd:\n' , currentDir . stdout )
52
- // this.d.info('setting git config')
53
- // await $`git config --global --add safe.directory ${this.repoPath}`
54
50
const result = await $ `git log -1 --pretty=format:"%H"`
55
51
const commitHash = result . stdout . trim ( )
56
52
this . lastRevision = commitHash || ''
@@ -69,10 +65,10 @@ export class AplOperator {
69
65
const previousRevision = this . lastRevision
70
66
71
67
// Pull the latest changes
72
- await $ `git pull` . quiet ( )
68
+ await $ `git pull`
73
69
74
70
// Get both hash and commit message in one command
75
- const result = await $ `git log -1 --pretty=format:"%H|%B"` . quiet ( )
71
+ const result = await $ `git log -1 --pretty=format:"%H|%B"`
76
72
const [ newRevision , commitMessage ] = result . stdout . split ( '|' , 2 )
77
73
78
74
if ( newRevision && newRevision !== previousRevision ) {
@@ -160,6 +156,46 @@ export class AplOperator {
160
156
}
161
157
}
162
158
159
+ private async runApplyIfNotBusy ( trigger : string ) : Promise < void > {
160
+ if ( this . isApplying ) {
161
+ this . d . info ( `[${ trigger } ] Apply already in progress, skipping` )
162
+ return
163
+ }
164
+
165
+ this . isApplying = true
166
+ this . d . info ( `[${ trigger } ] Starting apply process` )
167
+
168
+ try {
169
+ await this . executeApply ( )
170
+ await this . executeApplyAsApps ( )
171
+ this . d . info ( `[${ trigger } ] Apply process completed` )
172
+ } catch ( error ) {
173
+ this . d . error ( `[${ trigger } ] Apply process failed` , error )
174
+ } finally {
175
+ this . isApplying = false
176
+ }
177
+ }
178
+
179
+ private async periodicallyReconcile ( ) : Promise < void > {
180
+ this . d . info ( 'Starting reconciliation loop' )
181
+
182
+ while ( this . isRunning ) {
183
+ try {
184
+ this . d . info ( 'Reconciliation triggered' )
185
+
186
+ await this . runApplyIfNotBusy ( 'reconcile' )
187
+
188
+ this . d . info ( 'Reconciliation completed' )
189
+ } catch ( error ) {
190
+ this . d . error ( 'Error during reconciliation:' , error )
191
+ }
192
+
193
+ await new Promise ( ( resolve ) => setTimeout ( resolve , this . reconcileInterval ) )
194
+ }
195
+
196
+ this . d . info ( 'Reconciliation loop stopped' )
197
+ }
198
+
163
199
private async pollForChangesAndApplyIfAny ( ) : Promise < void > {
164
200
this . d . info ( 'Starting polling loop' )
165
201
@@ -170,9 +206,7 @@ export class AplOperator {
170
206
if ( hasChanges ) {
171
207
this . d . info ( 'Changes detected, triggering apply process' )
172
208
173
- // Execute them not in parallel as it resulted in issues
174
- await this . executeApply ( )
175
- await this . executeApplyAsApps ( )
209
+ await this . runApplyIfNotBusy ( 'poll' )
176
210
177
211
this . d . info ( 'Apply process completed successfully' )
178
212
} else {
@@ -211,14 +245,18 @@ export class AplOperator {
211
245
await this . executeApply ( )
212
246
await this . executeApplyAsApps ( )
213
247
214
- await this . pollForChangesAndApplyIfAny ( )
215
-
216
248
this . d . info ( 'APL operator started successfully' )
217
249
} catch ( error ) {
218
250
this . isRunning = false
219
251
this . d . error ( 'Failed to start APL operator:' , error )
220
252
throw error
221
253
}
254
+
255
+ try {
256
+ await Promise . all ( [ this . pollForChangesAndApplyIfAny ( ) , this . periodicallyReconcile ( ) ] )
257
+ } catch ( error ) {
258
+ this . d . error ( 'Error during polling or reconciling:' , error )
259
+ }
222
260
}
223
261
224
262
public stop ( ) : void {
0 commit comments