4effbdc6ec82ef3271d70338a1ab60b35dbc840c
[gitmo/MooseX-Daemonize.git] / lib / MooseX / Daemonize / Core.pm
1 package MooseX::Daemonize::Core;
2 use strict;    # because Kwalitee is pedantic
3 use Moose::Role;
4
5 our $VERSION = 0.01;
6
7 use POSIX ();
8
9 has is_daemon => (
10     isa     => 'Bool',
11     is      => 'rw',
12     default => sub { 0 },
13 );
14
15 sub daemon_fork   { fork }
16 sub daemon_detach { 
17     # ignore these signals
18     $SIG{'HUP'} = 'IGNORE';
19     
20     POSIX::setsid;  # set session id
21     chdir '/';      # change to root directory
22     umask 0;        # clear the file creation mask            
23     
24     # get the max numnber of possible file descriptors
25     my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
26     $openmax = 64 if !defined($openmax) || $openmax < 0;
27     
28     # close them all 
29     POSIX::close($_) foreach (0 .. $openmax);
30
31     open(STDIN,  "+>/dev/null");
32     open(STDOUT, "+>&STDIN");
33     open(STDERR, "+>&STDIN");    
34 }
35
36 sub daemonize {
37     my ($self) = @_;
38     return if $self->daemon_fork;
39     $self->daemon_detach;
40     $self->is_daemon(1);
41 }
42
43 1;
44 __END__
45
46 =head1 NAME
47
48 MooseX::Daemonize::Core - provides a Role the core daemonization features
49
50 =head1 VERSION
51
52 =head1 SYNOPSIS
53      
54 =head1 DESCRIPTION
55
56 =head1 ATTRIBUTES
57
58 =over
59
60 =item is_daemon Bool
61
62 If true, the process is the backgrounded process. This is useful for example
63 in an after 'start' => sub { } block
64
65 =back
66
67 =head1 METHODS 
68
69 =over
70
71 =item daemon_fork()
72
73 =item daemon_detach()
74
75 =item daemonize()
76
77 Calls C<Proc::Daemon::Init> to daemonize this process. 
78
79 =item setup_signals()
80
81 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
82
83 =item handle_sigint()
84
85 Handle a INT signal, by default calls C<$self->stop()>
86
87 =item handle_sighup()
88
89 Handle a HUP signal. By default calls C<$self->restart()>
90
91 =item meta()
92
93 The C<meta()> method from L<Class::MOP::Class>
94
95 =back
96
97 =head1 DEPENDENCIES
98
99 =for author to fill in:
100     A list of all the other modules that this module relies upon,
101     including any restrictions on versions, and an indication whether
102     the module is part of the standard Perl distribution, part of the
103     module's distribution, or must be installed separately. ]
104
105 Obviously L<Moose>, and L<Proc::Daemon>
106
107 =head1 INCOMPATIBILITIES
108
109 =for author to fill in:
110     A list of any modules that this module cannot be used in conjunction
111     with. This may be due to name conflicts in the interface, or
112     competition for system or program resources, or due to internal
113     limitations of Perl (for example, many modules that use source code
114     filters are mutually incompatible).
115
116 None reported.
117
118
119 =head1 BUGS AND LIMITATIONS
120
121 =for author to fill in:
122     A list of known problems with the module, together with some
123     indication Whether they are likely to be fixed in an upcoming
124     release. Also a list of restrictions on the features the module
125     does provide: data types that cannot be handled, performance issues
126     and the circumstances in which they may arise, practical
127     limitations on the size of data sets, special cases that are not
128     (yet) handled, etc.
129
130 No bugs have been reported.
131
132 Please report any bugs or feature requests to
133 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
134 L<http://rt.cpan.org>.
135
136 =head1 SEE ALSO
137
138 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
139
140 =head1 AUTHOR
141
142 Chris Prather  C<< <perigrin@cpan.org> >>
143
144 =head1 THANKS
145
146 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the 
147 #moose denzians
148
149 Some bug fixes sponsored by Takkle Inc.
150
151 =head1 LICENCE AND COPYRIGHT
152
153 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights 
154 reserved.
155
156 This module is free software; you can redistribute it and/or
157 modify it under the same terms as Perl itself. See L<perlartistic>.
158
159
160 =head1 DISCLAIMER OF WARRANTY
161
162 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
163 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
164 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
165 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
166 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
167 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
168 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
169 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
170 NECESSARY SERVICING, REPAIR, OR CORRECTION.
171
172 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
173 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
174 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
175 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
176 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
177 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
178 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
179 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
180 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
181 SUCH DAMAGES.