Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Morbo + Minion workers = development joy #85

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/Mojolicious/Plugin/Minion.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,51 @@ package Mojolicious::Plugin::Minion;
use Mojo::Base 'Mojolicious::Plugin';

use Minion;
use Mojo::IOLoop;

sub register {
my ($self, $app, $conf) = @_;
push @{$app->commands->namespaces}, 'Minion::Command';
my $minion = Minion->new(%$conf)->app($app);
$app->helper(minion => sub {$minion});
$app->helper(minion_dev_server => \&_dev_server);
}

sub _dev_server {
my ($c, @args) = @_;

$c->app->hook(
before_server_start => sub {
my ($server, $app) = @_;
return unless $server->isa('Mojo::Server::Daemon');
$app->minion->missing_after(0)->repair;

# without server event finish, use server pid going away to set finished
my $server_pid = $$;
Mojo::IOLoop->subprocess(
sub {
$app->minion->on(worker => sub {
my ($minion, $worker) = @_;
$minion->missing_after(180);
$worker->status(@args);
$worker->on(dequeue => sub { pop->once(spawn => \&_job_spawned) });
$worker->on(wait => sub { shift->{finished} = !kill 0, $server_pid});
});
$app->minion->worker->run;
},
sub {
$app->log->warn("Subprocess error: $_[1]") and return if $_[1];
}
);
}
) if $c->app->mode eq 'development';
}

sub _job_spawned {
my ($job, $pid) = @_;
my ($id, $task) = ($job->id, $job->task);
$job->app->log->debug(
qq{Process $pid is performing job "$id" with task "$task"});
}

1;
Expand All @@ -30,6 +69,10 @@ Mojolicious::Plugin::Minion - Minion job queue plugin
helper pg => sub { state $pg = Mojo::Pg->new('postgresql://postgres@/test') };
plugin Minion => {Pg => app->pg};

# Perform jobs in the web server during development
plugin Minion => {Pg => 'postgresql://postgres@/test'};
app->minion_dev_server;

# Add tasks to your application
app->minion->add_task(slow_log => sub {
my ($job, $msg) = @_;
Expand Down Expand Up @@ -67,6 +110,14 @@ Get L<Minion> object for application.
# Perform jobs for testing
$app->minion->perform_jobs;

=head2 minion_dev_server

$app->minion_dev_server;

In C<development> mode when started with a development web server perform all
jobs from the web server without starting a separate worker process, takes the
same arguments as L<Minion::Worker/"status">.

=head1 METHODS

L<Mojolicious::Plugin::Minion> inherits all methods from
Expand Down
Loading