1 package Tak::Daemon::DaemonService;
4 use Log::Contextual ();
5 use Log::Contextual::SimpleLogger;
9 with 'Tak::Role::Service';
11 has 'set_done_cb' => (is => 'rw');
13 has 'router' => (is => 'lazy');
15 sub _build_router { Tak::Router->new }
17 sub handle_daemonize {
20 POSIX::setsid or die "Couldn't setsid: $!";
25 sub handle_become_successor {
28 $self->set_done_cb(sub { $done = 1 });
29 # because this is funnier than "no warnings 'once'". Also because
30 # I don't have to stop and think "what else is in this lexical scope?"
31 $Tak::STDIOSetup::Next = $Tak::STDIOSetup::Next = sub {
33 # have to do this here because when we're being set up stderr may
34 # be redirected (e.g. because we're in an ->do under a repl) - and
35 # plus it leaves logging running until the last possible minute,
36 # which is almost certainly a win anyway.
38 open STDERR, '>', '/dev/null' or die "Couldn't re-open stderr: $!";
39 Log::Contextual::set_logger( # there's no NullLogger? I thought I wrote one
40 Log::Contextual::SimpleLogger->new({ levels => [] })
43 my $x = $self; # close over while evading void context warnings
44 $0 = 'tak-daemon-node';
45 Tak->loop_until($done);
52 $self->set_done_cb->();
56 sub start_router_request {
57 shift->router->start_request(@_);
61 shift->router->receive(@_);