Skip to content

Commit

Permalink
transfer daemonization logic in executable
Browse files Browse the repository at this point in the history
No need for unix-specific code in agent object.
  • Loading branch information
guillomovitch committed Feb 28, 2013
1 parent 8b5fa7c commit 53b47bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
28 changes: 28 additions & 0 deletions bin/fusioninventory-daemon
Expand Up @@ -4,9 +4,12 @@ use strict;
use warnings;
use lib './lib';

use Cwd;
use English qw(-no_match_vars);
use Getopt::Long;
use Pod::Usage;
use Proc::Daemon;
use Proc::PID::File;

use FusionInventory::Agent;

Expand Down Expand Up @@ -52,6 +55,31 @@ my $agent = FusionInventory::Agent->new(%setup);

eval {
$agent->init(options => $options);
};

if ($EVAL_ERROR) {
print STDERR "Initialisation failure:.\n";
print STDERR $EVAL_ERROR;
exit 1;
}

if ($options->{'no-fork'}) {

my $cwd = getcwd();
Proc::Daemon::Init();

# If we use relative path, we must stay in the current directory
if (substr( $setup{libdir}, 0, 1 ) ne '/') {
chdir($cwd);
}

if (Proc::PID::File->running()) {
print STDERR "An agent is already runnnig, exiting...";
exit 1;
}
}

eval {
$agent->run();
};

Expand Down
41 changes: 0 additions & 41 deletions lib/FusionInventory/Agent.pm
Expand Up @@ -3,7 +3,6 @@ package FusionInventory::Agent;
use strict;
use warnings;

use Cwd;
use English qw(-no_match_vars);
use UNIVERSAL::require;
use File::Glob;
Expand Down Expand Up @@ -98,32 +97,6 @@ sub init {
exit 1;
}

if ($config->{daemon} && !$config->{'no-fork'}) {

$logger->debug("Time to call Proc::Daemon");

Proc::Daemon->require();
if ($EVAL_ERROR) {
$logger->error("Can't load Proc::Daemon. Is the module installed?");
exit 1;
}

my $cwd = getcwd();
Proc::Daemon::Init();
$logger->debug("Daemon started");


# If we use relative path, we must stay in the current directory
if (substr( $params{libdir}, 0, 1 ) ne '/') {
chdir($cwd);
}

if ($self->_isAlreadyRunning()) {
$logger->debug("An agent is already runnnig, exiting...");
exit 1;
}
}

# compute list of allowed tasks
my %available = $self->getAvailableTasks(disabledTasks => $config->{'no-task'});
my @tasks = keys %available;
Expand Down Expand Up @@ -398,20 +371,6 @@ sub _getTaskVersion {
return $version;
}

sub _isAlreadyRunning {
my ($self) = @_;

Proc::PID::File->require();
if ($EVAL_ERROR) {
$self->{logger}->debug(
'Proc::PID::File unavailable, unable to check for running agent'
);
return 0;
}

return Proc::PID::File->running();
}

sub _loadState {
my ($self) = @_;

Expand Down

0 comments on commit 53b47bc

Please sign in to comment.