5.0 KiB
5.0 KiB
Register Creator Queue System - Fix Summary
Problem Identified
The Register Creator queue system was experiencing the following issues:
- RegisterCreatorJob failing immediately - Jobs were being processed but failing without clear error messages
- No progress updates - Progress tracking wasn't working in queue context
- Auth context missing -
Auth::user()was being called in queue workers where no authenticated user exists - Slow PDF processing -
pdftkcommands were running without timeout, potentially hanging the system
Root Causes
1. Authentication Context Issue
The ProgressTracker class was calling Auth::user() in its addToQueue() method. In queue worker context, there's no authenticated user, causing the job to fail.
2. Missing User ID in Job Dispatch
The RegisterCreatorJob wasn't receiving the user ID, making it impossible to track who initiated the job.
3. Inefficient PDF Page Counting
The addRowInTable function was using shell_exec to run pdftk commands without:
- Timeout protection (could hang indefinitely)
- Caching (same files were being checked multiple times)
- Error output suppression
4. Progress Updates Not Visible
Progress updates weren't showing proper percentage or status information.
Solutions Implemented
1. Fixed ProgressTracker Authentication (app/Services/RegisterCreator/ProgressTracker.php)
// Before: Would crash in queue context
$user = Auth::user();
// After: Gracefully handles missing auth context
try {
$user = Auth::user();
} catch (\Throwable $th) {
$user = null;
Log::warning("Could not get authenticated user in queue context");
}
2. Added User ID to RegisterCreatorJob (app/Jobs/RegisterCreatorJob.php)
// Added userId property
public int $userId;
// Constructor now accepts user ID
public function __construct(array $jobData, int $userId = null)
{
$this->jobData = $jobData;
$this->userId = $userId;
}
// User ID is now logged and tracked
$settings['tracking_user_id'] = $this->userId;
3. Updated Job Dispatch (app/Http/Controllers/RegisterCreatorController.php)
// Now passes user ID when dispatching
RegisterCreatorJob::dispatch($jobData, $u->id);
4. Optimized PDF Page Counting (app/Functions/addRowInTable.php)
// Added timeout to prevent hanging
$command = "timeout 5 pdftk {$allPath} dump_data 2>/dev/null | grep NumberOfPages";
// Added caching to prevent redundant checks
static $pageCountCache = [];
if (isset($pageCountCache[$cacheKey])) {
$pageCount = $pageCountCache[$cacheKey];
}
5. Enhanced Progress Updates (app/Services/RegisterCreator/RegisterCreatorService.php)
// Now shows actual percentage and detailed descriptions
$progressPercent = (int) (($index + 1) / count($documents) * 100);
$this->progressTracker->update(
"Processing document {$index + 1}/{$total}: {$docTitle}",
$progressPercent
);
6. Added Error Resilience (app/Services/RegisterCreator/ProgressTracker.php)
// Progress tracking failures won't stop the job
try {
Cache::put("register-creator-progress-{$this->jobId}", $progressData, ...);
} catch (\Throwable $th) {
Log::error("Failed to update progress...");
}
Queue Worker Configuration
Started a dedicated queue worker with extended timeouts:
nohup php artisan queue:work database \
--timeout=7200 \
--tries=3 \
--sleep=3 \
--max-time=7200 \
> /var/www/html/stellar2/storage/logs/queue-worker.log 2>&1 &
Parameters:
--timeout=7200: Job can run up to 2 hours--tries=3: Retry failed jobs up to 3 times--sleep=3: Wait 3 seconds between jobs--max-time=7200: Worker restarts after 2 hours
Testing & Verification
To test the fixes:
-
Check if queue worker is running:
ps aux | grep "queue:work" -
Monitor queue worker log:
tail -f /var/www/html/stellar2/storage/logs/queue-worker.log -
Check job progress:
php artisan tinker Cache::get('register-creator-queue') -
Monitor Laravel logs:
tail -f /var/www/html/stellar2/storage/logs/truncgil-$(date +%Y-%m-%d).log | grep -i register
Performance Improvements
- PDF Processing: 5-second timeout prevents indefinite hanging
- Caching: Page counts are cached, reducing redundant
pdftkcalls - Progress Updates: More informative with actual percentages
- Error Handling: Jobs continue even if progress tracking fails
Next Steps
- Monitor the queue worker for the next few hours
- Check that progress updates are appearing in the UI
- Verify that jobs complete successfully
- Review Laravel logs for any new errors
- Consider adding Redis for better queue performance (optional)
Additional Notes
- All code comments and text messages are in English as per project requirements
- The system now handles missing authentication context gracefully
- PDF processing has timeout protection to prevent hanging
- Progress tracking is non-blocking and won't stop job execution if it fails