File Coverage

blib/lib/Sys/CmdMod/Plugin/Eatmydata.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package Sys::CmdMod::Plugin::Eatmydata;
2             {
3             $Sys::CmdMod::Plugin::Eatmydata::VERSION = '0.18';
4             }
5             BEGIN {
6 1     1   23485 $Sys::CmdMod::Plugin::Eatmydata::AUTHORITY = 'cpan:TEX';
7             }
8             # ABSTRACT: CmdMod plugin for eatmydata
9              
10 1     1   29 use 5.010_000;
  1         3  
  1         36  
11 1     1   1725 use mro 'c3';
  1         795  
  1         6  
12 1     1   41 use feature ':5.10';
  1         1  
  1         129  
13              
14 1     1   456 use Moose;
  0            
  0            
15             use namespace::autoclean;
16              
17             # use IO::Handle;
18             # use autodie;
19             # use MooseX::Params::Validate;
20              
21             use Sys::ForkAsync;
22              
23             extends 'Sys::CmdMod::Plugin';
24              
25             has '_init_ok' => (
26             'is' => 'rw',
27             'isa' => 'Bool',
28             'default' => 0,
29             );
30              
31             sub _init_priority { return 0; }
32              
33             sub BUILD {
34             my $self = shift;
35              
36             if ( -x $self->binary() ) {
37             return 1;
38             }
39             else {
40             die( 'Could not find eatmydata executable at ' . $self->binary() );
41             }
42             }
43              
44             sub _init_binary {
45             my $self = shift;
46              
47             return $self->_find_binary('eatmydata');
48             }
49              
50             sub cmd {
51             my $self = shift;
52             my $cmd = shift;
53              
54             # we only want sync to be called later if eatmydata was actually used ...
55             $self->_init_ok(1);
56              
57             return $self->binary() . q{ } . $cmd;
58             }
59              
60             sub DEMOLISH {
61             my $self = shift;
62              
63             # dirty hack, as long as Proc::ProcessTable is broken ...
64             my $syncs_running = qx(ps x | grep sys-cmdmod-eatmydata-sync | grep -v grep | wc -l);
65             chomp($syncs_running);
66              
67             # run 'sync' in background
68             if ( $self->_init_ok() && !$syncs_running ) {
69             #say 'Scheduling a background sync ...';
70             my $FA = Sys::ForkAsync::->new({
71             'setsid' => 1,
72             'name' => 'sys-cmdmod-eatmydata-sync',
73             'redirect_output' => 1,
74             });
75             my $sub = sub {
76             sleep 1;
77             system('sync');
78             };
79             $FA->dispatch($sub);
80             } else {
81             #say 'NOT scheduling a background sync. Already one running ...';
82             }
83              
84             return 1;
85             }
86              
87             no Moose;
88             __PACKAGE__->meta->make_immutable;
89              
90             1;
91              
92             __END__
93              
94             =pod
95              
96             =encoding utf-8
97              
98             =head1 NAME
99              
100             Sys::CmdMod::Plugin::Eatmydata - CmdMod plugin for eatmydata
101              
102             =head1 METHODS
103              
104             =head2 BUILD
105              
106             Detect binary and initialize this module.
107              
108             =head2 DEMOLISH
109              
110             If this module was successfully initialized in BUILD this will run an async 'sync'.
111              
112             =head2 cmd
113              
114             Return this modules command prefix.
115              
116             =head1 NAME
117              
118             Sys::CmdMod::Plugin::Eatmydata - Abstract base class for command modifier
119              
120             =head1 AUTHOR
121              
122             Dominik Schulz <dominik.schulz@gauner.org>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2012 by Dominik Schulz.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut