Skip to content
Browse files

Add optional pidfile support, documentation and a test.

  • Loading branch information...
1 parent f405346 commit d51d5839921484474b290ab8a9db73fb2910f363 Chris Nehren committed with
Showing with 107 additions and 1 deletion.
  1. +64 −0 lib/Bot/Pastebot/Administrivia.pm
  2. +41 −0 pastebot
  3. +2 −1 t/01_basic.t
View
64 lib/Bot/Pastebot/Administrivia.pm
@@ -0,0 +1,64 @@
+# Miscellaneous administrivia.
+
+package Bot::Pastebot::Administrivia;
+
+use warnings;
+use strict;
+
+use Carp qw(croak);
+use Bot::Pastebot::Conf qw( get_names_by_type get_items_by_name );
+
+use base qw(Exporter);
+our @EXPORT_OK = qw(get_pid write_pidfile uses_pidfile);
+
+# Return this module's configuration.
+
+use Bot::Pastebot::Conf qw(SCALAR REQUIRED);
+
+my %conf = (
+ administrivia => {
+ name => SCALAR | REQUIRED,
+ pidfile => SCALAR,
+ },
+);
+
+sub get_conf { return %conf }
+
+# return the name used in the config for this section.
+sub _get_name {
+ my $name;
+ eval { ($name) = get_names_by_type('administrivia') };
+ return $name unless $@;
+}
+
+# Do we have a pidfile configured?
+sub uses_pidfile {
+ _get_name();
+}
+
+# Examine the PID file to see if there's a session running already.
+sub get_pid {
+ my $name = _get_name();
+ my %conf = get_items_by_name($name);
+ my $pidfile = $conf{pidfile};
+ return unless -e $pidfile;
+ my $pid = do {
+ local $/;
+ open my $fh, '<', $pidfile or die "open($pidfile): $!";
+ <$fh>;
+ };
+ my $is_running = kill 0, $pid;
+ return $pid if $is_running;
+}
+
+# We don't seem to be running, so write our PID file.
+sub write_pidfile {
+ my $name = _get_name();
+ my %conf = get_items_by_name($name);
+ my $pidfile = $conf{pidfile};
+ open my $fh, '>', $pidfile or die "open($pidfile): $!";
+ print $fh $$;
+ close $fh;
+}
+
+1;
View
41 pastebot
@@ -15,11 +15,13 @@ use Bot::Pastebot::Conf;
use Bot::Pastebot::Data;
use Bot::Pastebot::Client::Irc;
use Bot::Pastebot::Server::Http;
+use Bot::Pastebot::Administrivia qw/uses_pidfile get_pid write_pidfile/;
my %conf = (
Bot::Pastebot::Data->get_conf(),
Bot::Pastebot::Client::Irc->get_conf(),
Bot::Pastebot::Server::Http->get_conf(),
+ Bot::Pastebot::Administrivia->get_conf(),
);
# Command line options.
@@ -27,6 +29,15 @@ my %conf = (
my $conf_file = Bot::Pastebot::Conf->get_conf_file();
Bot::Pastebot::Conf->load($conf_file, \%conf);
+if (uses_pidfile() && (my $pid = get_pid()) ) {
+ die(
+ "pastebot seems to be running as PID $pid.\n",
+ "Please check if this is the case.\n",
+ );
+} elsif( uses_pidfile() ) {
+ write_pidfile();
+}
+
Bot::Pastebot::Data->initialize();
Bot::Pastebot::Client::Irc->initialize();
Bot::Pastebot::Server::Http->initialize();
@@ -117,6 +128,36 @@ Pastebot configuration is broken down into three section types:
"web_server", "irc", and "pastes". They define the web server, irc
network, and paste database that the bot will use.
+=head2 Optional Administrivia
+
+These settings make it easier to administrate pastebot. Right now it
+only covers a PID file, but may be expanded upon in the future. This
+entire section is optional, but is global for the bot.
+
+A sample:
+
+ administrivia
+ name administrivia
+ pidfile /var/run/pastebot.pid
+
+=over 2
+
+=item name
+
+The name for this section. 'administrivia' is simple and clear, but
+it's not limited to this.
+
+=item pidfile
+
+A path, absolute or relative, that will hold a PID for the bot. Upon
+startup, this file will be read and its contents interpreted as a
+PID. If this PID is running, pastebot will exit with a message
+notifying the administrator of this fact. If this PID is not
+running, pastebot will write its own PID to the pidfile before
+continuing.
+
+=back
+
=head2 Configuring a Web Server
The web_server section defines a pastebot's web server. A pastebot
View
3 t/01_basic.t
@@ -1,8 +1,9 @@
use POE;
-use Test::More tests => 5;
+use Test::More tests => 6;
use_ok("Bot::Pastebot::Conf");
+use_ok("Bot::Pastebot::Administrivia");
use_ok("Bot::Pastebot::Data");
use_ok("Bot::Pastebot::WebUtil");
use_ok("Bot::Pastebot::Client::Irc");

0 comments on commit d51d583

Please sign in to comment.
Something went wrong with that request. Please try again.