runBatch($directory, ['jushuitan:sync-shipping', '--days=3']); $this->assertSame(75, $process->getExitCode()); $this->assertFileDoesNotExist($directory . '/called'); $import = $this->runBatch($directory, ['order-data:import-after-sales-cost', '/input.xlsx', '--dry-run']); $this->assertSame(75, $import->getExitCode()); $this->assertFileDoesNotExist($directory . '/called'); flock($lock, LOCK_UN); $process = $this->runBatch($directory, ['jushuitan:sync-shipping', '--days=3']); $this->assertTrue($process->isSuccessful(), $process->getErrorOutput()); $arguments = file($directory . '/called', FILE_IGNORE_NEW_LINES); $this->assertContains('batch', $arguments); $this->assertSame(['jushuitan:sync-shipping', '--days=3'], array_slice($arguments, -2)); $this->assertContains('--no-deps', $arguments); $this->assertSame('image=123456789abc', $arguments[0]); } finally { fclose($lock); foreach (glob($directory . '/*') as $path) { unlink($path); } rmdir($directory); } } private function runBatch(string $directory, array $arguments): Process { file_put_contents($directory . '/docker', '#!/bin/sh' . "\n" . 'if [ "$1" = inspect ]; then case "$4" in *Mounts*) echo "$CAIWU_STORAGE_ROOT" ;; *) echo caiwu_akaunting:123456789abc ;; esac; exit 0; fi' . "\n" . 'printf "%s\n" "image=$IMAGE_TAG" "$@" > "$CAIWU_CAPTURE"' . "\n"); chmod($directory . '/docker', 0700); $process = new Process(array_merge(['sh', dirname(__DIR__, 3) . '/deploy/run-finance-batch.sh'], $arguments), $directory, [ 'PATH' => $directory . ':' . getenv('PATH'), 'CAIWU_REMOTE_DIR' => $directory, 'CAIWU_UPSTREAM_LOCK' => $directory . '/sync.lock', 'CAIWU_BATCH_LOCK_WAIT' => '0.1', 'CAIWU_CAPTURE' => $directory . '/called', 'CAIWU_STORAGE_ROOT' => $directory . '/storage', ]); $process->run(); return $process; } public function test_exports_skip_idle_containers_and_can_read_while_upstream_writer_holds_lock(): void { $directory = sys_get_temp_dir() . '/caiwu-export-batch-' . bin2hex(random_bytes(6)); mkdir($directory . '/storage/app/account-bills-exports/1', 0777, true); $lock = fopen($directory . '/sync.lock', 'c'); flock($lock, LOCK_EX); $task = $directory . '/storage/app/account-bills-exports/1/task.json'; try { $this->assertTrue($this->runBatch($directory, ['account-bills:exports:run'])->isSuccessful()); $this->assertFileDoesNotExist($directory . '/called'); file_put_contents($task, json_encode(['status' => 'queued'])); $this->assertTrue($this->runBatch($directory, ['account-bills:exports:run'])->isSuccessful()); $this->assertFileExists($directory . '/called'); } finally { fclose($lock); if (is_file($task)) unlink($task); foreach (['/storage/app/account-bills-exports/1', '/storage/app/account-bills-exports', '/storage/app', '/storage'] as $path) rmdir($directory . $path); foreach (glob($directory . '/*') as $path) unlink($path); rmdir($directory); } } public function test_finance_ledger_recalculation_runs_in_the_isolated_batch_profile(): void { $directory = sys_get_temp_dir() . '/caiwu-ledger-batch-' . bin2hex(random_bytes(6)); mkdir($directory); try { $process = $this->runBatch($directory, ['finance-ledger:recalculate']); $this->assertTrue($process->isSuccessful(), $process->getErrorOutput()); $arguments = file($directory . '/called', FILE_IGNORE_NEW_LINES); $this->assertContains('batch', $arguments); $this->assertSame('finance-ledger:recalculate', end($arguments)); $this->assertContains('--no-deps', $arguments); $this->assertSame('image=123456789abc', $arguments[0]); } finally { foreach (glob($directory . '/*') as $path) { unlink($path); } rmdir($directory); } } public function test_business_mirror_history_backfill_runs_in_the_isolated_batch_profile(): void { $directory = sys_get_temp_dir() . '/caiwu-mirror-batch-' . bin2hex(random_bytes(6)); mkdir($directory); $arguments = [ 'business-mirror:sync-tables', 'jiansun.dbo.fys_Ticketing_install_item', '--full', '--batch=500', '--max-batches=20', ]; try { $process = $this->runBatch($directory, $arguments); $this->assertTrue($process->isSuccessful(), $process->getErrorOutput()); $called = file($directory . '/called', FILE_IGNORE_NEW_LINES); $this->assertContains('batch', $called); $this->assertContains('--no-deps', $called); $this->assertSame('image=123456789abc', $called[0]); $this->assertSame($arguments, array_slice($called, -count($arguments))); } finally { foreach (glob($directory . '/*') as $path) { unlink($path); } rmdir($directory); } } }