Skip Menu |
 

This queue is for tickets about the MooseX-Daemonize CPAN distribution.

Report information
The Basics
Id: 67016
Status: resolved
Priority: 0/
Queue: MooseX-Daemonize

People
Owner: stevan.little [...] gmail.com
Requestors: CALDRIN [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in:
  • 0.08
  • 0.12
Fixed in: (no value)



Subject: MooseX::Daemonize does not work with Log4perl
Download (untitled) / with headers
text/plain 638b
When using Log::Log4perl to log in a MooseX::Daemonize daemon the daemon does not start. Furthermore, I sometimes find log messages in the pid file even though they have different names. The attached module and start file explains the issue. When running it, I'd expect the daemon to start, write "Started daemon" to the log file, write "Log 1", "Log 2", "Log 3" to the logfile and then continue to run until /tmp/stopit exists. Instead it starts, writes "Started daemon" and then disappears without further notice. I consider this a critical issue since Log4perl is probably one of the most important ways to log from a daemon.
Subject: exp-daemon
Download exp-daemon
application/octet-stream 197b

Message body not shown because it is not plain text.

Subject: Daemon.pm
Download Daemon.pm
text/x-perl 1.2k
package Exp::Daemon; use warnings; use strict; use Moose; with 'MooseX::Daemonize'; with 'MooseX::Log::Log4perl'; after start => sub { my ($self) = @_; return unless $self->is_daemon; $self->log->error("Log 1"); $self->log->error("Log 2"); $self->log->error("Log 3"); while (not -e '/tmp/stopit') { sleep 5; } }; sub run { my ($self) = @_; my ($command) = @{$self->extra_argv}; defined $command || die "No command specified"; $self->status if $command eq 'status'; $self->restart if $command eq 'restart'; $self->stop if $command eq 'stop'; # $self->start if $command eq 'start'; if ($command eq 'start') { use Log::Log4perl; my $string = ' log4perl.rootLogger = ALL, file log4perl.appender.file = Log::Log4perl::Appender::File log4Perl.appender.file.filename = /tmp/exp_daemon_logfile log4perl.appender.file.layout = PatternLayout log4perl.appender.file.layout.ConversionPattern = %d %p %c - %m in %F{2} (%L)%n '; Log::Log4perl->init(\$string); $self->log->info("Started daemon"); $self->start; } } 1; # End of Exp::Daemon
Download (untitled) / with headers
text/plain 740b
On Mon Mar 28 06:37:53 2011, CALDRIN wrote: Show quoted text
> When using Log::Log4perl to log in a MooseX::Daemonize daemon the
daemon Show quoted text
> does not start. Furthermore, I sometimes find log messages in the pid > file even though they have different names. > > The attached module and start file explains the issue. When running
it, Show quoted text
> I'd expect the daemon to start, write "Started daemon" to the log
file, Show quoted text
> write "Log 1", "Log 2", "Log 3" to the logfile and then continue to
run Show quoted text
> until /tmp/stopit exists. > > Instead it starts, writes "Started daemon" and then disappears without > further notice. > > I consider this a critical issue since Log4perl is probably one of the > most important ways to log from a daemon.
I have the same problem.
So, the problem is that by default MooseX::Daemonize::Core will close all open filehandles (see the &detach_daemon method). Since you are initializing Log::Log4Perl outside of the daemon code (in &run) when it actually gets to the log statements in the daemon, the log file handle has been closed. So there are two workarounds here (and I have tested both and they both work): 1) You can wait and initialize the Log::Log4perl in the daemon (after the call to &is_daemon) 2) You can use the 'dont_close_all_files' option either from the command line or in your .sh script, and this will disable this file closing behavior and therefore leave the Log::Log4perl file handle intact. Ideally we should have some docs that explain this, if you would care to submit a patch to do just that, I would happily accept the patch and upload a new version to CPAN (complete with credits). But at this point in time, I do not use this module all that much anymore and so I cannot say when I will get around to actually writing up those docs myself. Thanks, - Stevan
Download (untitled) / with headers
text/plain 266b
STEVAN wrote: Show quoted text
> Ideally we should have some docs that explain this, if you would care > to submit a patch to do just that,
Sure, please find a patch attached. Please let me know if that is what you expected or where you would like to see changes. so long Maik
Subject: MooseX-Daemonize-0.12-logging-docu.patch
diff -ur MooseX-Daemonize-0.12.old/lib/MooseX/Daemonize.pm MooseX-Daemonize-0.12/lib/MooseX/Daemonize.pm --- MooseX-Daemonize-0.12.old/lib/MooseX/Daemonize.pm 2011-06-06 08:12:44.497535239 +0200 +++ MooseX-Daemonize-0.12/lib/MooseX/Daemonize.pm 2011-06-06 09:48:04.937460270 +0200 @@ -523,6 +523,28 @@ L<Moose>, L<MooseX::Getopt>, L<MooseX::Types::Path::Class> and L<POSIX> +=head1 CAVEATS + +When going into background MooseX::Daemonize closes all open file +handles. This may interfere with you logging because it may also close the log +file handle you want to write to. To prevent this you can either defer opening +the log file until after start. Alternatively, use can use the +'dont_close_all_files' option either from the command line or in your .sh +script. + +Assuming you want to use Log::Log4perl for example you could expand the +MooseX::Daemonize example above like this. + + after start => sub { + my $self = shift; + return unless $self->is_daemon; + Log::Log4perl->init(\$log4perl_config); + my $logger = Log::Log4perl->get_logger(); + $logger->info("Daemon started"); + # your daemon code here ... + }; + + =head1 INCOMPATIBILITIES None reported. Although obviously this will not work on Windows.
Patch applied and uploaded to CPAN as 0.13 Thanks!
The reported issue is not a bug. Since it is documented I consider this fixed.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.