From 4e034966c2a23368eff026d9f57db5405be3b1dc Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 2 Aug 2024 11:48:41 +0200 Subject: [PATCH] migration lockfile lifetime --- application/config/migration.php | 15 +++++++++++ application/controllers/Migrate.php | 3 ++- system/libraries/Migration.php | 39 +++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/application/config/migration.php b/application/config/migration.php index 03983d214..c19a65279 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -37,5 +37,20 @@ $config['migration_version'] = 211; $config['migration_path'] = APPPATH . 'migrations/'; +/* +|-------------------------------------------------------------------------- +| Migration Lockfile +|-------------------------------------------------------------------------- +| +| Path to the migration lockfile. +| This lockfile prevents migrations from running twice +| +*/ +$config['migration_lockfile'] = sys_get_temp_dir() . '/.migration_running'; + +// the maximum age of the lockfile in seconds +$config['migration_lf_maxage'] = 30; + + /* End of file migration.php */ /* Location: ./application/config/migration.php */ diff --git a/application/controllers/Migrate.php b/application/controllers/Migrate.php index 65bf69dba..d1da26609 100644 --- a/application/controllers/Migrate.php +++ b/application/controllers/Migrate.php @@ -3,6 +3,7 @@ class Migrate extends CI_Controller { public function index() { $this->load->library('Migration'); + $this->load->config('migration'); $result = array(); $latest = $this->migration->latest(); @@ -12,7 +13,7 @@ class Migrate extends CI_Controller { log_message('error', 'Migration failed'); $result['status'] = 'error'; } else { - while (file_exists(APPPATH . 'cache/.migration_running')) { + while (file_exists($this->config->item('migration_lockfile'))) { sleep(1); } $result['status'] = 'success'; diff --git a/system/libraries/Migration.php b/system/libraries/Migration.php index d08f78b5e..b70ea530b 100644 --- a/system/libraries/Migration.php +++ b/system/libraries/Migration.php @@ -91,7 +91,16 @@ class CI_Migration { * * @var string */ - protected $_migration_lockfile = APPPATH . 'cache/.migration_running'; + protected $_migration_lockfile = NULL; + + /** + * Max Age of the migration lockfile + * + * @var int + */ + + protected $_migration_lf_maxage = NULL; + /** * Whether to automatically run migrations @@ -147,6 +156,12 @@ class CI_Migration { // Add trailing slash if not set $this->_migration_path = rtrim($this->_migration_path, '/').'/'; + // If not set, set it + $this->_migration_lockfile !== '' OR $this->_migration_lockfile = '/tmp/.migration_running'; + + // selockfile maxage if not set in config file. Fallback is 60 seconds. + $this->_migration_lf_maxage !== '' OR $this->_migration_lf_maxage = 60; + // Load migration language $this->lang->load('migration'); @@ -341,7 +356,27 @@ class CI_Migration { } } else { - log_message('debug', 'Migration process is currently locked. Second migration attempt ignored.'); + + log_message('debug', 'There is a lockfile for migrations. Checking the age...'); + + // Get the file creation date + $lockfile_ctime = filemtime($this->_migration_lockfile); + + //compare to the current time + $tdiff = time() - $lockfile_ctime; + log_message('debug', 'Migration lockfile lifetime in seconds: '.$tdiff.'/'.$this->_migration_lf_maxage); + + // if the file is older then the configured limit, delete it + if ($tdiff > $this->_migration_lf_maxage) { + + unlink($this->_migration_lockfile); + log_message('debug', 'Deleted migration lockfile because it was older then maxage.'); + + } else { + + log_message('debug', 'Migration process is currently locked. Second migration attempt ignored.'); + + } } return $current_version;