<?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: Masashi Salvador Mitsuzawa</title>
    <description>The latest articles on DEV Community by Masashi Salvador Mitsuzawa (@masashisalvador57f).</description>
    <link>https://dev.to/masashisalvador57f</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%2F12631%2F4339408.jpeg</url>
      <title>DEV Community: Masashi Salvador Mitsuzawa</title>
      <link>https://dev.to/masashisalvador57f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/masashisalvador57f"/>
    <language>en</language>
    <item>
      <title>Writing Serial Process with Active Job</title>
      <dc:creator>Masashi Salvador Mitsuzawa</dc:creator>
      <pubDate>Tue, 27 Mar 2018 06:51:38 +0000</pubDate>
      <link>https://dev.to/masashisalvador57f/writing-serial-process-with-active-job-3b31</link>
      <guid>https://dev.to/masashisalvador57f/writing-serial-process-with-active-job-3b31</guid>
      <description>&lt;p&gt;I am using Active Job in Rails for writing Job Queue, I wrote some jobs that issues SQLs and writing data in temporary table, then I'd like to write a whole processor job that serially executes job1, job2, job3...., and jobN and completed a calculation task. &lt;/p&gt;

&lt;p&gt;then, in ActiveJob, we have two methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;job.perform_later() # asynchronous
job.perform_now() # wait for finishing.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so, in my case &lt;code&gt;perform_now&lt;/code&gt; is a solution.&lt;/p&gt;

&lt;h1&gt;
  
  
  Serializaed process with ActiveJob
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# this Job is sleeper
class Job1Job &amp;lt; ApplicationJob
  queue_as :default

  def perform()
    puts "hello1"
    sleep(5)
    puts "after sleep"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# this Job won't sleep
class Job2Job &amp;lt; ApplicationJob
  queue_as :default

  def perform()
    puts "hello2"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# this job is a manager
class Job3Job &amp;lt; ApplicationJob
  queue_as :default

  def perform()
    Job1Job.perform_now()
    Job2Job.perform_now()
    puts "hoge"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the result of execution in rails console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Job3Job.perform_later()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then we obtain&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# bundle exec rake jobs:work
hello1
after sleep
hello2
hoge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;:)&lt;/p&gt;

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