Skip to content

Commit

Permalink
refactor pool with detached workers
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed May 1, 2017
1 parent 4e4d445 commit 6fefa92
Show file tree
Hide file tree
Showing 25 changed files with 769 additions and 686 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ perl5/
db.db
data/builds/
data/config.yml
tmp/
1 change: 1 addition & 0 deletions .perltidyrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
-l=120
-pt=2
33 changes: 19 additions & 14 deletions bin/crafty
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ use Crafty;
use Crafty::Log;
use Crafty::Config;
use Crafty::EventBus;
use Crafty::PubSub;

my $opt_base = 'data';
my $opt_config = 'data/config.yml';
my $opt_listen = '0.0.0.0:5000';
my $opt_listen;
my $opt_verbose;
GetOptions(
'config=s' => \$opt_config,
Expand All @@ -42,26 +43,28 @@ Crafty::Log->init(verbose => 1) if $opt_verbose;
my $config = Crafty::Config->new(root => $root, base => $opt_base);
$config->load($opt_config);

$opt_listen //= $config->config->{listen} // '127.0.0.1:5000';
$config->config->{listen} //= $opt_listen;

Crafty::PubSub->instance->own->address($opt_listen);

my $app = Crafty->new(config => $config, root => $root);

my $w; $w = AE::signal HUP => sub {
my $w;
$w = AE::signal HUP => sub {
warn "Exiting gracefully (waiting for everything to finish)...\n";

$app->stop(sub { sleep 2; undef $w; kill 'QUIT', $$; sleep 5; kill 'TERM', $$; });
sleep 2;
undef $w;

kill 'QUIT', $$;

sleep 5;

kill 'TERM', $$;
};

my $psgi = builder {
mount '/events' => builder {
return Plack::App::EventSource->new(
headers => ['Access-Control-Allow-Credentials', 'true'],
handler_cb => sub {
my ($conn, $env) = @_;

Crafty::EventBus->instance->new_conn($conn, $env);
}
)->to_app;
};

mount '/' => builder {
enable_if { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' }
'Plack::Middleware::ReverseProxy';
Expand All @@ -72,6 +75,8 @@ my $psgi = builder {

enable "HTTPExceptions", rethrow => 1;

#enable "AccessLog", format => "combined";

return $app->to_psgi;
};
};
Expand Down
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
requires 'AnyEvent';
requires 'AnyEvent::Fork';
requires 'AnyEvent::Fork::Pool';
requires 'AnyEvent::HTTP';
requires 'AnyEvent::DBI';
requires 'EV';
requires 'Promises';
Expand Down
27 changes: 9 additions & 18 deletions lib/Crafty.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ has 'db',
default => sub {
my $self = shift;

return Crafty::DB->new(db_file => $self->config->db_file);
return Crafty::DB->new(
config => $self->config->config,
db_file => $self->config->db_file
);
};
has 'view',
is => 'ro',
Expand All @@ -42,21 +45,6 @@ sub BUILD {
$self->pool->start;
}

sub stop {
my $self = shift;
my ($cb) = @_;

$self->pool->stop(
sub {
delete $self->{pool};

Crafty::Log->info("Stopping app");

$cb->();
}
);
}

sub build_routes {
my $self = shift;

Expand All @@ -69,6 +57,9 @@ sub build_routes {
$routes->add_route('/download/:build_id', name => 'Download');
$routes->add_route('/restart/:build_id', name => 'Restart');

$routes->add_route('/events', name => 'Events');
$routes->add_route('/_event', name => 'Event');

$routes->add_route('/webhook/:provider/:project', name => 'Hook');

return $routes;
Expand Down Expand Up @@ -97,10 +88,10 @@ sub to_psgi {
pool => $self->pool,
);

return $action->run(%{$match->captures || {}});
return $action->run(%{ $match->captures || {} });
}
else {
return [404, [], ['Not Found']];
return [ 404, [], ['Not Found'] ];
}
};
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Crafty/Action/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ sub render {

my $content = $view->render_file($template, $args);

return $view->render_file('layout.caml', {content => $content});
return $view->render_file('layout.caml', { content => $content, verbose => Crafty::Log->is_verbose });
}

sub not_found {
my $self = shift;
my ($respond) = @_;

my $res = ['404', [], ['Not Found']];
my $res = [ '404', [], ['Not Found'] ];

return $respond ? $respond->($res) : $res;
}
Expand All @@ -36,7 +36,7 @@ sub redirect {
my $self = shift;
my ($url, $respond) = @_;

my $res = [302, [Location => $url], ['']];
my $res = [ 302, [ Location => $url ], [''] ];

return $respond ? $respond->($res) : $res;
}
Expand All @@ -47,7 +47,7 @@ sub handle_error {

Crafty::Log->error(@_) unless ref $error;

my $res = ref $error ? $error : [500, [], ['error']];
my $res = ref $error ? $error : [ 500, [], ['error'] ];

return $respond ? $respond->($res) : $res;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Crafty/Action/Cancel.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ sub run {
my ($build) = @_;

return deferred->reject($self->not_found)
unless $build && $build->cancel;
unless $build && $build->is_cancelable;

return $self->db->save($build);
return deferred->resolve($build);
},
sub {
return deferred->reject($self->not_found);
Expand All @@ -29,7 +29,7 @@ sub run {
sub {
my ($build) = @_;

$self->pool->peek;
$self->pool->cancel($build);

return $self->redirect("/builds/$uuid", $respond);
},
Expand Down
19 changes: 19 additions & 0 deletions lib/Crafty/Action/Event.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Crafty::Action::Event;
use Moo;
extends 'Crafty::Action::Base';

use Crafty::PubSub;

sub run {
my $self = shift;

my $body = $self->req->content;

$body = JSON::decode_json($body);

Crafty::PubSub->instance->publish(@$body);

return [ 200, [], ['ok'] ];
}

1;
44 changes: 44 additions & 0 deletions lib/Crafty/Action/Events.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package Crafty::Action::Events;
use Moo;
extends 'Crafty::Action::Base';

use Promises qw(deferred);
use JSON ();
use Plack::App::EventSource;
use Crafty::PubSub;
use Crafty::Log;

sub run {
my $self = shift;
my (%params) = @_;

return sub {
my $respond = shift;

my $cb = Plack::App::EventSource->new(
headers => [ 'Access-Control-Allow-Credentials', 'true' ],
handler_cb => sub {
my ($conn, $env) = @_;

Crafty::PubSub->instance->subscribe(
'*' => sub {
my ($ev, $data) = @_;

eval {
$conn->push(JSON::encode_json({ type => $ev, data => $data }));
1;
} or do {
$conn->close;
};

return deferred->resolve;
}
);
}
)->call($self->env);

$cb->($respond);
};
}

1;
2 changes: 0 additions & 2 deletions lib/Crafty/Action/Tail.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,4 @@ sub tail {
};
}

sub DESTROY { "ACTION TAIL DESTROY" }

1;
4 changes: 2 additions & 2 deletions lib/Crafty/Build.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ sub status_name {
return {
'N' => 'New',
'I' => 'Preparing',
'L' => 'Preparing',
'L' => 'Locked',
'P' => 'Running',
'S' => 'Success',
'E' => 'Error',
'F' => 'Failure',
'C' => 'Canceling',
'C' => 'Canceled',
'K' => 'Killed',
}->{ $self->status };
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Crafty/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ sub load {
$self->{config}->{builds_dir} =
$self->resolve_path($self->{config}->{builds_dir}, 'builds');

$self->{config}->{pool}->{mode} //= 'fork';

return $self->{config};
}

Expand Down
Loading

0 comments on commit 6fefa92

Please sign in to comment.