6 use lib '/srv/MojoX-Gearman/lib';
9 use Data::Dump qw(dump);
12 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
13 plugin 'pod_renderer';
19 $self->render('index');
22 get '/ping_http' => sub {
24 my $gearman = $self->client;
25 my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
27 $self->render( 'ping', pong => $pong );
30 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
31 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
33 get '/ping_g' => sub {
35 my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
37 $self->render( 'ping', pong => $pong );
41 my ( $self, $json ) = @_;
42 #my $data = $self->render( json => $json, partial => 1 );
44 if ( my $callback = $self->param('callback') ) {
45 $data = "$callback($data)";
47 $self->render( data => $data, format => 'js' );
51 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
53 my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
54 warn $self->param('call'), " = ", dump($ret), "\n";
55 die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
56 _render_jsonp( $self, $ret );
59 get '/_g/status' => sub {
62 my $ret = $gearman->req( 'TEXT' => 'status' );
63 warn "# status:\n$ret";
65 my @c = qw(function total running available);
68 foreach my $l ( split(/\n/,$ret) ) {
69 my @v = split(/\t/, $l);
71 $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
74 warn "## ", dump $status;
75 _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
78 get '/gnuplot' => sub {
81 my $sql = $self->param('sql') || die "sql required";
83 my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
84 die "no result for $sql" unless $ret;
86 $ret = Mojo::JSON->new->decode( $ret );
88 my $dir = $self->app->home->rel_dir('public');
94 warn "# $sql -> $name";
96 mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
97 my $png = "$dir/gnuplot/$name"; # FIXME
99 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot: $!";
100 open($gnuplot, '>', '/tmp/gnuplot') if $self->param('debug');
102 my @c = @{ $ret->{columns} };
103 warn "first column not timestamp" unless shift @c eq 'timestamp';
110 set timefmt "%Y-%m-%d %H:%M:%S"
111 set format x "%H%M%S"
113 plot '-' using 1:3 title "$c[0]"
115 foreach my $row ( @{ $ret->{rows} } ) {
116 print $gnuplot join("\t", @$row),"\n";
118 print $gnuplot "e\n";
121 $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name");
124 get '/_redis' => sub {
127 my $redis = Redis->new;
130 $status->{$_} = $redis->scard($_) foreach $redis->keys('poll.*');
132 warn "## ", dump $status;
133 _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
141 % title 'Gearman demo';
144 <li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
145 <li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
151 <li><a href="/gnuplot?sql=select timestamp,rtt from ping where ip << inet '10.17/16' order by timestamp desc limit 1000">ttl from 10.17 network</a>
156 <%= link_to 'HTTP ping' => 'ping_http' %>
157 <%= link_to 'Gearman ping' => 'ping_g' %>
161 pong: <tt><%= $pong %>
165 <%= form_for gnuplot => begin %>
166 <%= text_area 'sql', cols => 80 %>
169 <img src="<%= $img %>">
171 @@ layouts/default.html.ep
172 <!doctype html><html>
173 <head><title><%= title %></title></head>
174 <body><%= content %></body>