[RUBY] I want to perform asynchronous processing and periodic execution with Rail !!!

I want to process asynchronously

The processing of the action is heavy and makes the user wait. .. In such a case, let's cut it out as asynchronous processing.

After that, the command will be executed with the server running.

First add Sidekiq to the gem Sidekiq ... Sounds good. ..

gem 'sidekiq'

And install


When you start Sidekiq by executing the bundle exec sidekiq command ...

bash-4.4$ bundle exec sidekiq
          .ss,  $$:         .,d$
          `$$P,d$P'    .,md$P"'
         $$^' `"/$$$'       ____  _     _      _    _
         $:     ,$$:       / ___|(_) __| | ___| | _(_) __ _
         `b     :$$        \___ \| |/ _` |/ _ \ |/ / |/ _` |
                $$:         ___) | | (_| |  __/   <| | (_| |
                $$         |____/|_|\__,_|\___|_|\_\_|\__, |
              .d$$                                       |_|

Cool ... !! So I was able to start Sidekiq easily.

Now edit config / environments / development.rb to get Rails and Sidekiq working together.


  config.active_job.queue_adapter = :sidekiq

The settings are now complete.

Next, create a job with a command.

bin/rails g job sample

Let's output to Log to check if asynchronous processing is done


class SampleJob < ApplicationJob
  queue_as :default

  def perform(*args)
    Sidekiq::Logging.logger.info "Create a job"

Now you can check if asynchronous processing has been performed. All you have to do is call it where you want to perform asynchronous processing or periodic execution.


#Asynchronous processing
#Specify execution date and time(Run at noon tomorrow)
SampleJob.set(wait_until: Date.tomorrow.noon).perform_later

Asynchronous processing was possible like this. That's it for today!

Recommended Posts

I want to perform asynchronous processing and periodic execution with Rail !!!
I want to perform aggregation processing with spring-batch
I want to perform Group By processing with Stream (group-by-count, group-by-sum, group-by-max)
I want to transition screens with kotlin and java!
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to implement various functions with kotlin and java!
[Java] I want to perform distinct with the key in the object
I want to use DBViewer with Eclipse 2018-12! !!
I want to display images with REST Controller of Java and Spring!
[Active Admin] I want to customize the default create and update processing
I want to test Action Cable with RSpec test
Asynchronous processing with regular execution in Spring Boot
I want to use java8 forEach with index
I want to play with Firestore from Rails
[Rails] I want to load CSS with webpacker
I want to control the start / stop of servers and databases with Alexa
I tried to read and output CSV with Outsystems
I started MySQL 5.7 with docker-compose and tried to connect
I want to monitor a specific file with WatchService
I want to authenticate users to Rails with Devise + OmniAuth
I want to get along with Map [Java beginner]
I want to redirect sound from Ubuntu with xrdp
[Java] I want to write asynchronous processing using Promise in Java-Trial of Promise-like grammar of JavaScript-
I tried to increase the processing speed with spiritual engineering
I want to bring Tomcat to the server and start the application
I want to perform high-speed prime factorization in Ruby (ABC177E)
[jackson] I want to receive JSON values "0" and "1" as boolean
When you want to explicitly write OR or AND with ransack
[Rails] I tried to implement batch processing with Rake task
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to manually send an authorization email with Devise
I want to distinct the duplicated data with has_many through
I want to pass the startup command to postgres with docker-compose.
[Java] I want to test standard input & standard output with JUnit
I want to convert characters ...
Spring with Kotorin --6 Asynchronous processing
Asynchronous processing with Shoryuken + SQS
[Wire Mock] I want to set up a stub / mock server in Java and perform E2E tests.
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
I want to give edit and delete permissions only to the poster
Webpack and webpacker I want to tell Ruby people right now
I want to connect SONY headphones WH-1000XM4 with LDAC on ubuntu 20.04! !!
I want to convert an array to Active Record Relation with Rails
I want to be able to think and write regular expressions myself. ..
I want to hook log file generation / open with log4j # FileAppender
I want to add a browsing function with ruby on rails
I want to understand the flow of Spring processing request parameters
I want to INSERT Spring Local Time with MySQL Time (also milliseconds)
I want to avoid OutOfMemory when outputting large files with POI
I want to operate cron with GUI, so I will install Dkron
I really want to do "new T ()"! (And without inspection exceptions)
[Rails] I want to add data to Params when transitioning with link_to
I want to extract between character strings with a regular expression
Swift: I want to chain arrays
I want to use FormObject well
I want to convert InputStream to String
Processing speed with and without static
I tried to interact with Java
I want to docker-compose up Next.js!
Manually perform processing equivalent to @ConfigurationProperties