Files
PowershellAutomation/internal/runner.ps1
T
Arne Moerman 6c10d359d2 Enhance SandwichReminder-AutoOrder feature with direct checkout navigation and improved logic
- Added new configuration options for direct checkout navigation, including `useDirectCheckoutNavigation`, `checkoutPath`, and `checkoutOpenDelayMs`.
- Updated the auto-order flow to navigate directly to the checkout page, skipping the mini-cart and date/time steps.
- Improved keyboard automation logic for item remark and order confirmation processes.
- Removed the old SandwichAutoOrder.ps1 file as its functionality has been integrated into SandwichReminder-AutoOrder.ps1.
- Introduced a new runner-launcher.vbs to start the runner.ps1 without a visible console window, ensuring WinForms dialogs can appear.
- Implemented a mutex mechanism in runner.ps1 to prevent concurrent execution of the same feature.
2026-05-11 13:59:22 +02:00

93 lines
3.8 KiB
PowerShell

#Requires -Version 5.1
# runner.ps1 — background entry point; called by the scheduled task.
# All output goes to the log file only (no console window).
$ErrorActionPreference = 'Stop'
$script:InternalRoot = $PSScriptRoot # runner.ps1 lives in internal\
# ── Load shared libraries ─────────────────────────────────────────────────────
. (Join-Path $InternalRoot 'lib\Logging.ps1')
. (Join-Path $InternalRoot 'lib\NetworkUtils.ps1')
. (Join-Path $InternalRoot 'lib\ToastHelper.ps1')
. (Join-Path $InternalRoot 'lib\PromptHelper.ps1')
. (Join-Path $InternalRoot 'lib\Config.ps1')
Write-Log -Level Info -Message '────── Runner started ──────' -Feature 'Runner'
# ── Load config and state ─────────────────────────────────────────────────────
$config = Get-Config
$state = Get-State
# ── Run each enabled feature ──────────────────────────────────────────────────
$featuresDir = Join-Path $InternalRoot 'features'
foreach ($featureKey in $config.features.Keys) {
$featureConfig = $config.features[$featureKey]
if (-not $featureConfig['enabled']) {
continue
}
$featureFile = Join-Path $featuresDir "$featureKey.ps1"
if (-not (Test-Path $featureFile)) {
Write-Log -Level Warn `
-Message "Feature '$featureKey' is enabled but '$featureFile' was not found. Skipping." `
-Feature 'Runner'
continue
}
# Per-feature mutex: prevents two concurrent runner instances from executing
# the same feature simultaneously (e.g. if the previous run is still in progress).
$mutexName = "Global\PSAutomation-Feature-$featureKey"
$mutex = $null
$mutexAcquired = $false
try {
$mutex = New-Object System.Threading.Mutex($false, $mutexName)
$mutexAcquired = $mutex.WaitOne(0) # non-blocking: skip if already locked
if (-not $mutexAcquired) {
Write-Log -Level Warn `
-Message "Feature '$featureKey' skipped: another instance is still running." `
-Feature 'Runner'
continue
}
# Dot-source the feature — defines $FeatureMeta and Invoke-Feature in local scope.
# Each iteration overwrites Invoke-Feature, which is fine because we call it immediately.
$ErrorActionPreference = 'Stop'
. $featureFile
$featureState = if ($state.ContainsKey($featureKey)) { $state[$featureKey] } else { @{} }
$updatedState = Invoke-Feature -Config $featureConfig -State $featureState
if ($null -ne $updatedState) {
$state[$featureKey] = $updatedState
}
Write-Log -Level Info -Message "Feature '$featureKey' completed." -Feature 'Runner'
}
catch {
Write-Log -Level Error `
-Message "Feature '$featureKey' threw an unhandled exception: $_" `
-Feature 'Runner'
# Continue to the next feature regardless of this failure
}
finally {
$ErrorActionPreference = 'Stop'
if ($mutexAcquired -and $null -ne $mutex) {
try { $mutex.ReleaseMutex() } catch {}
}
if ($null -ne $mutex) {
try { $mutex.Dispose() } catch {}
}
}
}
# ── Persist updated state ─────────────────────────────────────────────────────
Save-State $state
Write-Log -Level Info -Message '────── Runner finished ──────' -Feature 'Runner'