<?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: Brian Marete</title>
    <description>The latest articles on DEV Community by Brian Marete (@brianmarete).</description>
    <link>https://dev.to/brianmarete</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%2F692994%2F13b8e94a-77d2-4c97-9676-f2f5d824d343.jpeg</url>
      <title>DEV Community: Brian Marete</title>
      <link>https://dev.to/brianmarete</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brianmarete"/>
    <language>en</language>
    <item>
      <title>Scheduling Jobs on Heroku</title>
      <dc:creator>Brian Marete</dc:creator>
      <pubDate>Mon, 13 Jun 2022 07:41:46 +0000</pubDate>
      <link>https://dev.to/brianmarete/scheduling-jobs-on-heroku-3854</link>
      <guid>https://dev.to/brianmarete/scheduling-jobs-on-heroku-3854</guid>
      <description>&lt;p&gt;&lt;a href="https://elements.heroku.com/addons/scheduler"&gt;Heroku Scheduler&lt;/a&gt; is a free Heroku add-on that runs any command that can be run in your application at scheduled time intervals, similar to cron jobs in a Linux server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;The add-on is free but you will need to use it on a verified Heroku account. You can verify your account by adding a credit card to your account. To install using the Heroku CLI, run this command in your application folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku addons:create scheduler:standard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The add-on can also be installed from the 'Resources' section in the Heroku application dashboard. Just search for 'Heroku Scheduler' and follow the prompts to install the add-on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Once installed, the add-on can be accessed from the Resources section. Click on 'Heroku Scheduler' from your listed add-ons and it will navigate to a new page where you can create your first job.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4o-KxWEt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t7clymji75w94n7gh70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4o-KxWEt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t7clymji75w94n7gh70.png" alt="Scheduler start page" width="880" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This page can also be accessed from from the CLI&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku addons:open scheduler
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clicking on 'Create job' opens a form to the side&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YlLTRU-S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxbtipda2too8llhdo21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YlLTRU-S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxbtipda2too8llhdo21.png" alt="Job editor" width="800" height="1702"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Suppose we have a script called &lt;code&gt;emailUpdates.js&lt;/code&gt; that sends a daily email to users of our app at 6 PM. We can run this script using the scheduler by&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set schedule to 'Every day at...' 6:00 PM (The default timezone is UTC so you'll need to offset this to your desired time)&lt;/li&gt;
&lt;li&gt;Set command to &lt;code&gt;node emailUpdates.js&lt;/code&gt;. Heroku suggests placing your scripts in a &lt;code&gt;bin&lt;/code&gt; folder so if your script is located there, the command would be &lt;code&gt;node ./bin/emailUpdates.js&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Click save job&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The job to run can be any command that can be run in your application. In a node.js app, this could be a script defined in the &lt;code&gt;scripts&lt;/code&gt; section of your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;p&gt;Scheduled jobs are meant to execute short running tasks or enqueue longer running tasks into a background job queue. Anything that takes longer than a couple of minutes to complete should use a &lt;a href="https://devcenter.heroku.com/articles/background-jobs-queueing"&gt;worker dyno&lt;/a&gt; to run.&lt;/p&gt;

&lt;p&gt;A dyno started by scheduler will not run longer than its scheduling interval. For example, for a job that runs every 10 minutes, dynos will be terminated after running for approximately 10 minutes.&lt;/p&gt;

&lt;p&gt;So that's how schedule simple tasks on Heroku. For more details, you can view the official documentation &lt;a href="https://devcenter.heroku.com/articles/scheduler"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>heroku</category>
    </item>
  </channel>
</rss>
