From e3eda0cb3b1577333c7dfdde3222b8adef17b2ed Mon Sep 17 00:00:00 2001 From: Alexander Lutay Date: Wed, 27 Nov 2013 10:17:42 +0100 Subject: [PATCH] MT#5027 Speed up 'build' runs by using Perl daemon --- Makefile | 4 +- debian/ngcp-ngcpcfg.install | 1 + helper/build_config | 4 +- helper/tt2-daemon | 76 +++++++++++++++++++++++++++++++++++++ helper/tt2-wrapper | 27 ++----------- scripts/build | 20 ++++++++++ 6 files changed, 104 insertions(+), 28 deletions(-) create mode 100755 helper/tt2-daemon diff --git a/Makefile b/Makefile index 5ed0f947..95856164 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ # for syntax checks -BASH_SCRIPTS = scripts/* functions/* etc/ngcp-config/ngcpcfg.cfg helper/build_config sbin/ngcpcfg +BASH_SCRIPTS = scripts/* functions/* etc/ngcp-config/ngcpcfg.cfg helper/build_config sbin/ngcpcfg helper/tt2-wrapper PERL_SCRIPTS = helper/sort-yml \ helper/sync-db \ - helper/tt2-wrapper \ + helper/tt2-daemon \ helper/validate-yml helper/fileformat_version \ sbin/ngcp-network \ sbin/ngcp-sync-constants diff --git a/debian/ngcp-ngcpcfg.install b/debian/ngcp-ngcpcfg.install index 3477af88..e1831519 100644 --- a/debian/ngcp-ngcpcfg.install +++ b/debian/ngcp-ngcpcfg.install @@ -6,6 +6,7 @@ helper/fileformat_version usr/share/ngcp-ngcpcfg/helper/ helper/sort-yml usr/share/ngcp-ngcpcfg/helper/ helper/sync-db usr/share/ngcp-ngcpcfg/helper/ helper/tt2-wrapper usr/share/ngcp-ngcpcfg/helper/ +helper/tt2-daemon usr/share/ngcp-ngcpcfg/helper/ helper/validate-yml usr/share/ngcp-ngcpcfg/helper/ lib/* usr/lib/ngcp-ngcpcfg/ sbin/ngcp-network usr/sbin/ diff --git a/helper/build_config b/helper/build_config index 525ddf87..a7de5a94 100755 --- a/helper/build_config +++ b/helper/build_config @@ -58,8 +58,8 @@ umask 0077 TT_WRAPPER="${HELPER}/tt2-wrapper" log_debug "Output file ${output_file} based on ${input_file}" -log_debug "Executing: $TT_WRAPPER ${input_file} ${host_conf:-} ${local_conf:-} $NGCPCTL_CONFIG ${NETWORK_CONFIG:-} ${EXTRA_CONFIG_FILES:-} $CONSTANTS_CONFIG > ${output_file}" -if "$TT_WRAPPER" "${input_file}" ${host_conf:-} ${local_conf:-} "$NGCPCTL_CONFIG" "${NETWORK_CONFIG:-}" ${EXTRA_CONFIG_FILES:-} "$CONSTANTS_CONFIG" > "${output_file}" 2>/dev/null ; then +log_debug "Executing: $TT_WRAPPER ${input_file} > ${output_file}" +if "$TT_WRAPPER" "${input_file}" > "${output_file}" 2>/dev/null ; then log_info "Generating ${output_file}: OK" RC=0 else diff --git a/helper/tt2-daemon b/helper/tt2-daemon new file mode 100755 index 00000000..2ddef080 --- /dev/null +++ b/helper/tt2-daemon @@ -0,0 +1,76 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Carp; +use POSIX qw( setsid ); +use IO::Socket; +use Hash::Merge qw(merge); +use Template; +use YAML qw/LoadFile/; + +my $server_port = get_server_port(); + +daemonize(); + +handle_connections( $server_port ); + +exit; + +sub daemonize { + chdir '/' or croak "Can't chdir to /: $!"; + open STDIN, '/dev/null' or croak "Can't read /dev/null: $!"; + open STDOUT, '>/dev/null' or croak "Can't write to /dev/null: $!"; + defined(my $pid = fork) or croak "Can't fork: $!"; + exit if $pid; + setsid or croak "Can't start a new session: $!"; + open STDERR, '>&STDOUT' or croak "Can't dup stdout: $!"; +} + +sub get_server_port { + my $server = IO::Socket::INET->new( + 'Proto' => 'tcp', + 'LocalPort' => 42042, + 'Listen' => SOMAXCONN, + 'Reuse' => 1, + ); + die "can't setup server" unless $server; + + binmode $server => ":encoding(utf8)"; + + return $server; +} + +sub handle_connections { + my $port = shift; + my $config = {}; + my %loaded_ymls = (); + my $reuse; + + while ( my $client = $port->accept() ) { + chomp ( my $input = <$client> ); + + my $tt = Template->new({ ABSOLUTE => 1, RELATIVE => 1, EVAL_PERL => 1 }); + my @ARGV = split(' ', $input) or + print $client "Usage: echo '