From 4476246048abce80113e57decfa96360645939c5 Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Tue, 20 Mar 2018 19:24:48 +0000 Subject: [PATCH 1/3] Added initial Minion support and example test job --- .gitignore | 2 ++ README.md | 36 +++++++++++++++++++ cpanfile | 4 +++ lib/Pear/LocalLoop.pm | 7 ++++ lib/Pear/LocalLoop/Plugin/Minion.pm | 37 ++++++++++++++++++++ lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm | 15 ++++++++ 6 files changed, 101 insertions(+) create mode 100644 lib/Pear/LocalLoop/Plugin/Minion.pm create mode 100644 lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm diff --git a/.gitignore b/.gitignore index 2ecdfe1..027fb18 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ myapp.conf hypnotoad.pid *.db +*.db-wal +*.db-shm *~ /images *.swp diff --git a/README.md b/README.md index 61360c8..fc470dc 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,39 @@ cpanm --installdeps . --with-feature postgres PEAR_TEST_PG=1 prove -lr ``` +# Minion + +to set up minion support, you will need to create a database and user for +minion to connect to. In production his should be a PostgreSQL database, +however an SQLite db can be used in testing. + +To use the SQLite version, run the following commands: + +``` +cpanm --installdeps --with-feature sqlite . +``` + +And then add the following to your configuration file: + +``` + minion => { + SQLite => 'sqlite:minion.db', + }, +``` + +This will then use an SQLite db for the minion backend, at minion.db + + +## Example PostgreSQL setup + +``` +# Example commands - probably not the best ones +# TODO come back and improve these with proper ownership and DDL rights +sudo -u postgres createuser minion +sudo -u postgres createdb localloop_minion +sudo -u postgres psql +psql=# alter user minion with encrypted password 'abc123'; +psql=# grant all privileges on database localloop_minion to minion; +``` + + diff --git a/cpanfile b/cpanfile index 5ef4b1f..3d853b6 100644 --- a/cpanfile +++ b/cpanfile @@ -39,6 +39,10 @@ feature 'postgres', 'PostgreSQL Support' => sub { requires 'Test::PostgreSQL'; }; +feature 'sqlite', 'SQLite Support' => sub { + requires 'Minion::Backend::SQLite'; +}; + feature 'codepoint-open', 'Code Point Open manipulation' => sub { requires 'Geo::UK::Postcode::CodePointOpen'; }; diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 5bd5e26..be29224 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -42,6 +42,7 @@ sub startup { $self->plugin('Pear::LocalLoop::Plugin::Currency'); $self->plugin('Pear::LocalLoop::Plugin::Postcodes'); $self->plugin('Pear::LocalLoop::Plugin::TemplateHelpers'); + $self->plugin('Pear::LocalLoop::Plugin::Minion'); $self->plugin('Authentication' => { 'load_user' => sub { @@ -187,6 +188,12 @@ sub startup { my $admin_routes = $r->under('/admin')->to('admin#under'); + if ( defined $config->{minion} ) { + $self->plugin( 'Minion::Admin' => { + return_to => '/admin/home', + route => $admin_routes->any('/minion'), + } ); + } $admin_routes->get('/home')->to('admin#home'); $admin_routes->get('/tokens')->to('admin-tokens#index'); diff --git a/lib/Pear/LocalLoop/Plugin/Minion.pm b/lib/Pear/LocalLoop/Plugin/Minion.pm new file mode 100644 index 0000000..50f7abd --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/Minion.pm @@ -0,0 +1,37 @@ +package Pear::LocalLoop::Plugin::Minion; +use Mojo::Base 'Mojolicious::Plugin'; + +use Mojo::Loader qw/ find_modules load_class /; + +sub register { + my ( $plugin, $app, $cong ) = @_; + + if ( defined $app->config->{minion} ) { + $app->log->debug('Setting up Minion'); + $app->plugin('Minion' => $app->config->{minion} ); + + my $job_namespace = __PACKAGE__ . '::Job'; + my @modules = find_modules $job_namespace; + for my $package ( @modules ) { + my ( $job ) = $package =~ /${job_namespace}::(.*)$/; + $app->log->debug( $package ); + $app->log->debug( $job ); + load_class $package; + $app->minion->add_task( + $job => sub { + my ( $job, @args ) = @_; + my $job_runner = $package->new( + job => $job, + ); + $job_runner->run( @args ); + } + ); + } + $app->minion->enqueue('test' => [ 'test arg 1', 'test_arg 2' ] ); + } else { + $app->log->debug('No Minion Config'); + } + +} + +1; diff --git a/lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm b/lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm new file mode 100644 index 0000000..c01dd3b --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm @@ -0,0 +1,15 @@ +package Pear::LocalLoop::Plugin::Minion::Job::test; +use Mojo::Base -base; + +has [ qw/ job / ]; + +sub run { + my ( $self, @args ) = @_; + + $self->job->app->log->debug( 'Testing Job' ); + for my $arg ( @args ) { + $self->job->app->log->debug( $arg ); + } +} + +1; From c0375dcd41dcc83f96b954332e1ac5aeaa790e2c Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Tue, 20 Mar 2018 19:25:32 +0000 Subject: [PATCH 2/3] Stopped example job being enqueued if Minion is enabled --- lib/Pear/LocalLoop/Plugin/Minion.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop/Plugin/Minion.pm b/lib/Pear/LocalLoop/Plugin/Minion.pm index 50f7abd..6224134 100644 --- a/lib/Pear/LocalLoop/Plugin/Minion.pm +++ b/lib/Pear/LocalLoop/Plugin/Minion.pm @@ -27,7 +27,7 @@ sub register { } ); } - $app->minion->enqueue('test' => [ 'test arg 1', 'test_arg 2' ] ); + # $app->minion->enqueue('test' => [ 'test arg 1', 'test_arg 2' ] ); } else { $app->log->debug('No Minion Config'); } From 30c210200f80a190c7e5ddf2a4739e1a39d3218a Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Wed, 21 Mar 2018 17:14:51 +0000 Subject: [PATCH 3/3] Created role for Minion Jobs to make dev easier --- lib/Pear/LocalLoop/Plugin/Minion.pm | 7 ++++--- lib/Pear/LocalLoop/Plugin/Minion/Job.pm | 12 ++++++++++++ .../Plugin/Minion/Job/leaderboards_recalc.pm | 12 ++++++++++++ lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm | 4 +--- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 lib/Pear/LocalLoop/Plugin/Minion/Job.pm create mode 100644 lib/Pear/LocalLoop/Plugin/Minion/Job/leaderboards_recalc.pm diff --git a/lib/Pear/LocalLoop/Plugin/Minion.pm b/lib/Pear/LocalLoop/Plugin/Minion.pm index 6224134..54a592b 100644 --- a/lib/Pear/LocalLoop/Plugin/Minion.pm +++ b/lib/Pear/LocalLoop/Plugin/Minion.pm @@ -7,15 +7,16 @@ sub register { my ( $plugin, $app, $cong ) = @_; if ( defined $app->config->{minion} ) { - $app->log->debug('Setting up Minion'); + $app->log->debug('Setting up Minion tasks'); $app->plugin('Minion' => $app->config->{minion} ); + $app->log->debug('Loaded Minion Job packages:'); + my $job_namespace = __PACKAGE__ . '::Job'; my @modules = find_modules $job_namespace; for my $package ( @modules ) { my ( $job ) = $package =~ /${job_namespace}::(.*)$/; $app->log->debug( $package ); - $app->log->debug( $job ); load_class $package; $app->minion->add_task( $job => sub { @@ -27,7 +28,7 @@ sub register { } ); } - # $app->minion->enqueue('test' => [ 'test arg 1', 'test_arg 2' ] ); + # $app->minion->enqueue('test' => [ 'test arg 1', 'test_arg 2' ] ); } else { $app->log->debug('No Minion Config'); } diff --git a/lib/Pear/LocalLoop/Plugin/Minion/Job.pm b/lib/Pear/LocalLoop/Plugin/Minion/Job.pm new file mode 100644 index 0000000..2006845 --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/Minion/Job.pm @@ -0,0 +1,12 @@ +package Pear::LocalLoop::Plugin::Minion::Job; +use Mojo::Base -base; + +has [ qw/ job / ]; + +has app => sub { shift->job->app }; + +sub run { + die ( __PACKAGE__ . " must implement run sub" ); +} + +1; diff --git a/lib/Pear/LocalLoop/Plugin/Minion/Job/leaderboards_recalc.pm b/lib/Pear/LocalLoop/Plugin/Minion/Job/leaderboards_recalc.pm new file mode 100644 index 0000000..142df7f --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/Minion/Job/leaderboards_recalc.pm @@ -0,0 +1,12 @@ +package Pear::LocalLoop::Plugin::Minion::Job::leaderboards_recalc; +use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job'; + +sub run { + my ( $self, @args ) = @_; + + my $leaderboard_rs = $self->app->schema->resultset('Leaderboard'); + + $leaderboard_rs->recalculate_all; +} + +1; diff --git a/lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm b/lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm index c01dd3b..e0cfcf0 100644 --- a/lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm +++ b/lib/Pear/LocalLoop/Plugin/Minion/Job/test.pm @@ -1,7 +1,5 @@ package Pear::LocalLoop::Plugin::Minion::Job::test; -use Mojo::Base -base; - -has [ qw/ job / ]; +use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job'; sub run { my ( $self, @args ) = @_;