From 941308698de7a6ae712d45da12f150dcbfad70a5 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 7 Aug 2014 16:46:43 +0000 Subject: [PATCH] use Sys::Proctitle instead of Perl $0 --- Netdisco/Makefile.PL | 1 + Netdisco/bin/netdisco-daemon-fg | 5 +++-- Netdisco/lib/App/Netdisco/Daemon/Worker/Common.pm | 8 +++++--- Netdisco/lib/App/Netdisco/Daemon/Worker/Manager.pm | 8 +++++--- Netdisco/lib/App/Netdisco/Daemon/Worker/Scheduler.pm | 6 ++++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Netdisco/Makefile.PL b/Netdisco/Makefile.PL index ce6ace9a..9be1444d 100644 --- a/Netdisco/Makefile.PL +++ b/Netdisco/Makefile.PL @@ -48,6 +48,7 @@ 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; diff --git a/Netdisco/bin/netdisco-daemon-fg b/Netdisco/bin/netdisco-daemon-fg index 86dea474..a987fead 100755 --- a/Netdisco/bin/netdisco-daemon-fg +++ b/Netdisco/bin/netdisco-daemon-fg @@ -6,6 +6,7 @@ use warnings; use FindBin; FindBin::again(); use Path::Class 'dir'; +use Sys::Proctitle 'setproctitle'; BEGIN { # stuff useful locations into @INC @@ -37,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 -$0 = 'netdisco-daemon: master'; +setproctitle 'netdisco-daemon: master'; my $mce = MCE->new( spawn_delay => 0.15, @@ -94,7 +95,7 @@ sub worker_factory { return sub { my $self = shift; my $wid = $self->wid; - $0 = sprintf 'netdisco-daemon: worker #%s %s: init', $wid, lc($role); + setproctitle 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 b32901d3..e2721030 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/Worker/Common.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/Worker/Common.pm @@ -1,7 +1,9 @@ package App::Netdisco::Daemon::Worker::Common; use Dancer qw/:moose :syntax :script/; + use Try::Tiny; +use Sys::Proctitle 'setproctitle'; use Role::Tiny; use namespace::clean; @@ -16,7 +18,7 @@ sub worker_body { my $type = $self->worker_type; while (1) { - $0 = sprintf 'netdisco-daemon: worker #%s %s: idle', $wid, lc($type); + setproctitle sprintf 'netdisco-daemon: worker #%s %s: idle', $wid, lc($type); my $jobs = jq_take($self->wid, $type); foreach my $job (@$jobs) { @@ -24,7 +26,7 @@ sub worker_body { try { $job->started(scalar localtime); - $0 = sprintf 'netdisco-daemon: worker #%s %s: working on #%s: %s', + setproctitle 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; @@ -49,7 +51,7 @@ sub close_job { my $type = $self->worker_type; my $now = scalar localtime; - $0 = sprintf 'netdisco-daemon: worker #%s %s: wrapping up %s #%s: %s', + setproctitle 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 2e5ad7be..4dc6eee9 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/Worker/Manager.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/Worker/Manager.pm @@ -2,10 +2,12 @@ package App::Netdisco::Daemon::Worker::Manager; use Dancer qw/:moose :syntax :script/; +use List::Util 'sum'; +use Sys::Proctitle 'setproctitle'; + use Role::Tiny; use namespace::clean; -use List::Util 'sum'; use App::Netdisco::JobQueue qw/jq_locked jq_getsome jq_lock/; sub worker_begin { @@ -40,7 +42,7 @@ sub worker_body { while (1) { debug "mgr ($wid): getting potential jobs for $num_slots workers"; - $0 = sprintf 'netdisco-daemon: worker #%s manager: gathering', $wid; + setproctitle sprintf 'netdisco-daemon: worker #%s manager: gathering', $wid; # get some pending jobs # TODO also check for stale jobs in Netdisco DB @@ -63,7 +65,7 @@ sub worker_body { } debug "mgr ($wid): sleeping now..."; - $0 = sprintf 'netdisco-daemon: worker #%s manager: idle', $wid; + setproctitle 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 55f6e34e..81a498e4 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/Worker/Scheduler.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/Worker/Scheduler.pm @@ -1,7 +1,9 @@ package App::Netdisco::Daemon::Worker::Scheduler; use Dancer qw/:moose :syntax :script/; + use Algorithm::Cron; +use Sys::Proctitle 'setproctitle'; use Role::Tiny; use namespace::clean; @@ -36,11 +38,11 @@ sub worker_body { # sleep until some point in the next minute my $naptime = 60 - (time % 60) + int(rand(45)); - $0 = sprintf 'netdisco-daemon: worker #%s scheduler: idle', $wid; + setproctitle sprintf 'netdisco-daemon: worker #%s scheduler: idle', $wid; debug "sched ($wid): sleeping for $naptime seconds"; sleep $naptime; - $0 = sprintf 'netdisco-daemon: worker #%s scheduler: queueing', $wid; + setproctitle sprintf 'netdisco-daemon: worker #%s scheduler: queueing', $wid; # NB next_time() returns the next *after* win_start my $win_start = time - (time % 60) - 1;