Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

perl - Setting the "finish" event on a Mojolicious/Minion::Job

I am trying to have Minion jobs feed back to the Mojolicious web app upon completion (probably by posting a message to an API). The underlying idea here is then for the web app to feed back to the client that has uploaded/started the job.

I have tried doing this:

get '/' => sub {
    my $c = shift;
    my $id = $c->minion->enqueue('thing', [ qw/a b 1/, { foo => 'bar' } ]);

    my $job = $c->minion->job($id);

    $job->on(finish => sub ($job) {
              my $id   = $job->id;
              my $task = $job->task;
              $job->app->log->info(qq{Job "$id" was performed with task "$task"});
         });


    $c->render(template => 'index');
};

which doesn't work - I guess because the event is only emitted in the process performing the job, and the event does not get serialized and queued.

If I do this:

app->minion->add_task
    (thing => sub ($job, $c, $sub, @args) {
     $job->on(finish => sub ($job) {
              my $id   = $job->id;
              my $task = $job->task;
              $job->app->log->info(qq{Job "$id" was performed with task "$task"});
          });
          sleep 2;
     });

it works ok, but it means I have to add the event handling to every task - which adds complexity to the code.

Is there a way to avoid having to do this?

I am thinking of:

  1. being able to set a default class for jobs (so that jobs are all of a subclass of Mojo::Job - for example Minion::Job::WithFeedback)
  2. better yet, being able to inject roles into task creation (so that you can do $c->minion->enqueue('thing', [ qw/a b 1/, { foo => 'bar' } ], { roles => qw/+WithFeedback +WithTimeout/);

I know I could poll all jobs regularly and see what changed status - this is what the Minion::Admin plugin does - but I would like to see if there is a different way that doesn't require polling the database.

Is this possible? and while we're at it - is this a bad idea in and of itself?

question from:https://stackoverflow.com/questions/65626783/setting-the-finish-event-on-a-mojolicious-minionjob

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...