Fix race condition

pull/6062/head
Daniel Supernault 1 year ago
parent d595963f51
commit bbc7f7b0d4
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

@ -163,12 +163,7 @@ class TransformImports extends Command
throw new \Exception("Could not generate unique creation_id for ImportPost ID {$ip->id}");
}
$uid = str_pad($id, 6, 0, STR_PAD_LEFT);
$yearStr = str_pad($uniqueIdData['year'], 2, 0, STR_PAD_LEFT);
$monthStr = str_pad($uniqueIdData['month'], 2, 0, STR_PAD_LEFT);
$dayStr = str_pad($uniqueIdData['day'], 2, 0, STR_PAD_LEFT);
$zone = $yearStr.$monthStr.$dayStr.str_pad($uniqueIdData['incr'], 3, 0, STR_PAD_LEFT);
$statusId = '1'.$uid.$zone;
$statusId = $uniqueIdData['status_id'];
$status = new Status;
$status->profile_id = $pid;

@ -192,23 +192,48 @@ class ImportService
}
$maxExistingIncr = $query->max('creation_id') ?? 0;
$incr = $maxExistingIncr + 1;
if ($incr > 999) {
[$newYear, $newMonth, $newDay] = self::getNextValidDate($year, $month, $day);
if (! $newYear) {
return null;
while ($incr <= 999) {
$uid = str_pad($userId, 6, 0, STR_PAD_LEFT);
$yearStr = str_pad($year, 2, 0, STR_PAD_LEFT);
$monthStr = str_pad($month, 2, 0, STR_PAD_LEFT);
$dayStr = str_pad($day, 2, 0, STR_PAD_LEFT);
$zone = $yearStr.$monthStr.$dayStr.str_pad($incr, 3, 0, STR_PAD_LEFT);
$statusId = '1'.$uid.$zone;
$statusExists = DB::table('statuses')->where('id', $statusId)->exists();
$importExists = ImportPost::where('user_id', $userId)
->where('creation_year', $year)
->where('creation_month', $month)
->where('creation_day', $day)
->where('creation_id', $incr)
->when($excludeImportPostId, function ($q) use ($excludeImportPostId) {
return $q->where('id', '!=', $excludeImportPostId);
})
->exists();
if (! $statusExists && ! $importExists) {
return [
'incr' => $incr,
'year' => $year,
'month' => $month,
'day' => $day,
'status_id' => $statusId,
];
}
return self::getUniqueCreationId($userId, $newYear, $newMonth, $newDay, $excludeImportPostId);
$incr++;
}
return [
'incr' => $incr,
'year' => $year,
'month' => $month,
'day' => $day,
];
[$newYear, $newMonth, $newDay] = self::getNextValidDate($year, $month, $day);
if (! $newYear) {
return null;
}
return self::getUniqueCreationId($userId, $newYear, $newMonth, $newDay, $excludeImportPostId);
}, 3);
}

Loading…
Cancel
Save