Merge branch 'ent-9201-mergeador-bloqueo-pandora_db' into 'develop'

add check locked bbdd in merge process pandora_enterprise#9201

See merge request artica/pandorafms!5041
This commit is contained in:
Diego Muñoz-Reja 2022-08-24 10:29:30 +00:00
commit 6edd361142
2 changed files with 42 additions and 2 deletions

View File

@ -1543,14 +1543,20 @@ sub db_insert_get_values ($) {
######################################################################## ########################################################################
## Try to obtain the given lock. ## Try to obtain the given lock.
######################################################################## ########################################################################
sub db_get_lock($$;$) { sub db_get_lock($$;$$) {
my ($dbh, $lock_name, $lock_timeout) = @_; my ($dbh, $lock_name, $lock_timeout, $do_not_wait_lock) = @_;
# Only supported in MySQL. # Only supported in MySQL.
return 1 unless ($RDBMS eq 'mysql'); return 1 unless ($RDBMS eq 'mysql');
# Set a default lock timeout of 1 second # Set a default lock timeout of 1 second
$lock_timeout = 1 if (! defined ($lock_timeout)); $lock_timeout = 1 if (! defined ($lock_timeout));
if ($do_not_wait_lock) {
if (!db_is_free_lock($dbh, $lock_name)) {
return 0;
}
}
# Attempt to get the lock! # Attempt to get the lock!
my $sth = $dbh->prepare('SELECT GET_LOCK(?, ?)'); my $sth = $dbh->prepare('SELECT GET_LOCK(?, ?)');
@ -1563,6 +1569,26 @@ sub db_get_lock($$;$) {
return $lock; return $lock;
} }
########################################################################
## Check is lock is free.
########################################################################
sub db_is_free_lock($$) {
my ($dbh, $lock_name) = @_;
# Only supported in MySQL.
return 1 unless ($RDBMS eq 'mysql');
# Attempt to get the lock!
my $sth = $dbh->prepare('SELECT IS_FREE_LOCK(?)');
$sth->execute($lock_name);
my ($lock) = $sth->fetchrow;
# Something went wrong
return 0 if (! defined ($lock));
return $lock;
}
######################################################################## ########################################################################
## Release the given lock. ## Release the given lock.
######################################################################## ########################################################################

View File

@ -1193,6 +1193,20 @@ if ($conf{'_force'} == 1) {
db_release_pandora_lock($dbh, $lock_name, $LOCK_TIMEOUT); db_release_pandora_lock($dbh, $lock_name, $LOCK_TIMEOUT);
} }
# Get a lock merging.
my $lock_merge = db_get_lock ($dbh, 'merge-working', $LOCK_TIMEOUT, 1);
if ($lock_merge == 0) {
log_message ('', " [*] Merge is running.\n\n");
exit 1;
}
# Get a lock on merging events.
my $lock_merge_events = db_get_lock ($dbh, 'merging-events', $LOCK_TIMEOUT, 1);
if ($lock_merge_events == 0) {
log_message ('', " [*] Merge events is running.\n\n");
exit 1;
}
# Get a lock on dbname. # Get a lock on dbname.
my $lock = db_get_pandora_lock ($dbh, $lock_name, $LOCK_TIMEOUT); my $lock = db_get_pandora_lock ($dbh, $lock_name, $LOCK_TIMEOUT);
if ($lock == 0) { if ($lock == 0) {