RailsのActive Jobによる非同期ジョブスケジューリング
非同期処理を行う為に、ここではSidekiqを利用しています。
Sidekiq: https://github.com/mperham/sidekiq/wiki/Active-Job
SidekiqではKey/Value処理を行う為に、Redisを利用しているので準備
shinya@DESKTOP-A5QLPD0:~/$ sudo apt install redis-server [sudo] password for shinya: Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libjemalloc1 redis-tools Suggested packages: ruby-redis The following NEW packages will be installed: libjemalloc1 redis-server redis-tools 0 upgraded, 3 newly installed, 0 to remove and 43 not upgraded. Need to get 634 kB of archives. After this operation, 3012 kB of additional disk space will be used. Do you want to continue? [Y/n] Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libjemalloc1 amd64 3.6.0-11 [82.4 kB] Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 redis-tools amd64 5:4.0.9-1ubuntu0.2 [516 kB] Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 redis-server amd64 5:4.0.9-1ubuntu0.2 [35.4 kB] Fetched 634 kB in 4s (159 kB/s) Selecting previously unselected package libjemalloc1. (Reading database ... 54906 files and directories currently installed.) Preparing to unpack .../libjemalloc1_3.6.0-11_amd64.deb ... Unpacking libjemalloc1 (3.6.0-11) ... Selecting previously unselected package redis-tools. Preparing to unpack .../redis-tools_5%3a4.0.9-1ubuntu0.2_amd64.deb ... Unpacking redis-tools (5:4.0.9-1ubuntu0.2) ... Selecting previously unselected package redis-server. Preparing to unpack .../redis-server_5%3a4.0.9-1ubuntu0.2_amd64.deb ... Unpacking redis-server (5:4.0.9-1ubuntu0.2) ... Setting up libjemalloc1 (3.6.0-11) ... Setting up redis-tools (5:4.0.9-1ubuntu0.2) ... Setting up redis-server (5:4.0.9-1ubuntu0.2) ... invoke-rc.d: could not determine current runlevel Created symlink /etc/systemd/system/redis.service → /lib/systemd/system/redis-server.service. Created symlink /etc/systemd/system/multi-user.target.wants/redis-server.service → /lib/systemd/system/redis-server.service. Processing triggers for libc-bin (2.27-3ubuntu1) ... Processing triggers for systemd (237-3ubuntu10.28) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Processing triggers for ureadahead (0.100.0-21) ... shinya@DESKTOP-A5QLPD0:~/$
Redisを起動
shinya@DESKTOP-A5QLPD0:~/$ redis-server 12214:C 09 Nov 07:40:33.658 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12214:C 09 Nov 07:40:33.661 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=12214, just started 12214:C 09 Nov 07:40:33.662 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 12214:M 09 Nov 07:40:33.665 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 12214 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 12214:M 09 Nov 07:40:37.126 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 12214:M 09 Nov 07:40:37.135 # Server initialized 12214:M 09 Nov 07:40:37.143 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 12214:M 09 Nov 07:40:37.145 * Ready to accept connections
Sidekiqのインストール
shinya@DESKTOP-A5QLPD0:~/$ vim Gemfile shinya@DESKTOP-A5QLPD0:~/$ cat Gemfile | grep -A2 "Active Job" ### Active Job (A-Sync) gem 'sidekiq' shinya@DESKTOP-A5QLPD0:~/$ shinya@DESKTOP-A5QLPD0:~/$ bundle Fetching gem metadata from https://rubygems.org/............ Resolving dependencies.... Using rake 13.0.0 Using concurrent-ruby 1.1.5 <SNIP> Fetching sidekiq 6.0.3 Installing sidekiq 6.0.3 Bundle complete! 30 Gemfile dependencies, 109 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. shinya@DESKTOP-A5QLPD0:~/$
Sidekiqの起動
shinya@DESKTOP-A5QLPD0:~/$ bundle exec sidekiq m, `$b .ss, $$: .,d$ `$$P,d$P' .,md$P"' ,$$$$$b/md$$$P^' .d$$$$$$/$$$P' $$^' `"/$$$' ____ _ _ _ _ $: ,$$: / ___|(_) __| | ___| | _(_) __ _ `b :$$ \___ \| |/ _` |/ _ \ |/ / |/ _` | $$: ___) | | (_| | __/ <| | (_| | $$ |____/|_|\__,_|\___|_|\_\_|\__, | .d$$ |_| 2019-11-08T22:48:19.284Z pid=12548 tid=oxyucamm4 INFO: Running in ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux] 2019-11-08T22:48:19.286Z pid=12548 tid=oxyucamm4 INFO: See LICENSE and the LGPL-3.0 for licensing details. 2019-11-08T22:48:19.288Z pid=12548 tid=oxyucamm4 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org 2019-11-08T22:48:19.290Z pid=12548 tid=oxyucamm4 INFO: Booting Sidekiq 6.0.3 with redis options {:id=>"Sidekiq-server-PID-12548", :url=>nil} 2019-11-08T22:48:19.296Z pid=12548 tid=oxyucamm4 INFO: Starting processing, hit Ctrl-C to stop
SidekiqをRailsで利用する為の設定を入れる
shinya@DESKTOP-A5QLPD0:~/$ ls config/environments/ development.rb production.rb test.rb shinya@DESKTOP-A5QLPD0:~/$ vim config/environments/development.rb shinya@DESKTOP-A5QLPD0:~/$ cat config/environments/development.rb | grep -A2 "Active Job" # Active Job config.active_job.queue_adapter = :sidekiq shinya@DESKTOP-A5QLPD0:~/$
テストジョブの作成
1) Generatorでジョブのテンプレートを作成
shinya@DESKTOP-A5QLPD0:~/$ bin/rails generate job sample Running via Spring preloader in process 13642 invoke test_unit create test/jobs/sample_job_test.rb create app/jobs/sample_job.rb shinya@DESKTOP-A5QLPD0:~/$ cat test/jobs/sample_job_test.rb require 'test_helper' class SampleJobTest < ActiveJob::TestCase # test "the truth" do # assert true # end end shinya@DESKTOP-A5QLPD0:~/$ cat app/jobs/sample_job.rb class SampleJob < ApplicationJob queue_as :default def perform(*args) # Do something later end end shinya@DESKTOP-A5QLPD0:~/$ [/SHELL] 2) テストジョブを作成 [SHELL] shinya@DESKTOP-A5QLPD0:~/$ vim app/jobs/sample_job.rb shinya@DESKTOP-A5QLPD0:~/$ cat app/jobs/sample_job.rb class SampleJob < ApplicationJob queue_as :default def perform(*args) # Do something later Sidekiq::Logging.logger.info "Sidekiqのテストジョブ実行" end end shinya@DESKTOP-A5QLPD0:~/$ [/SHELL] 3) ジョブをCALLする [SHELL] shinya@DESKTOP-A5QLPD0:~/$ cat app/controllers/document_controller.rb | grep Sample SampleJob.perform_later #SampleJob.set(wait: 3.seconds).perform_later #SampleJob.set(wait: 1.week).perform_later shinya@DESKTOP-A5QLPD0:~/$ [/SHELL] <strong>特定処理を実行後の非同期処理とログの確認</strong> [SHELL] ---==_mimepart_5dc5fe832cc3c_36023ffff4bcf99c6866e-- document: {"id"=>180, "name"=>"SidekiqテストASYNCジョブ", "description"=>"SidekiqテストASYNCジョブ", "created_at"=>Sat, 09 Nov 2019 08:47:14 JST +09:00, "updated_at"=>Sat, 09 Nov 2019 08:47:14 JST +09:00, "user_id"=>16} [ActiveJob] Enqueued SampleJob (Job ID: 444d268d-3fee-4fcf-b0d1-3eef7fb9bf8c) to Sidekiq(default) Redirected to http://localhost:3000/document/180 Completed 302 Found in 790ms (ActiveRecord: 3.2ms)
メモ:購入完了や登録メール等の処理を非同期にする事でショッピングカートや登録ページで待ちが発生する事をふせぐのに使えそう。