App-Framework

 view release on metacpan or  search on metacpan

lib/App/Framework/Extension/Daemon.pm  view on Meta::CPAN

=cut

sub new
{
	my ($obj, %args) = @_ ;

	my $class = ref($obj) || $obj ;

	## Need Net::Server::Daemonize
	eval "use Net::Server::Daemonize;" ;
	if (@$)
	{
		croak "Sorry. You need to have Net::Server::Daemonize installed to be able to use $class" ;
	}

	## create object dynamically
	my $this = App::Framework::Core->inherit($class, %args) ;

	## Set options
	$this->feature('Options')->append_options(\@OPTIONS) ;
	
	## hi-jack the app function
	$this->extend_fn(
		'app_fn'	=> sub {$this->daemon_run(@_);},
	) ;

	return($this) ;
}



#============================================================================================

=back

=head2 CLASS METHODS

=over 4

=cut

#============================================================================================

#-----------------------------------------------------------------------------

=item B<init_class([%args])>

Initialises the object class variables.

=cut

sub init_class
{
	my $class = shift ;
	my (%args) = @_ ;

	# Add extra fields
	$class->add_fields(\%FIELDS, \%args) ;

	# init class
	$class->SUPER::init_class(%args) ;

}


#============================================================================================

=back

=head2 OBJECT METHODS

=over 4

=cut

#============================================================================================


#----------------------------------------------------------------------------

=item B<daemon_run()>

Daemonize then run the application's app subroutine inside a loop.
 
=cut


sub daemon_run
{
	my $this = shift ;


my $use_net=0;
if ($use_net)
{	

print "Calling daemonize()...\n" ;
	## Daemonize
	Net::Server::Daemonize::daemonize(
	    $this->user,             # User
	    $this->group,            # Group
	    $this->pid,				 # Path to PID file - optional
	);
print "Calling application run...\n" ;
	
	## call application run
	$this->call_extend_fn('app_fn') ;

}

else
{
  ##my $pid = safe_fork();
print "Calling fork()...\n" ;
  my $pid = fork;
  unless( defined $pid ){
    die "Couldn't fork: [$!]\n";
  }


  ### parent process should do the pid file and exit



( run in 0.530 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )