Skip to content

Commit 422bb2c

Browse files
committed
feat: add reconcile loop
1 parent 95eb5c7 commit 422bb2c

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

src/operator/apl-operator.ts

+51-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export class AplOperator {
1515
private lastRevision = ''
1616
private repoPath: string
1717
private repoUrl: string
18+
private isApplying = false
19+
private reconcileInterval = 300_000 // 5 minutes in milliseconds
1820

1921
constructor(
2022
username: string,
@@ -44,13 +46,7 @@ export class AplOperator {
4446
try {
4547
await $`git clone ${this.repoUrl} ${this.repoPath}`
4648
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)
4949

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}`
5450
const result = await $`git log -1 --pretty=format:"%H"`
5551
const commitHash = result.stdout.trim()
5652
this.lastRevision = commitHash || ''
@@ -69,10 +65,10 @@ export class AplOperator {
6965
const previousRevision = this.lastRevision
7066

7167
// Pull the latest changes
72-
await $`git pull`.quiet()
68+
await $`git pull`
7369

7470
// 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"`
7672
const [newRevision, commitMessage] = result.stdout.split('|', 2)
7773

7874
if (newRevision && newRevision !== previousRevision) {
@@ -160,6 +156,46 @@ export class AplOperator {
160156
}
161157
}
162158

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+
163199
private async pollForChangesAndApplyIfAny(): Promise<void> {
164200
this.d.info('Starting polling loop')
165201

@@ -170,9 +206,7 @@ export class AplOperator {
170206
if (hasChanges) {
171207
this.d.info('Changes detected, triggering apply process')
172208

173-
// Execute them not in parallel as it resulted in issues
174-
await this.executeApply()
175-
await this.executeApplyAsApps()
209+
await this.runApplyIfNotBusy('poll')
176210

177211
this.d.info('Apply process completed successfully')
178212
} else {
@@ -211,14 +245,18 @@ export class AplOperator {
211245
await this.executeApply()
212246
await this.executeApplyAsApps()
213247

214-
await this.pollForChangesAndApplyIfAny()
215-
216248
this.d.info('APL operator started successfully')
217249
} catch (error) {
218250
this.isRunning = false
219251
this.d.error('Failed to start APL operator:', error)
220252
throw error
221253
}
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+
}
222260
}
223261

224262
public stop(): void {

0 commit comments

Comments
 (0)