Hi! Solved this myself, running:
sudo chmod -R ugo+rw storage
Hi all!
Have this Job, that are working fine localy but not on server
Here is my Job-file
<?php
namespace App\Jobs;
use App\Models\UnprocessedGpsData;
use App\Models\TrackerGps;
use App\Facades\GpsService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Throwable;
class ProcessGpsDataJob extends Job
{
protected $records;
protected $maxRetries = 5; // Maximum retry attempts
protected $retryDelay = 100; // Delay in milliseconds
public function __construct(array $records)
{
$this->records = $records;
}
public function handle()
{
$idsToDelete = [];
$idsToUpdate = [];
$idsToProcess = array_column($this->records, 'id');
Log::info('Job started now: ');
// Update processing_by field before processing
try {
UnprocessedGpsData::whereIn('id', $idsToProcess)->update([
'processing_by' => getmypid(),
]);
Log::info('Job started with IDs: ' . implode(',', $idsToProcess));
} catch (Throwable $e) {
Log::error('Error in job: ' . $e->getMessage());
throw $e; // Rethrow to ensure job handling respects the failure
}
$attempt = 0;
while ($attempt < $this->maxRetries) {
try {
DB::transaction(function () use (&$idsToDelete, &$idsToUpdate) {
foreach ($this->records as $unprocessed) {
$tracker = TrackerGps::firstOrCreate(['tracker' => $unprocessed['tracker']], [
'golfclub_id' => config('tracker.default_club'),
]);
// Process GPS data
$isProcessed = GpsService::storegpsposWithCoordinates(
null,
$tracker,
$unprocessed['latitude'],
$unprocessed['longitude'],
$unprocessed['date'],
$unprocessed['battery'],
0.0,
1
);
if ($isProcessed) {
$idsToDelete[] = $unprocessed['id'];
} else {
$idsToUpdate[] = $unprocessed['id'];
}
}
// Force delete processed records
foreach (array_chunk($idsToDelete, 500) as $batchDelete) {
UnprocessedGpsData::whereIn('id', $batchDelete)->forceDelete();
Log::info('Force deleted records (Processed): ' . implode(',', $batchDelete));
}
// // Force delete failed-to-process records
foreach (array_chunk($idsToUpdate, 500) as $batchUpdate) {
UnprocessedGpsData::whereIn('id', $batchUpdate)->forceDelete();
Log::info('Force deleted records (Failed): ' . implode(',', $batchUpdate));
}
});
// If the transaction is successful, break out of the retry loop
break;
} catch (Throwable $e) {
$attempt++;
// Check if the error is a deadlock
if ($e instanceof \Illuminate\Database\QueryException && str_contains($e->getMessage(), '1213')) {
Log::warning("Deadlock detected on attempt $attempt. Retrying...");
usleep($this->retryDelay * 1000); // Convert milliseconds to microseconds
} else {
// Log and rethrow other errors
Log::error('Job failed: ' . $e->getMessage());
throw $e;
}
}
}
if ($attempt >= $this->maxRetries) {
Log::error("Job failed after $this->maxRetries attempts due to persistent deadlock issues.");
}
}
}
The error on my server
oot@easyflow-test:/etc/supervisor/conf.d# tail -f /home/forge/.forge/second_job.log
138▕ }
139▕ stream_set_chunk_size($stream, $this->streamChunkSize);
140▕ $this->stream = $stream;
141▕ }
+12 vendor frames
13 /home/forge/test.easyflow.golf/releases/20241210102605/artisan:37
Illuminate\Foundation\Console\Kernel::handle()
2024-12-10 10:41:03 App\Jobs\ProcessGpsDataJob ..................... RUNNING
2024-12-10 10:41:04 App\Jobs\ProcessGpsDataJob ..................... 1s FAIL
UnexpectedValueException
The stream or file "/home/forge/test.easyflow.golf/releases/20241210102605/storage/logs/laravel-2024-12-10.log" could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: The stream or file "/home/forge/test.easyflow.golf/releases/20241210102605/storage/logs/laravel-2024-12-10.log" could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: The stream or file "/home/forge/test.easyflow.golf/releases/20241210102605/storage/logs/laravel-2024-12-10.log" could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: Error in job: The stream or file "/home/forge/test.easyflow.golf/releases/20241210102605/storage/logs/laravel-2024-12-10.log" could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: Job started with IDs: 25,26,27,28,29,30,31,32,33,34,35,36
Context: {"exception":{}}
Context: {"exception":{}}
at /home/forge/test.easyflow.golf/releases/20241210102605/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:137
133▕ }
134▕ if (!is_resource($stream)) {
135▕ $this->stream = null;
136▕
➜ 137▕ throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened in append mode: '.$this->errorMessage, $url) . Utils::getRecordMessageForException($record));
138▕ }
139▕ stream_set_chunk_size($stream, $this->streamChunkSize);
140▕ $this->stream = $stream;
141▕ }
Error say that it somthing to do with permission, but cant figer it out
Please help
Please or to participate in this conversation.