From d5675c1bb30375f04e1a187f553f85979934b07a Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 7 Aug 2014 18:50:08 +0100 Subject: [PATCH] portable setting of proctitle --- Netdisco/MANIFEST | 1 + Netdisco/META.yml | 5 +++++ Netdisco/Makefile.PL | 5 ++++- Netdisco/bin/netdisco-daemon-fg | 6 +++--- .../lib/App/Netdisco/Daemon/Worker/Common.pm | 8 +++---- .../lib/App/Netdisco/Daemon/Worker/Manager.pm | 6 +++--- .../App/Netdisco/Daemon/Worker/Scheduler.pm | 6 +++--- Netdisco/lib/App/Netdisco/Util/Daemon.pm | 21 +++++++++++++++++++ 8 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 Netdisco/lib/App/Netdisco/Util/Daemon.pm diff --git a/Netdisco/MANIFEST b/Netdisco/MANIFEST index e44e74a2..440a37c1 100644 --- a/Netdisco/MANIFEST +++ b/Netdisco/MANIFEST @@ -150,6 +150,7 @@ lib/App/Netdisco/Manual/Deployment.pod lib/App/Netdisco/Manual/Developing.pod lib/App/Netdisco/Manual/ReleaseNotes.pod lib/App/Netdisco/Manual/WritingPlugins.pod +lib/App/Netdisco/Util/Daemon.pm lib/App/Netdisco/Util/Device.pm lib/App/Netdisco/Util/DNS.pm lib/App/Netdisco/Util/ExpandParams.pm diff --git a/Netdisco/META.yml b/Netdisco/META.yml index 6d596879..f1122a26 100644 --- a/Netdisco/META.yml +++ b/Netdisco/META.yml @@ -19,6 +19,9 @@ no_index: - inc - share - t +recommends: + Graph: 0 + GraphViz: 0 requires: Algorithm::Cron: 0.07 AnyEvent: 7.05 @@ -36,6 +39,7 @@ requires: Dancer::Plugin::Auth::Extensible: 0.3 Dancer::Plugin::DBIC: 0.2001 Dancer::Plugin::Passphrase: 2.0.1 + Data::Printer: 0 File::ShareDir: 1.03 File::Slurp: 9999.19 Guard: 1.022 @@ -45,6 +49,7 @@ requires: List::MoreUtils: 0.33 MCE: 1.408 MIME::Base64: 3.13 + Module::Find: 0.12 Module::Load: 0.32 Moo: 1.001 Net::DNS: 0.72 diff --git a/Netdisco/Makefile.PL b/Netdisco/Makefile.PL index 9be1444d..7f93e261 100644 --- a/Netdisco/Makefile.PL +++ b/Netdisco/Makefile.PL @@ -48,7 +48,6 @@ requires 'Socket6' => 0.23; requires 'Starman' => 0.4008; requires 'SNMP::Info' => 3.18; requires 'SQL::Translator' => 0.11016; -requires 'Sys::Proctitle' => 0; requires 'Template' => 2.24; requires 'Template::Plugin::CSV' => 0.04; requires 'Template::Plugin::Number::Format' => 1.02; @@ -62,6 +61,10 @@ requires 'YAML::XS' => 0.41; requires 'namespace::clean' => 0.24; requires 'version' => 0.9902; +if ( $^O eq 'linux' ) { + requires 'Sys::Proctitle' => 0; +} + recommends 'Graph' => 0; recommends 'GraphViz' => 0; diff --git a/Netdisco/bin/netdisco-daemon-fg b/Netdisco/bin/netdisco-daemon-fg index a987fead..a1955116 100755 --- a/Netdisco/bin/netdisco-daemon-fg +++ b/Netdisco/bin/netdisco-daemon-fg @@ -6,7 +6,6 @@ use warnings; use FindBin; FindBin::again(); use Path::Class 'dir'; -use Sys::Proctitle 'setproctitle'; BEGIN { # stuff useful locations into @INC @@ -24,6 +23,7 @@ warning sprintf "App::Netdisco %s backend", ($App::Netdisco::VERSION || 'HEAD'); # local job queue management use App::Netdisco::Daemon::LocalQueue ':all'; +use App::Netdisco::Util::Daemon; # needed to quench AF_INET6 symbol errors use NetAddr::IP::Lite ':lower'; @@ -38,7 +38,7 @@ my $tmp_dir = ($ENV{NETDISCO_TEMP} || dir($home, 'tmp')); mkdir $tmp_dir if ! -d $tmp_dir; setpgrp(0,0); # only portable variety of setpgrp -setproctitle 'netdisco-daemon: master'; +prctl 'netdisco-daemon: master'; my $mce = MCE->new( spawn_delay => 0.15, @@ -95,7 +95,7 @@ sub worker_factory { return sub { my $self = shift; my $wid = $self->wid; - setproctitle sprintf 'netdisco-daemon: worker #%s %s: init', $wid, lc($role); + prctl sprintf 'netdisco-daemon: worker #%s %s: init', $wid, lc($role); info "applying role $role to worker $wid"; # $self->sendto('stderr', ">>> worker $wid starting with role $role\n"); diff --git a/Netdisco/lib/App/Netdisco/Daemon/Worker/Common.pm b/Netdisco/lib/App/Netdisco/Daemon/Worker/Common.pm index e2721030..55c7f00d 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/Worker/Common.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/Worker/Common.pm @@ -3,7 +3,7 @@ package App::Netdisco::Daemon::Worker::Common; use Dancer qw/:moose :syntax :script/; use Try::Tiny; -use Sys::Proctitle 'setproctitle'; +use App::Netdisco::Util::Daemon; use Role::Tiny; use namespace::clean; @@ -18,7 +18,7 @@ sub worker_body { my $type = $self->worker_type; while (1) { - setproctitle sprintf 'netdisco-daemon: worker #%s %s: idle', $wid, lc($type); + prctl sprintf 'netdisco-daemon: worker #%s %s: idle', $wid, lc($type); my $jobs = jq_take($self->wid, $type); foreach my $job (@$jobs) { @@ -26,7 +26,7 @@ sub worker_body { try { $job->started(scalar localtime); - setproctitle sprintf 'netdisco-daemon: worker #%s %s: working on #%s: %s', + prctl sprintf 'netdisco-daemon: worker #%s %s: working on #%s: %s', $wid, lc($type), $job->id, $job->summary; info sprintf "$tag (%s): starting %s job(%s) at %s", $wid, $target, $job->id, $job->started; @@ -51,7 +51,7 @@ sub close_job { my $type = $self->worker_type; my $now = scalar localtime; - setproctitle sprintf 'netdisco-daemon: worker #%s %s: wrapping up %s #%s: %s', + prctl sprintf 'netdisco-daemon: worker #%s %s: wrapping up %s #%s: %s', $self->wid, lc($type), $job->action, $job->id, $job->status; info sprintf "$tag (%s): wrapping up %s job(%s) - status %s at %s", $self->wid, $job->action, $job->id, $job->status, $now; diff --git a/Netdisco/lib/App/Netdisco/Daemon/Worker/Manager.pm b/Netdisco/lib/App/Netdisco/Daemon/Worker/Manager.pm index 4dc6eee9..10f19928 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/Worker/Manager.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/Worker/Manager.pm @@ -3,7 +3,7 @@ package App::Netdisco::Daemon::Worker::Manager; use Dancer qw/:moose :syntax :script/; use List::Util 'sum'; -use Sys::Proctitle 'setproctitle'; +use App::Netdisco::Util::Daemon; use Role::Tiny; use namespace::clean; @@ -42,7 +42,7 @@ sub worker_body { while (1) { debug "mgr ($wid): getting potential jobs for $num_slots workers"; - setproctitle sprintf 'netdisco-daemon: worker #%s manager: gathering', $wid; + prctl sprintf 'netdisco-daemon: worker #%s manager: gathering', $wid; # get some pending jobs # TODO also check for stale jobs in Netdisco DB @@ -65,7 +65,7 @@ sub worker_body { } debug "mgr ($wid): sleeping now..."; - setproctitle sprintf 'netdisco-daemon: worker #%s manager: idle', $wid; + prctl sprintf 'netdisco-daemon: worker #%s manager: idle', $wid; sleep( setting('workers')->{sleep_time} || 2 ); } } diff --git a/Netdisco/lib/App/Netdisco/Daemon/Worker/Scheduler.pm b/Netdisco/lib/App/Netdisco/Daemon/Worker/Scheduler.pm index 81a498e4..f7252ca3 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/Worker/Scheduler.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/Worker/Scheduler.pm @@ -3,7 +3,7 @@ package App::Netdisco::Daemon::Worker::Scheduler; use Dancer qw/:moose :syntax :script/; use Algorithm::Cron; -use Sys::Proctitle 'setproctitle'; +use App::Netdisco::Util::Daemon; use Role::Tiny; use namespace::clean; @@ -38,11 +38,11 @@ sub worker_body { # sleep until some point in the next minute my $naptime = 60 - (time % 60) + int(rand(45)); - setproctitle sprintf 'netdisco-daemon: worker #%s scheduler: idle', $wid; + prctl sprintf 'netdisco-daemon: worker #%s scheduler: idle', $wid; debug "sched ($wid): sleeping for $naptime seconds"; sleep $naptime; - setproctitle sprintf 'netdisco-daemon: worker #%s scheduler: queueing', $wid; + prctl sprintf 'netdisco-daemon: worker #%s scheduler: queueing', $wid; # NB next_time() returns the next *after* win_start my $win_start = time - (time % 60) - 1; diff --git a/Netdisco/lib/App/Netdisco/Util/Daemon.pm b/Netdisco/lib/App/Netdisco/Util/Daemon.pm new file mode 100644 index 00000000..4b0bf175 --- /dev/null +++ b/Netdisco/lib/App/Netdisco/Util/Daemon.pm @@ -0,0 +1,21 @@ +package App::Netdisco::Util::Daemon; + +use strict; +use warnings; + +use Module::Load (); +eval { Module::Load::load 'Sys::Proctitle' }; + +use base 'Exporter'; +our @EXPORT = 'prctl'; + +sub prctl { + if ($^O eq 'linux') { + Sys::Proctitle::setproctitle(shift); + } + else { + $0 = shift; + } +} + +1;