/[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 116 - (show annotations)
Tue Oct 6 00:07:05 2009 UTC (14 years, 8 months ago) by dpavlin
Original Path: trunk/lib/Sack/Node.pm
File size: 2218 byte(s)
added report to node view resposne and version it [0.07]

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.07';
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', version => $VERSION };
57 } elsif ( $data->{exit} ) {
58 warn "[$port] exit";
59 exit;
60 } else {
61 warn "[$port] UNKNOWN ", dump( $data ), $/;
62 $result = { 'error' => $data };
63 }
64
65 warn "[$port] >>>>\n";
66 Storable::store_fd( $result => $client );
67 }
68
69 }
70
71
72 our $rec;
73 our $out;
74
75 sub view {
76 my ( $self, $code ) = @_;
77
78 undef $out;
79
80 my $affected = 0;
81 my $start_t = time;
82
83 my $coderef = eval "sub { $code }";
84 if ( $@ ) {
85 warn "ABORT code: $@";
86 return;
87 }
88
89
90 foreach my $pos ( 0 .. $#{ $self->{data} } ) {
91 $rec = $self->{data}->[$pos];
92 if ( ! $rec ) {
93 print STDERR "END @ $pos";
94 last;
95 }
96
97 eval { $coderef->() };
98 if ( $@ ) {
99 warn "ABORT $pos $@\n";
100 last;
101 } else {
102 $affected++;
103 }
104
105 $pos % 10000 == 0 ? print STDERR $pos :
106 $pos % 1000 == 0 ? print STDERR "." : 0 ;
107 };
108
109 my $dt = time - $start_t;
110 my $report = [ $self->{port}, $affected, $dt, $affected / $dt ];
111 warn sprintf "[%d] %d affected in %1.4fs %.2f/s\n", @$report;
112
113 # warn "# out ", dump( $out );
114
115 return {
116 out => $out,
117 report => $report,
118 version => $VERSION,
119 };
120 }
121
122 1;

  ViewVC Help
Powered by ViewVC 1.1.26