<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kevin Kinsey</title>
    <description>The latest articles on DEV Community by Kevin Kinsey (@dalecosp).</description>
    <link>https://dev.to/dalecosp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F79616%2F537a8bce-76cf-4c09-94f8-ad5b55baee01.jpeg</url>
      <title>DEV Community: Kevin Kinsey</title>
      <link>https://dev.to/dalecosp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dalecosp"/>
    <language>en</language>
    <item>
      <title>Make Cronjobs Take Their Turn</title>
      <dc:creator>Kevin Kinsey</dc:creator>
      <pubDate>Wed, 15 Jan 2020 17:28:49 +0000</pubDate>
      <link>https://dev.to/dalecosp/make-cronjobs-take-their-turn-2502</link>
      <guid>https://dev.to/dalecosp/make-cronjobs-take-their-turn-2502</guid>
      <description>&lt;p&gt;Not every company has racks of servers to devote to a single web property. While we're experiencing record numbers of sessions and pageloads on OMBE.com at present, we've still not decided to bite the bullet and shell out for load-balancing or a CDN and the attendant headaches of pushing everything from single-server orientation into multi-server-ready.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fc1.staticflickr.com%2F3%2F2290%2F2419850566_e802af1520_z.jpg%3Fzz%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fc1.staticflickr.com%2F3%2F2290%2F2419850566_e802af1520_z.jpg%3Fzz%3D1" title="A Server Farm, credit: Darren Mckeeman via Flickr" alt="A Server Farm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, when we began experiencing load spikes without corresponding traffic spikes, we had some investigation to do.&lt;/p&gt;

&lt;p&gt;What we found was that we had several labor-intensive scripts running from &lt;code&gt;cron(8)&lt;/code&gt;, doing DB maintenance and stuff (such as generating static HTML from a large dataset) ... and they were all contending for system resources. Here's what our crontab file used to look like (and this is what a LOT of examples of crontab files look like):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#min    hr      day     month   wkday   command
*/5     *       *       *       *       /etc/scripts/kill_lockfile
5       4       *       *       *       /etc/scripts/dump_db
30      1       */2     *       *       /etc/scripts/make_sitemaps
0,30    *       *       *       *       /etc/scripts/make_all_filters
*/7     *       *       *       *       /etc/scripts/fix_brandnames
55,25   *       *       *       *       /etc/scripts/fix_foo
48,18   *       *       *       *       /etc/scripts/fix_bar
35,5    *       *       *       *       /etc/scripts/create_baz
                                        .... and many more ...

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;make_sitemaps&lt;/code&gt; is DB-intensive, and so are &lt;code&gt;dump_db&lt;/code&gt;, &lt;code&gt;make_all_filters&lt;/code&gt; and &lt;code&gt;fix_brandnames&lt;/code&gt;, and probably some other "fix_this" scripts.  God forbid they ever run at the same time ... but sooner or later, with a &lt;code&gt;crontab(5)&lt;/code&gt; like this, they will, and they &lt;strong&gt;did&lt;/strong&gt;.  It made pages slow to a virtual crawl ... and that's not good for your visitors.&lt;/p&gt;

&lt;p&gt;So, what's the answer? Simplify! &lt;/p&gt;

&lt;p&gt;Here's what our &lt;code&gt;crontab(5)&lt;/code&gt; looks like now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#min    hr      day     month   wkday   command
*/5      *       *       *       *     /etc/scripts/frequent
59       *       *       *       *     /etc/scripts/hourly
02       0       *       *       *     /etc/scripts/daily
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each of these scripts is a wrapper that calls all the jobs we want to do daily, hourly, or frequently &lt;strong&gt;in sequence&lt;/strong&gt;.  So, &lt;em&gt;none&lt;/em&gt; of the jobs run concurrently.&lt;/p&gt;

&lt;p&gt;Now, how do we keep &lt;code&gt;frequent&lt;/code&gt; from running when &lt;code&gt;hourly&lt;/code&gt; is running?  We create and hold a lockfile in &lt;code&gt;/tmp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's the top of &lt;code&gt;daily&lt;/code&gt; --- PLEASE NOTE: this is pseudo-code that resembles the love-child of PHP &amp;amp; Shell (perhaps with a little perl too); you'll want to implement this in your language of choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$lockfile = '/tmp/cronlock';

while (file_exists($lockfile)) {
   sleep 15;
}

echo daily &amp;gt; $lockfile;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the end of &lt;code&gt;daily&lt;/code&gt; we erase our lockfile.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hourly&lt;/code&gt; looks much the same. Although we put a little insurance at the top in case our &lt;code&gt;hourly&lt;/code&gt; jobs take ... a full hour:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//check for the lockfile, sleep if exists
if (file_get_contents($lockfile) == "hourly") {
  // we are waiting on an instance of ourself!
  exit;
}

while (file_exists($lockfile)) {
   sleep(60); // wait for the lockfile to disappear; check every minute
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;frequent&lt;/code&gt; is a little different; since it runs several times an hour, we don't sleep if the lockfile's present.  We just exit ... after all, &lt;code&gt;cron&lt;/code&gt; will run our script again in 5 minutes!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (file_exists($lockfile)) {
    exit();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, now we have all our maintenance scripts playing nice with each other, running in sequence and taking turns.  The server's load average may still spike, but it will be because of &lt;strong&gt;traffic&lt;/strong&gt;, not because of our maintenance jobs.&lt;/p&gt;

&lt;p&gt;Let me know if this is helpful, and have a great WEYTI!&lt;/p&gt;

</description>
      <category>loadaverage</category>
      <category>linux</category>
      <category>cron</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
