Search code examples
perluuidmojolicious

Perl Mojolicious minion->enqueue with custom-id


According to Minion:

my $id = $minion->enqueue(foo => [@args]);

Is it possible to use a custom-id? For example:

my $id = Data::UUID->new->create_str;
$self->minion->enqueue(foo => [@args] => {id => $id});

Similar to the LinkCheck example, I don't want users to see "other" users' URLs as the id is easily guessable.


Solution

  • Not really. enqueue gets its ID from Minion::Backend::Pg::enqueue, which itself gets the ID by doing an SQL insert query that ends with RETURNING id, which means that the ID returned by enqueue is the ID of the row inserted into the database.


    Instead, you could use a hash mapping the IDs returned by enqueue to the GUIDs generated by Data::UUID. Something like:

    my $internal_id = $minion->enqueue(foo => [@args]);
    my $external_id = Data::UUID->new->create_str;
    $self->extern_to_intern_map{$external_id} = $internal_id;
    

    (note that extern_to_intern_map will grow without ever shrinking; if your application is running for a long time, you may want to explicitly delete old mappings)