]> iEval git - gruntmaster-daemon.git/blame - lib/Gruntmaster/Daemon/Base.pm
Finish SELinux policy ad fix some bugs
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Base.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon::Base;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7our @EXPORT_OK = qw/watch/;
8our $VERSION = '0.001';
9
10use Fcntl qw/O_WRONLY O_EXCL O_CREAT/;
11use Linux::Inotify2;
12use Log::Log4perl qw/get_logger/;
13
14##################################################
15
16sub process{
17 my ($name, $dir, $cb) = @_;
18 my $logger = get_logger;
19 $logger->debug("Taking job $name...");
20 if (sysopen my $file, "$dir/$name/pidfile", O_WRONLY | O_EXCL | O_CREAT){
21 $logger->debug("Successfully taken job $name, executing callback");
22 $cb->("$dir/$name");
23 } else {
24 $logger->debug("Job $name already taken");
25 }
26}
27
28sub watch{
29 my ($dir, $cb) = @_;
30 for (<$dir/*>) {
31 s,$dir/,,;
32 process $_, $dir, $cb;
33 }
34
35 my $logger = Log::Log4perl->get_logger(__PACKAGE__);
36 my $inotify = Linux::Inotify2->new or $logger->logdie("Unable to create Linux::Inotify2 object: $!");
37 $inotify->watch($dir, IN_MOVED_TO, sub { process $_[0]->name, $dir, $cb }) or $logger->logdie("Error watching $dir: $!");
38 1 while $inotify->poll;
39 $logger->logdie("Inotify polling stopped: $!");
40}
41
421
This page took 0.025031 seconds and 4 git commands to generate.