Yes, it's the 26th day at the earliest. Today's theme
I will do it.
DockerFile
First, add the description of redis to the docker file.
The image and port number are specified in the redis: part. After that, I specified redis_url in environment.
version: '3'
services:
db:
image: postgres:11.2-alpine
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: /bin/sh
environment:
WEBPACKER_DEV_SERVER_HOST: "0.0.0.0"
RAILS_SERVE_STATIC_FILES: "1"
EDITOR: "vim"
REDIS_URL: redis://redis:6379
volumes:
- ./apps:/apps
ports:
- "3000:3000"
- "3035:3035"
depends_on:
- db
tty: true
redis:
image: redis:latest
ports:
- 6379:6379
The Gemfile looks like this.
Gemfile
gem 'redis-rails' gem'sidekiq' is added. Since an error occurred when sidekiq was updated, the version is specified as follows. gem 'sidekiq', "~> 5.0"
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "2.6.4"
gem "rails", "~> 6.0.0"
gem "pg", ">= 0.18", "< 2.0"
gem "puma", "~> 3.11"
gem "sass-rails", "~> 5"
gem "webpacker", "~> 4.0"
gem "turbolinks", "~> 5"
gem "jbuilder", "~> 2.7"
gem "bootsnap", ">= 1.4.2", require: false
gem "bcrypt"
gem "rails-i18n"
gem "kaminari"
gem "date_validator"
gem "valid_email2"
gem "nokogiri"
gem "slim-rails"
gem "html2slim"
#to add
gem 'redis-rails'
gem 'sidekiq', "~> 5.0"
...
config
Now add the following settings to config / initializers / redis.rb. You have now added the redis URL.
redis.rb
REDIS ||= Redis.new(url: ENV['REDIS_URL'] || 'redis://localhost:6379')
And add sidekiq settings to config / environment / development.rb
config/environment/development.rb
config.active_job.queue_adapter = :sidekiq
Now that we've modified the Gemfile, run the bundle command
$ bundle
Then run the bundle exec sidekiq command to start Sidekiq.
$ bundle exec sidekiq
bash-4.4$ bundle exec sidekiq
m,
`$b
.ss, $$: .,d$
`$$P,d$P' .,md$P"'
,$$$$$bmmd$$$P^'
.d$$$$$$$$$$P'
$$^' `"^$$$' ____ _ _ _ _
$: ,$$: / ___|(_) __| | ___| | _(_) __ _
`b :$$ \___ \| |/ _` |/ _ \ |/ / |/ _` |
$$: ___) | | (_| | __/ <| | (_| |
$$ |____/|_|\__,_|\___|_|\_\_|\__, |
.d$$ |_|
I got up properly. From here, I will write asynchronous processing.
$ bin/rails g job sample
Running via Spring preloader in process 22386
invoke test_unit
create...
The task is now generated. The app / jobs / sample_job.rb file has been created. This time, the task will write a task to display a character string in log.
class SampleJob < ApplicationJob
queue_as :default
def perform(*args)
Sidekiq::Logging.logger.info "-----I ran a sample job.-----"
end
end
I will write like this. Since the outer frame is created by rails g job, each part is actually only the Sidekiq :: Logging ... line.
Call job on the controller.
A log output job is executed asynchronously using a method called perform_later.
controller.rb
class Admin::TopController < Admin::Base
def index
SampleJob.perform_later
end
end
When you access the index screen and check the log of sidekiq ..
2020-06-15T11:30:52.383Z 15 TID-gphk3l13j SampleJob JID-631a85bb82f73f2a07beb772 INFO: ---------I ran a sample job.-------------
You can see that the job is running asynchronously like this!
You can also specify the date and time to execute. You can use the set method to run the job at noon the next day.
SampleJob.set(wait_until: Date.tomorrow.noon).perform_later
That's all for asynchronous processing using sidekiq after connecting to redis with docker. Thank you very much.
** 74 days left until full-fledged **
Recommended Posts