#!/usr/bin/env perl

use Mojolicious::Lite;

use lib '/srv/MojoX-Gearman/lib';
use MojoX::Gearman;

use Data::Dump qw(dump);

# Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
plugin 'pod_renderer';

get '/' => sub {
	my $self = shift;
	$self->render('index');
};

get '/ping_http' => sub {
	my $self = shift;
	my $gearman = $self->client;
	my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
	warn "ping = $pong";
	$self->render( 'ping', pong => $pong );
};

my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
$gearman->server( $ENV{GEARMAN} || 'localhost:4730' );

get '/ping_g' => sub {
	my $self = shift;
	my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
	warn "ping = $pong";
	$self->render( 'ping', pong => $pong );
};

get '/g/:call/:args' => sub {
	my $self = shift;
	my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
	warn $self->param('call'), " = ", dump($ret), "\n";
	$self->render( 'dump', dump => $ret );
};
app->start;
__DATA__

@@ index.html.ep
% layout 'default';
% title 'Gearman demo';
Welcome to Mojolicious Gearman integration demo!

<%= link_to 'HTTP ping' => 'ping_http' %>
<%= link_to 'Gearman ping' => 'ping_g' %>

@@ ping.html.ep
% layout 'default';
pong: <tt><%= $pong %>

@@ dump.html.ep
% layout 'default';
<pre><%= $dump %></pre>

@@ layouts/default.html.ep
<!doctype html><html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
</html>
