Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional pidfile support, documentation and a test.
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters