| | |
| | | try { |
| | | $incoming = $this->buildOperation($type, $userId, $data, $options); |
| | | $mergeable = $this->registry->getMergeable($type); |
| | | $existingById = $this->storage->find($incoming->id); |
| | | |
| | | if ($existingById) { |
| | | // Operation with this ID already exists |
| | | if (in_array($existingById->state, ['pending', 'scheduled']) && $mergeable) { |
| | | // Still pending and mergeable, merge into it |
| | | $merged = $mergeable->merge($existingById, $incoming); |
| | | $this->storage->save($merged); |
| | | $this->runQueueOnShutdown(); |
| | | |
| | | return [ |
| | | 'success' => true, |
| | | 'operation_id' => $merged->id, |
| | | 'updated_existing' => true, |
| | | ]; |
| | | } else { |
| | | // Already processing/completed, or not mergeable - generate new ID |
| | | $incoming->id = 'u' . $userId . '_' . time() . '_' . uniqid(); |
| | | |
| | | JVB()->error()->log( |
| | | '[Queue]:add', |
| | | 'Duplicate ID for non-mergeable operation, generated new ID', |
| | | [ |
| | | 'type' => $type, |
| | | 'existing_state' => $existingById->state, |
| | | ], |
| | | 'warning' |
| | | ); |
| | | } |
| | | } |
| | | |
| | | if ($mergeable) { |
| | | $existing = $this->storage->findMergeable($type, $userId); |