From 3514a9e0edf27a9c81f66eaef7d051592d03df9d Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Wed, 21 Mar 2018 17:14:51 +0000 Subject: [PATCH] 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 ) = @_;