<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mojolicious minion Archives | The Curious Technoid</title>
	<atom:link href="https://thecurioustechnoid.com/tag/mojolicious-minion/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecurioustechnoid.com/tag/mojolicious-minion/</link>
	<description>technology made simple</description>
	<lastBuildDate>Wed, 05 May 2021 04:44:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://thecurioustechnoid.com/wp-content/uploads/2020/06/cropped-fav-1-32x32.png</url>
	<title>mojolicious minion Archives | The Curious Technoid</title>
	<link>https://thecurioustechnoid.com/tag/mojolicious-minion/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Mojolicious Minion &#8211; A High Performance Job Queue</title>
		<link>https://thecurioustechnoid.com/mojolicious-minion-a-high-performance-job-queue/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mojolicious-minion-a-high-performance-job-queue</link>
					<comments>https://thecurioustechnoid.com/mojolicious-minion-a-high-performance-job-queue/#respond</comments>
		
		<dc:creator><![CDATA[Rakshith Chengappa Mullengada]]></dc:creator>
		<pubDate>Mon, 26 Apr 2021 14:55:28 +0000</pubDate>
				<category><![CDATA[Perl / Mojolicious]]></category>
		<category><![CDATA[minion]]></category>
		<category><![CDATA[minions]]></category>
		<category><![CDATA[mojolicious]]></category>
		<category><![CDATA[mojolicious minion]]></category>
		<guid isPermaLink="false">https://thecurioustechnoid.com/?p=514</guid>

					<description><![CDATA[<p>Minion is a third party plugin for Mojolicious Web Framework which allows you to process time consuming tasks in the background. They are high performance job queue for Perl programming language and is not limited to just Mojolicious web framework. Minion is a high performance job queue for the Perl programming language, with support for&#8230;</p>
<p>The post <a href="https://thecurioustechnoid.com/mojolicious-minion-a-high-performance-job-queue/">Mojolicious Minion &#8211; A High Performance Job Queue</a> appeared first on <a href="https://thecurioustechnoid.com">The Curious Technoid</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-drop-cap">Minion is a third party plugin for Mojolicious Web Framework which allows you to process time consuming tasks in the background. They are high performance job queue for Perl programming language and is not limited to just Mojolicious web framework.</p>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><a href="https://youtu.be/Icc9YOOF414" target="_blank" rel="noopener"><img decoding="async" src="https://thecurioustechnoid.com/wp-content/uploads/2020/06/WatchOnYoutubeImage.png" alt="" class="wp-image-78" width="217" height="87"/></a></figure></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Minion is a high performance job queue for the Perl programming language, with support for multiple named queues, priorities, high priority fast lane, delayed jobs, job dependencies, job progress, job results, retries with backoff, rate limiting, unique jobs, expiring jobs, statistics, distributed workers, parallel processing, autoscaling, remote control, Mojolicious admin ui, resource leak protection and multiple backends (such as PostgreSQL).</p><cite>mojolicious.org</cite></blockquote>



<p>Minions doesn&#8217;t come pre-installed with Mojolicious installation so it needs to be installed separately.</p>



<pre class="wp-block-code"><code>cpanm Minion</code></pre>



<p>If you use docker, I have built an image which has Mojolicious and Minion together. You can get it here:</p>



<pre class="wp-block-code"><code>docker pull curioustechnoid/mojolicious</code></pre>



<p>Minion uses database backend to store and process the jobs. Most of the popular databases(like SQLite, PostgreSQL) are supported. Jobs can be added to the queue from different programs like Mojolicious, Perl Program, Command Line etc. The Minion worker will be monitoring your backend database and processes the jobs based on priority and availability.</p>



<p>There are 2 important components to Minions:</p>



<ul class="wp-block-list"><li>Minion Worker</li><li>Job Queues</li></ul>



<p>Let us see how we can configure and deploy Minion.</p>



<h2 class="wp-block-heading">Minion Worker</h2>



<p>Minion workers are the silent warriors that keeps monitoring the backend database and processes the jobs that gets added to the queue.</p>



<p>Let us go ahead and create a script to start the Minion worker. I am using Mojolicious lite app to create a worker. You can very well create a worker without using Mojolicious, read more about it <a href="https://docs.mojolicious.org/Minion/Guide#Custom-workers" target="_blank" rel="noreferrer noopener">here</a>. We will also add definition of our jobs inside this script. We will be adding a &#8216;<em>slow-running</em>&#8216; task and a &#8216;<em>high priority&#8217;</em> task to the script as an example. For the purpose of this demo, I am using SQLite as the backend.</p>



<pre class="wp-block-code"><code>#!/usr/bin/env perl
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# minion-worker.pl
#
# AUTHOR:  Curious Technoid
# DATE:  20-Apr-2021
# VERSION:  1.0
# PURPOSE:
#           This mojo app is to initiate the minion worker
#           which will process the jobs in the queue.
#
# USAGE: 
#       ./minion-worker.pl minion worker -m production
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 
use Mojolicious::Lite -signatures;


# Using sqlite for the queue
plugin Minion =&gt; { SQLite =&gt; 'minionjobqueue.db', };



#
# Dummy Slow Running Task
#
app-&gt;minion-&gt;add_task(slow_task =&gt; sub ($job) {

        my $id = $job-&gt;id;
        $job-&gt;app-&gt;log-&gt;info("Slow Running Task Initiated -&gt; ".$id);
        sleep 20;
        $job-&gt;app-&gt;log-&gt;info("Slow Running Task Completed -&gt; ".$id);
        $job-&gt;finish({message =&gt; "Slow Running Task Completed -&gt; ".$id});
});

#
# Dummy High Priority Task
#
app-&gt;minion-&gt;add_task(high_priority =&gt; sub ($job) {

        my $id = $job-&gt;id;
        $job-&gt;app-&gt;log-&gt;info("High Priority Task Initiated -&gt; ".$id);
        sleep 3;
        $job-&gt;app-&gt;log-&gt;info("High Priority Task Completed -&gt; ".$id);
        $job-&gt;finish({message =&gt; "High Priority Task Completed -&gt; ".$id});
});



app-&gt;start;</code></pre>



<h4 class="wp-block-heading">Start the worker</h4>



<p>Now that our Worker script is ready time to start it. You can manually start the worker by running the one of the below commands in terminal:</p>



<pre class="wp-block-code"><code>./minion-worker.pl minion worker

./minion-worker.pl minion worker -m production

nohup ./minion-worker.pl minion worker -m production &amp;</code></pre>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="794" height="208" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minion-1.png" alt="" class="wp-image-537" srcset="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minion-1.png 794w, https://thecurioustechnoid.com/wp-content/uploads/2021/04/minion-1-768x201.png 768w" sizes="(max-width: 794px) 100vw, 794px" /></figure>



<p>In a production environment, I prefer creating it as a systemd service and run it in the background.</p>



<pre class="wp-block-code"><code>&#91;Unit]
Description=Minion Workers for Mojolicious
After=mariadb.service

&#91;Service]
Type=simple
ExecStart=/path/to/your/worker/minion-worker.pl minion worker -m production
ExecReload=/path/to/your/worker/minion-worker.pl minion worker -m production
KillMode=process

&#91;Install]
WantedBy=multi-user.target</code></pre>



<p>Now we have the worker running, ready to process any jobs added to the queue. You can check the status of the jobs in the queue using the command:</p>



<pre class="wp-block-code"><code>./minion-worker.pl minion job</code></pre>



<p>Minion comes with a pre-built Admin console to monitor the jobs that are enqueued and processed. It can be enabled using the plugin:</p>



<pre class="wp-block-code"><code>plugin 'Minion::Admin';</code></pre>



<p>We will see it&#8217;s usage when we build our Mojolicious app for adding jobs to the queue.</p>



<p>Next task would be create means to enqueue the job queue.</p>



<h2 class="wp-block-heading">Job Queue</h2>



<p>There are many ways to add jobs to the job queue, we will look at the below 3 ways:</p>



<ul class="wp-block-list"><li>Mojolicious App</li><li>Perl Script</li><li>Command Line</li></ul>



<h4 class="wp-block-heading">Mojolicious App</h4>



<p>One of the best use cases of using Minions is to call it from Mojolicious Web App to do time consuming or CPU intensive task in the background. We will create a Mojolicious App which will do the below things:</p>



<ol class="wp-block-list"><li>Add new jobs to the Queue</li><li>Enable Minion Admin Console to monitor the job queue</li></ol>



<p>Without further adieu let us quickly create a new Mojolicious app to begin with:</p>



<pre class="wp-block-code"><code>mojo generate app mojominion</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="794" height="658" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionmojoGenerate-1.png" alt="" class="wp-image-549" srcset="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionmojoGenerate-1.png 794w, https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionmojoGenerate-1-768x636.png 768w" sizes="(max-width: 794px) 100vw, 794px" /></figure></div>



<p>In our main library created by the <em>mojo</em> command, we will first add the logic to call the Minion plugin.</p>



<p><code>File: mojominion/lib/mojominion.pm</code></p>



<pre class="wp-block-code"><code>$self-&gt;plugin(Minion =&gt; { SQLite =&gt; 'minionjobqueue.db'});</code></pre>



<p>We will keep it simple and only add routes to:</p>



<ol class="wp-block-list"><li>Add new jobs to the queue</li><li>Navigate to Minion Admin Console</li></ol>



<p>We will not think much about security as of this moment. This is just to show you how to add jobs to the queue, so right now anybody can add jobs to the queue.</p>



<pre class="wp-block-code"><code>  $r-&gt;get('/addjob/slow')-&gt;to(controller =&gt; 'Example', action =&gt; 'addSlowJob');  # Route to add slow job
  $r-&gt;get('/addjob/high')-&gt;to(controller =&gt; 'Example', action =&gt; 'addHighJob');  # Route to add high priority job
  $self-&gt;plugin('Minion::Admin' =&gt; {route =&gt; $r-&gt;any('/admin')});  # Route to navigate to Minion Admin Console</code></pre>



<p>You can find the complete source code <a href="https://github.com/curioustechnoid/Minion" target="_blank" rel="noreferrer noopener">here</a>.</p>



<p>Let us now add the logic in our default controller to push the job to the queue.</p>



<p><code>File: mojominion/lib/mojominion/Controller/Example.pm</code></p>



<p>Add the below 2 procedures in the controller:</p>



<pre class="wp-block-code"><code># Subroutine to add slow jobs
sub addSlowJob ($self) {

   my $jobid = $self-&gt;minion-&gt;enqueue(slow_task =&gt; &#91;]);

  $self-&gt;render(text =&gt; 'Your Slow Task is Added to the queue, Job ID: '.$jobid.'. &lt;a href="/admin"&gt;Click here&lt;/a&gt; to view the status.');
}



# Subroutine to add high priority jobs
sub addHighJob ($self) {

   # 
   # By default priority is set to 0, anything more than that gets priority.
   # You can have values between -100 to 100
   #
   my $jobid = $self-&gt;minion-&gt;enqueue(high_priority =&gt; &#91;] =&gt; {priority =&gt; 10});
   # my $jobid = $self-&gt;minion-&gt;enqueue(slow_task =&gt; &#91;]); #Another way of adding the task to the job queue

   $self-&gt;render(text =&gt; 'Your Task is Added to the high priority lane, Job ID: '.$jobid.'. &lt;a href="/admin"&gt;Click here&lt;/a&gt; to view the status.');

}</code></pre>



<p>Task given high priority will get picked up first once the worker becomes free. You can see this first hand in a video demo that I created in <a href="https://www.youtube.com/watch?v=Icc9YOOF414" target="_blank" rel="noreferrer noopener">youtube</a>.</p>



<p>You can find the complete source code of the above codes <a href="https://github.com/curioustechnoid/Minion" target="_blank" rel="noreferrer noopener">here</a>.</p>



<h5 class="wp-block-heading">Time to Test</h5>



<p>We created the worker, started it and then created a Mojolicious App that can handle job requests. Let us fire it up and see if it works.</p>



<pre class="wp-block-code"><code>morbo mojominion/script/mojominion</code></pre>



<p>Let us first access the admin console to see if that is accessible:</p>



<p><code>http://127.0.0.1:3000/admin</code></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1400" height="958" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/miniondashboard1.png" alt="" class="wp-image-539" srcset="https://thecurioustechnoid.com/wp-content/uploads/2021/04/miniondashboard1.png 1400w, https://thecurioustechnoid.com/wp-content/uploads/2021/04/miniondashboard1-768x526.png 768w" sizes="auto, (max-width: 1400px) 100vw, 1400px" /></figure>



<p>Admin console is working fine and we can see that we have 1 worker running. Time for us add some jobs by using the below URLs (keep the admin console open):</p>



<p><code>http://127.0.0.1:3000/addjob/slow</code><br><code>http://127.0.0.1:3000/addjob/high</code></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="569" height="217" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionslowtask-1.png" alt="" class="wp-image-546"/></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="574" height="212" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionhightask.png" alt="" class="wp-image-541"/></figure></div>



<p>Open the URL multiple times to add more jobs. If we head back to our admin console we can see our job getting executed.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1402" height="955" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/miniondashboard2.png" alt="" class="wp-image-542" srcset="https://thecurioustechnoid.com/wp-content/uploads/2021/04/miniondashboard2.png 1402w, https://thecurioustechnoid.com/wp-content/uploads/2021/04/miniondashboard2-768x523.png 768w" sizes="auto, (max-width: 1402px) 100vw, 1402px" /></figure>



<p>Just like that we have successfully implemented Minions using Mojolicious.</p>



<h4 class="wp-block-heading">Perl Script</h4>



<p>Can we use Minions without Mojolicious ? Most definitely. We will keep the above Mojolicious app running to access the admin console and then try to add a job using the below plain Perl script:</p>



<pre class="wp-block-code"><code>#!/usr/bin/env perl
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# add-minion-job.pl
#
# AUTHOR:  Curious Technoid
# DATE:  20-Apr-2021
# VERSION:  1.0
# PURPOSE:
#           This perl script is used to add a new job to
#           minion queue.
#
# USAGE: 
#       ./add-minion-job.pl slow
#       ./add-minion-job.pl fast
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 
use Minion;


# Do you notice we are using the same database
my $minion = Minion-&gt;new( SQLite =&gt; 'minionjobqueue.db');


# Getting the parameter
my $param = shift;


if(lc $param eq 'slow'){

    # Add slow task to the queue
    my $jobid = $minion-&gt;enqueue(slow_task =&gt; &#91;]);

    print("Your Slow Task is Added to the queue from perl, Job ID: $jobid\n\n");

}elsif(lc $param eq 'fast'){

    # Add high priority task to the queue
    my $jobid = $minion-&gt;enqueue(high_priority =&gt; &#91;] =&gt; {priority =&gt; 10});

    print("Your High Priority Task is Added to the queue from perl, Job ID: $jobid\n\n");
");

}else{

    die("Incorrect parameter\n");
}</code></pre>



<p>Once the script is ready, run it using the below command to add jobs:</p>



<pre class="wp-block-code"><code>./add-minion-job.pl slow
./add-minion-job.pl fast</code></pre>



<p>Now if you head back to the Admin console that is running from our previous example, you can notice that the new jobs added by the Perl script is queued and executed. If you don&#8217;t want to use the Mojolicious Admin console at all, you can always use the command line to check the jobs:</p>



<pre class="wp-block-code"><code>./minion-worker.pl minion job</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="799" height="309" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionjob.png" alt="" class="wp-image-543" srcset="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionjob.png 799w, https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionjob-768x297.png 768w" sizes="auto, (max-width: 799px) 100vw, 799px" /></figure>



<h4 class="wp-block-heading">Command Line</h4>



<p>We can even add jobs to the queue from command line directly. One way is ofcourse to use the pure Perl script above and use that to add jobs from command line. Not only that, you can also add jobs using the worker script(minion-worker.pl) we wrote in the beginning. Below command adds jobs to the queue directly from command line:</p>



<pre class="wp-block-code"><code>./minion-worker.pl minion job -e slow_task</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="795" height="266" src="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionCLI.png" alt="" class="wp-image-545" srcset="https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionCLI.png 795w, https://thecurioustechnoid.com/wp-content/uploads/2021/04/minionCLI-768x257.png 768w" sizes="auto, (max-width: 795px) 100vw, 795px" /></figure>



<p>You can pass arguments like this:</p>



<pre class="wp-block-code"><code>./minion-worker.pl minion job -e slow_task -a '&#91;"param1","param2"]'</code></pre>



<p>This is very useful if you want to schedule jobs using cron.</p>



<div style="height:32px" aria-hidden="true" class="wp-block-spacer"></div>



<p>That&#8217;s about it. We have configured, deployed and tested Mojolicious Minions in this article. If you wish to check out the hands on demonstration about the things we discussed in this blog, check out my video in youtube below.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Mojolicious Minion - High Performance Job Queue" width="640" height="360" src="https://www.youtube.com/embed/Icc9YOOF414?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>You can read more about Minion documentation in the <a href="https://docs.mojolicious.org/Minion" target="_blank" rel="noreferrer noopener">official page here</a>. </p>
<p>The post <a href="https://thecurioustechnoid.com/mojolicious-minion-a-high-performance-job-queue/">Mojolicious Minion &#8211; A High Performance Job Queue</a> appeared first on <a href="https://thecurioustechnoid.com">The Curious Technoid</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thecurioustechnoid.com/mojolicious-minion-a-high-performance-job-queue/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using Disk: Enhanced 
Database Caching 12/43 queries in 0.012 seconds using Disk (Request-wide modification query)

Served from: thecurioustechnoid.com @ 2026-06-19 01:25:54 by W3 Total Cache
-->