/[Sack]/trunk/lib/Sack/View.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Sack/View.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 120 - (show annotations)
Wed Oct 7 10:05:32 2009 UTC (14 years, 7 months ago) by dpavlin
Original Path: trunk/lib/Sack/Node.pm
File size: 2482 byte(s)
don't output debug <<<< and >>>
1 package Sack::Node;
2
3 use warnings;
4 use strict;
5
6 use IO::Socket::INET;
7 use File::Slurp;
8 use Carp qw(confess);
9 use Data::Dump qw(dump);
10 use Storable;
11 use Time::HiRes qw(time);
12
13 use lib 'lib';
14 use Sack::Color;
15
16 our $VERSION = '0.08';
17
18 sub new {
19 my $class = shift;
20 my $port = shift;
21 my $self = bless { port => $port }, $class;
22
23 my $pid_path = "/tmp/sack.$port.pid";
24 if ( -e $pid_path ) {
25 my $pid = read_file $pid_path;
26 kill 9, $pid; # warn "[$port] kill old $pid\n";
27 }
28 write_file $pid_path, $$;
29
30 my $sock = IO::Socket::INET->new(
31 Listen => SOMAXCONN,
32 LocalAddr => '127.0.0.1',
33 LocalPort => $port,
34 Proto => 'tcp',
35 Reuse => 1,
36 ) or die "[$port] die $!";
37
38 warn "[$port] accept $VERSION\n";
39
40 my $client = $sock->accept();
41
42 warn "[$port] connect from ", $client->peerhost, $/;
43
44 while ( 1 ) {
45
46 my $data = Storable::fd_retrieve( $client );
47 # warn "[$port] <<<<\n";
48 warn "[$port] data = ", dump( $data ) if $self->{debug};
49
50 my $result;
51
52 if ( $data->{view} ) {
53 $result = $self->view( $data->{view} );
54 } elsif ( $data->{data} ) {
55 $self->{data} = delete $data->{data};
56 $result = { data => 'loaded' };
57 } elsif ( $data->{exit} ) {
58 warn "[$port] exit";
59 close $sock;
60 exit;
61 } elsif ( $data->{restart} ) {
62 warn "[$port] restart";
63 close $sock;
64 exec "$0 $port";
65 } elsif ( $data->{info} ) {
66 $result = {
67 version => $VERSION,
68 size => $#{ $self->{data} } + 1,
69 };
70 } elsif ( my $sh = delete $data->{sh} ) {
71 $result = { sh => scalar `$sh` };
72 } else {
73 warn "[$port] UNKNOWN ", dump( $data ), $/;
74 $result = { 'error' => $data };
75 }
76
77 # warn "[$port] >>>>\n";
78 Storable::store_fd( $result => $client );
79 }
80
81 }
82
83
84 our $rec;
85 our $out;
86
87 sub view {
88 my ( $self, $code ) = @_;
89
90 undef $out;
91
92 my $affected = 0;
93 my $start_t = time;
94
95 my $coderef = eval "sub { $code }";
96 if ( $@ ) {
97 warn "ABORT code: $@";
98 return;
99 }
100
101
102 foreach my $pos ( 0 .. $#{ $self->{data} } ) {
103 $rec = $self->{data}->[$pos];
104 if ( ! $rec ) {
105 print STDERR "END @ $pos";
106 last;
107 }
108
109 eval { $coderef->() };
110 if ( $@ ) {
111 warn "ABORT $pos $@\n";
112 last;
113 } else {
114 $affected++;
115 }
116
117 $pos % 10000 == 0 ? print STDERR $pos :
118 $pos % 1000 == 0 ? print STDERR "." : 0 ;
119 };
120
121 my $dt = time - $start_t;
122 my $report = [ $self->{port}, $affected, $dt, $affected / $dt ];
123 warn sprintf "[%d] %d affected in %1.4fs %.2f/s\n", @$report;
124
125 # warn "# out ", dump( $out );
126
127 return {
128 out => $out,
129 report => $report,
130 };
131 }
132
133 1;

  ViewVC Help
Powered by ViewVC 1.1.26