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
bundle
When you start Sidekiq by executing the bundle exec sidekiq command ...
bash-4.4$ bundle exec sidekiq
m,
`$b
.ss, $$: .,d$
`$$P,d$P' .,md$P"'
,$$$$$b/md$$$P^'
.d$$$$$$/$$$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/environments/development.rb
...
config.active_job.queue_adapter = :sidekiq
end
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
sample_job.rb
class SampleJob < ApplicationJob
queue_as :default
def perform(*args)
Sidekiq::Logging.logger.info "Create a job"
end
end
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.
controller.rb
#Asynchronous processing
SampleJob.perform_later
#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