157 lines
5.3 KiB
PHP
157 lines
5.3 KiB
PHP
<?php
|
|
/**
|
|
* Weld Logs to Handovers Sync Cron Job
|
|
*
|
|
* This cron job synchronizes data from weld_logs table to handovers table
|
|
* using chunk processing and database transactions for better performance and reliability.
|
|
*/
|
|
|
|
try {
|
|
// Configuration
|
|
$chunkSize = 1000; // Process records in chunks to avoid memory issues
|
|
$batchSize = 100; // Process database operations in smaller batches
|
|
|
|
// Get base column from settings
|
|
$handOverZoneColumn = setting('hand_over_zone_column') ?? "line_number";
|
|
|
|
// Statistics tracking
|
|
$stats = [
|
|
'total_processed' => 0,
|
|
'handovers_created' => 0,
|
|
'handovers_updated' => 0,
|
|
'errors' => 0,
|
|
'start_time' => microtime(true)
|
|
];
|
|
|
|
echo "=== Weld Logs to Handovers Sync Started ===\n";
|
|
echo "Time: " . now() . "\n\n";
|
|
|
|
// Get total count for progress tracking
|
|
$totalRecords = db("weld_logs")->count();
|
|
echo "Total weld_logs records to process: {$totalRecords}\n\n";
|
|
|
|
// Process in chunks to avoid memory issues
|
|
db("weld_logs")
|
|
->select($handOverZoneColumn, 'project', 'design_area', 'piping_type')
|
|
->distinct()
|
|
->orderBy($handOverZoneColumn) // Laravel chunk() için orderBy gerekli
|
|
->chunk($chunkSize, function ($weldLogs) use (&$stats, $batchSize, $handOverZoneColumn) {
|
|
|
|
// Group by base column to avoid duplicates
|
|
$uniqueLines = [];
|
|
foreach ($weldLogs as $weldLog) {
|
|
$baseValue = $weldLog->{$handOverZoneColumn};
|
|
if (!empty($baseValue)) {
|
|
$uniqueLines[$baseValue] = $weldLog;
|
|
}
|
|
}
|
|
|
|
// Process unique lines in smaller batches for database operations
|
|
$batch = [];
|
|
foreach ($uniqueLines as $baseValue => $weldLog) {
|
|
|
|
$batch[] = [
|
|
'base_value' => $baseValue,
|
|
'weld_log' => $weldLog
|
|
];
|
|
|
|
// Process batch when it reaches batch size
|
|
if (count($batch) >= $batchSize) {
|
|
processBatch($batch, $stats);
|
|
$batch = [];
|
|
}
|
|
}
|
|
|
|
// Process remaining records in the last batch
|
|
if (!empty($batch)) {
|
|
processBatch($batch, $stats);
|
|
}
|
|
|
|
$stats['total_processed'] += count($uniqueLines);
|
|
|
|
// Progress update
|
|
echo "Processed chunk: " . count($uniqueLines) . " unique lines\n";
|
|
});
|
|
|
|
// Final statistics
|
|
$executionTime = round(microtime(true) - $stats['start_time'], 2);
|
|
|
|
echo "\n=== Sync Completed ===\n";
|
|
echo "Total processed: {$stats['total_processed']}\n";
|
|
echo "Handovers created: {$stats['handovers_created']}\n";
|
|
echo "Handovers updated: {$stats['handovers_updated']}\n";
|
|
echo "Errors: {$stats['errors']}\n";
|
|
echo "Execution time: {$executionTime} seconds\n";
|
|
echo "Time: " . now() . "\n";
|
|
|
|
} catch (Exception $e) {
|
|
echo "CRITICAL ERROR: " . $e->getMessage() . "\n";
|
|
echo "Stack trace: " . $e->getTraceAsString() . "\n";
|
|
exit(1);
|
|
}
|
|
|
|
/**
|
|
* Process a batch of weld logs to create/update handovers
|
|
*/
|
|
function processBatch($batch, &$stats)
|
|
{
|
|
try {
|
|
// Use database transaction for batch operations
|
|
\DB::beginTransaction();
|
|
|
|
foreach ($batch as $item) {
|
|
$baseValue = $item['base_value'];
|
|
$weldLog = $item['weld_log'];
|
|
|
|
try {
|
|
// Prepare handover data
|
|
$handoverData = [
|
|
'project' => $baseValue, // base column -> project (location)
|
|
'object' => $weldLog->project ?? '', // project -> object
|
|
'location' => $weldLog->design_area ?? '', // design_area -> location
|
|
'work_type' => 'ТРУБКА', // piping_type -> work_type
|
|
'status1' => 'In Progress',
|
|
'updated_at' => now()
|
|
];
|
|
|
|
// Check if handover record exists
|
|
$existingHandover = db("handovers")
|
|
->where('project', $baseValue)
|
|
->first();
|
|
|
|
if ($existingHandover) {
|
|
// Update existing record
|
|
db("handovers")
|
|
->where('id', $existingHandover->id)
|
|
->update($handoverData);
|
|
|
|
$stats['handovers_updated']++;
|
|
|
|
} else {
|
|
// Create new record
|
|
$handoverData['created_at'] = now();
|
|
$handoverData['final_status'] = 'Waiting';
|
|
db("handovers")->insert($handoverData);
|
|
|
|
$stats['handovers_created']++;
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
$stats['errors']++;
|
|
echo "Error processing base value {$baseValue}: " . $e->getMessage() . "\n";
|
|
continue; // Continue with next record instead of failing entire batch
|
|
}
|
|
}
|
|
|
|
// Commit transaction if all operations successful
|
|
\DB::commit();
|
|
|
|
} catch (Exception $e) {
|
|
// Rollback transaction on error
|
|
\DB::rollBack();
|
|
$stats['errors']++;
|
|
echo "Batch processing error: " . $e->getMessage() . "\n";
|
|
throw $e; // Re-throw to be caught by main try-catch
|
|
}
|
|
}
|
|
?>
|