NG/System.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« NG/Autobox/Number.pm ↑ Summary NG.pm »
1 package NG::System;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'NG::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use NG::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Sys::CpuAffinity;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Net::SSH::Any;
11 use Data::Dumper;
12 use Child;
13  
14 sub new {
15 my $pkg = shift;
16 return bless {}, $pkg;
17 }
18  
19 =head2 local_run
20 local_run "ls", sub {
21 my ($out, $err) = @_;
22 $out->each(sub{
23 ...
24 })
25 ...
26 }
27 =cut
28 sub local_run {
29 my $inh = gensym;
30 my $outh = gensym;
31 my $errh = gensym;
32 my $cb;
33 if ( ref($cb) ne 'CODE' ) {
34 $cb = pop @_;
35 }
36 my @commands = @_;
37  
38 my $ret = open3( $inh, $outh, $errh, @commands );
39  
40 my $stdout = NG::Array->new;
41 my $stderr = NG::Array->new;
42 while (<$outh>) {
43 chomp;
44 $stdout->push($_);
45 }
46 while (<$errh>) {
47 chomp;
48 $stderr->push($_);
49 }
50 if ( defined $cb ) {
51 $cb->( $stdout, $stderr );
52 return $ret;
53 }
54 else {
55 return $stdout;
56 }
57 }
58  
59 =head2 remote_run
60 TODO: async ssh command
61 remote_run @hosts, $cmd, sub {
62 my ($out, $err) = @_;
63 }
64 =cut
65 sub remote_run {
66 my $cb = pop @_;
67 my $cmd = pop @_;
68 for my $host (@_) {
69 my $ssh = Net::SSH::Any->new($host);
70 my ($out, $err) = $ssh->capture2($cmd);
71 $cb->($out, $err);
72 };
73 }
74  
75 =head2 taskset
76 taskset($subpid, [0, 2])
77 =cut
78 sub taskset {
79 my ( $pid, @cpus ) = @_;
80 Sys::CpuAffinity::setAffinity( $pid, \@cpus );
81 }
82  
83 =head2 fork_run
84 Array->new(1, 2, 3)->each(sub {
85 my $i = shift;
86 System::fork_run(
87 sub { my $parent = shift; my $ppid = $parent->pid; my $line = $parent->read; $parent->say("$ppid teach $line") },
88 sub { my $child = shift; $child->say("$i");printf "%d learn: %s\n", $child->pid, $child->read; },
89 );
90 });
91 =cut
92 sub fork_run {
93 my ( $code, $cb ) = @_;
94 my $child = Child->new(sub {
95 my ( $parent ) = @_;
96 $code->($parent);
97 }, pipe => 1);
98 my $proc = $child->start;
99 $cb->($proc);
100 };
101  
102 1;
« NG/Autobox/Number.pm ↑ Summary NG.pm »