140 lines
3.1 KiB
Perl
140 lines
3.1 KiB
Perl
package Common;
|
|
|
|
require Exporter;
|
|
@ISA = qw(Exporter);
|
|
@EXPORT_OK = qw(check_running debug set_debug get_slave get_master get_tmp_file);
|
|
|
|
use warnings;
|
|
use strict;
|
|
use English;
|
|
# requires dbi lib, for ubuntu use apt install libdbd-mysql-perl libdbi-perl
|
|
use DBI;
|
|
use Sys::Hostname;
|
|
# requires yaml lib, for ubuntu use apt install libyaml-libyaml-perl
|
|
use YAML::XS 'LoadFile';
|
|
|
|
my $hostname = hostname;
|
|
my $config = LoadFile('batch_config.yaml');
|
|
|
|
##################################################################
|
|
# This module contains common subroutines used by our batch
|
|
# scripts.
|
|
#
|
|
# Changes:
|
|
# 2016-08-23 (bfr)
|
|
# - added method get_tmp()
|
|
# 2016-08-16 (dzo)
|
|
# - switch config from ENV to YAML file
|
|
# 2011-06-27 (rja)
|
|
# - initial version
|
|
# - copied methods am_i_running(), and debug()
|
|
# - added methods check_running(), set_debug()
|
|
# - added variable $DEBUG
|
|
##################################################################
|
|
|
|
my $DEBUG = 0; # 1 = on, 0 = off
|
|
|
|
#
|
|
# returns a connection using the settings provided in the config file (key: 'slave')
|
|
#
|
|
sub get_slave {
|
|
return get_connection("slave");
|
|
}
|
|
|
|
#
|
|
# returns a connection using the settings provided in the config file (key 'master')
|
|
#
|
|
sub get_master {
|
|
return get_connection("master");
|
|
}
|
|
|
|
#
|
|
# returns tmp dir
|
|
#
|
|
sub get_tmp_file {
|
|
my $tmp_file = shift;
|
|
return $config->{'tmp'} . "/" . $tmp_file;
|
|
}
|
|
|
|
|
|
#
|
|
# returns a connection using the configured environment variables
|
|
#
|
|
# arguments:
|
|
# host - name of the config key (either "master" or "slave")
|
|
#
|
|
sub get_connection {
|
|
my $host = shift;
|
|
return DBI->connect(
|
|
"DBI:mysql:" .
|
|
"database=" . $config->{$host}->{"db"} . ";" .
|
|
"host=" . $config->{$host}->{"host"} . ":" . $config->{$host}->{"port"} . ";" .
|
|
"mysql_socket=" . $config->{$host}->{"sock"},
|
|
#"mysql_socket=",
|
|
$config->{$host}->{"user"},
|
|
$config->{$host}->{"pass"},
|
|
{
|
|
RaiseError => 1,
|
|
AutoCommit => 0,
|
|
"mysql_enable_utf8" => 1,
|
|
"mysql_auto_reconnect" => 1
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
sub check_running {
|
|
my $name = ${PROGRAM_NAME};
|
|
$name =~ s|.*/||;
|
|
if (am_i_running(get_tmp_file("${name}.pid"))) {
|
|
print STDERR "another instance of $PROGRAM_NAME is running on $hostname. Aborting this job.\n";
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
# INPUT: location of lockfile
|
|
# OUTPUT: 1, if a lockfile exists and a program with the pid inside
|
|
# the lockfile is running
|
|
# 0, if no lockfile exists or the program with the pid inside
|
|
# the lockfile is NOT running; resets the pid in the pid
|
|
# to the current pid
|
|
sub am_i_running {
|
|
my $LOCKFILE = shift;
|
|
my $PIDD;
|
|
|
|
if (open (FILE, "<$LOCKFILE")) {
|
|
while (<FILE>) {
|
|
$PIDD = $_;
|
|
}
|
|
|
|
close (FILE);
|
|
chomp($PIDD);
|
|
if (kill(0,$PIDD)) {
|
|
return 1;
|
|
}
|
|
}
|
|
open (FILE, ">$LOCKFILE") or die "Could not open lockfile $LOCKFILE: $!\n";
|
|
print FILE $$;
|
|
close (FILE);
|
|
return 0;
|
|
}
|
|
|
|
# 1 = on, 0 = off
|
|
sub set_debug {
|
|
$DEBUG = shift;
|
|
}
|
|
|
|
# logs statements if debugging is enabled
|
|
sub debug {
|
|
my $msg = shift;
|
|
if ($DEBUG) {
|
|
my $date = `date +"%Y-%m-%d %H:%M:%S"`;
|
|
chomp($date);
|
|
print STDERR "$date: $msg\n";
|
|
}
|
|
}
|
|
|
|
1;
|
|
|