diff --git a/core/modules/migrate/src/MigrateExecutable.php b/core/modules/migrate/src/MigrateExecutable.php index 59aada1381..a30bfac071 100644 --- a/core/modules/migrate/src/MigrateExecutable.php +++ b/core/modules/migrate/src/MigrateExecutable.php @@ -3,6 +3,7 @@ namespace Drupal\migrate; use Drupal\Component\Utility\Bytes; +use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Utility\Error; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\migrate\Event\MigrateEvents; @@ -90,6 +91,13 @@ class MigrateExecutable implements MigrateExecutableInterface { */ public $message; + /** + * The lock backend. + * + * @var \Drupal\Core\Lock\LockBackendInterface + */ + protected LockBackendInterface $lock; + /** * Constructs a MigrateExecutable and verifies and sets the memory limit. * @@ -142,12 +150,26 @@ protected function getEventDispatcher() { return $this->eventDispatcher; } + protected function getLock(): LockBackendInterface { + if (!$this->lock) { + $this->lock = \Drupal::lock(); + } + return $this->lock; + } + /** * {@inheritdoc} */ public function import() { + $lock_definition = $this->migration->getPluginDefinition()['lock'] ?? []; + if ($use_lock = !empty($lock_definition['use_lock'])) { + if (!$this->lock->acquire($this->migration->id(), $lock_definition['initial_timeout'] ?? 30)) { + $fail = TRUE; + } + } // Only begin the import operation if the migration is currently idle. - if ($this->migration->getStatus() !== MigrationInterface::STATUS_IDLE) { + // Also allow setting lock/use_status to FALSE to bypass this. + if (!empty($fail) || ($this->migration->getStatus() !== MigrationInterface::STATUS_IDLE && ($lock_definition['use_status'] ?? TRUE))) { $this->message->display($this->t('Migration @id is busy with another operation: @status', [ '@id' => $this->migration->id(), @@ -288,6 +310,10 @@ public function import() { } try { + // Extend our lock. + if ($use_lock) { + $this->lock->acquire($this->migration->id(), $lock_definition['timeout'] ?? 30); + } $source->next(); } catch (\Exception $e) {