migration lockfile lifetime

This commit is contained in:
github-actions
2024-08-02 11:48:41 +02:00
parent 23a6e231f6
commit 4e034966c2
3 changed files with 54 additions and 3 deletions

View File

@@ -37,5 +37,20 @@ $config['migration_version'] = 211;
$config['migration_path'] = APPPATH . 'migrations/'; $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 */ /* End of file migration.php */
/* Location: ./application/config/migration.php */ /* Location: ./application/config/migration.php */

View File

@@ -3,6 +3,7 @@ class Migrate extends CI_Controller {
public function index() { public function index() {
$this->load->library('Migration'); $this->load->library('Migration');
$this->load->config('migration');
$result = array(); $result = array();
$latest = $this->migration->latest(); $latest = $this->migration->latest();
@@ -12,7 +13,7 @@ class Migrate extends CI_Controller {
log_message('error', 'Migration failed'); log_message('error', 'Migration failed');
$result['status'] = 'error'; $result['status'] = 'error';
} else { } else {
while (file_exists(APPPATH . 'cache/.migration_running')) { while (file_exists($this->config->item('migration_lockfile'))) {
sleep(1); sleep(1);
} }
$result['status'] = 'success'; $result['status'] = 'success';

View File

@@ -91,7 +91,16 @@ class CI_Migration {
* *
* @var string * @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 * Whether to automatically run migrations
@@ -147,6 +156,12 @@ class CI_Migration {
// Add trailing slash if not set // Add trailing slash if not set
$this->_migration_path = rtrim($this->_migration_path, '/').'/'; $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 // Load migration language
$this->lang->load('migration'); $this->lang->load('migration');
@@ -341,7 +356,27 @@ class CI_Migration {
} }
} else { } 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; return $current_version;