Merge pull request #681 from HB9HIL/miglock

Migration Lockfile
This commit is contained in:
HB9HIL
2024-08-06 11:50:10 +02:00
committed by GitHub
5 changed files with 66 additions and 12 deletions

View File

@@ -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 */

View File

@@ -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;

View File

@@ -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';

View File

@@ -40,14 +40,15 @@
<td><?php echo (isset($migration_version) ? $migration_version : "<span class='badge text-bg-danger'>". __("There is something wrong with your Migration in Database!") . "</span>"); ?></td>
</tr>
<?php if (!$migration_is_uptodate) { ?>
</table>
<div class="alert alert-danger mt-3 mb-3">
<h5><?= __("Migration is outdated and locked!"); ?></h5>
<p><?= sprintf(__("The current migration is not the version it is supposed to be. Reload this page. If this warning persists, your migration is probably locked due to a past failed process. Check the folder %s for a file called %s and remove this file to force the migration to run again."), "'application/cache/'", "'.migration_running'"); ?></p>
<p><?= sprintf(__("Current migration is %s"), $migration_version); ?><br>
<?= sprintf(__("Migration should be %s"), $migration_config); ?></p>
</div>
<table>
</table>
<div class="alert alert-danger mt-3 mb-3">
<h5><?= __("Migration is outdated and locked!"); ?></h5>
<p><?= sprintf(__("The current migration is not the version it is supposed to be. Reload this page after %s seconds. If this warning persists, your migration is likely locked due to a previously failed process. Delete the file %s to force the migration to run again."), $miglock_lifetime, $migration_lockfile); ?></p>
<p><?= sprintf(__("Check this wiki article <u><a href='%s' target='_blank'>here</a></u> for more information."), "https://github.com/wavelog/wavelog/wiki/Migration-is-locked"); ?></p>
<p><?= sprintf(__("Current migration is %s"), $migration_version); ?><br>
<?= sprintf(__("Migration should be %s"), $migration_config); ?></p>
</div>
<table>
<?php } ?>
<tr>
<td><?= __("Environment"); ?></td>

View File

@@ -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;