5 use lib '/srv/MojoX-Gearman/lib';
8 use Data::Dump qw(dump);
11 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
12 plugin 'pod_renderer';
18 $self->render('index');
21 get '/ping_http' => sub {
23 my $gearman = $self->client;
24 my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
26 $self->render( 'ping', pong => $pong );
29 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
30 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
32 get '/ping_g' => sub {
34 my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
36 $self->render( 'ping', pong => $pong );
40 my ( $self, $json ) = @_;
41 #my $data = $self->render( json => $json, partial => 1 );
43 if ( my $callback = $self->param('callback') ) {
44 $data = "$callback($data)";
46 $self->render( data => $data, format => 'js' );
50 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
52 my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
53 warn $self->param('call'), " = ", dump($ret), "\n";
54 die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
55 _render_jsonp( $self, $ret );
58 get '/_g/status' => sub {
61 my $ret = $gearman->req( 'TEXT' => 'status' );
62 warn "# status:\n$ret";
64 my @c = qw(function total running available);
67 foreach my $l ( split(/\n/,$ret) ) {
68 my @v = split(/\t/, $l);
70 $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
73 warn "## ", dump $status;
74 _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
77 get '/gnuplot' => sub {
80 my $sql = $self->param('sql') || die "sql required";
82 my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
83 die "no result for $sql" unless $ret;
85 $ret = Mojo::JSON->new->decode( $ret );
87 my $dir = $self->app->home->rel_dir('public');
93 warn "# $sql -> $name";
95 mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
96 my $png = "$dir/gnuplot/$name"; # FIXME
98 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot: $!";
99 open($gnuplot, '>', '/tmp/gnuplot') if $self->param('debug');
101 my @c = @{ $ret->{columns} };
102 warn "first column not timestamp" unless shift @c eq 'timestamp';
109 set timefmt "%Y-%m-%d %H:%M:%S"
110 set format x "%H%M%S"
112 plot '-' using 1:3 title "$c[0]"
114 foreach my $row ( @{ $ret->{rows} } ) {
115 print $gnuplot join("\t", @$row),"\n";
117 print $gnuplot "e\n";
120 $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name");
128 % title 'Gearman demo';
131 <li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
132 <li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
138 <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>
143 <%= link_to 'HTTP ping' => 'ping_http' %>
144 <%= link_to 'Gearman ping' => 'ping_g' %>
148 pong: <tt><%= $pong %>
152 <%= form_for gnuplot => begin %>
153 <%= text_area 'sql', cols => 80 %>
156 <img src="<%= $img %>">
158 @@ layouts/default.html.ep
159 <!doctype html><html>
160 <head><title><%= title %></title></head>
161 <body><%= content %></body>