diff --git a/application/config/migration.php b/application/config/migration.php index 03983d214..d0dbce0ab 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'] = 300; + + /* End of file migration.php */ /* Location: ./application/config/migration.php */ diff --git a/application/controllers/Debug.php b/application/controllers/Debug.php index 54115cb98..e9af0827a 100644 --- a/application/controllers/Debug.php +++ b/application/controllers/Debug.php @@ -42,9 +42,11 @@ class Debug extends CI_Controller // get mig version from config file $this->load->config('migration'); $data['migration_config'] = $this->config->item('migration_version'); + $data['migration_lockfile'] = $this->config->item('migration_lockfile'); + $data['miglock_lifetime'] = $this->config->item('migration_lf_maxage'); // compare mig versions - if ($data['migration_version'] !== $data['migration_config'] && file_exists('application/cache/.migration_running')) { + if ($data['migration_version'] != $data['migration_config'] && file_exists($data['migration_lockfile'])) { $data['migration_is_uptodate'] = false; } else { $data['migration_is_uptodate'] = true; 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/application/views/debug/index.php b/application/views/debug/index.php index 22ec83cce..9e0642e2e 100644 --- a/application/views/debug/index.php +++ b/application/views/debug/index.php @@ -40,14 +40,15 @@ ". __("There is something wrong with your Migration in Database!") . ""); ?> - -
-
-

-


-

-
- +
+
+
+

+

here for more information."), "https://github.com/wavelog/wavelog/wiki/Migration-is-locked"); ?>

+


+

+
+ diff --git a/system/libraries/Migration.php b/system/libraries/Migration.php index d08f78b5e..256748747 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 480 seconds. + $this->_migration_lf_maxage !== '' OR $this->_migration_lf_maxage = 480; + // 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;