resque-await-migration 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 56f8cf84358b4fa5c1773af7645d46f62acda79aefc2fb25563f5d3258dd974f
4
+ data.tar.gz: bea23dd7eb4a03147d68b4885900032603089352e8bb1a8d32481c9989da66aa
5
+ SHA512:
6
+ metadata.gz: a58901ba51c76227b186bd1da0b5d8eaa5c7c13d854ecfc35a843a2478a4c1570403325774e9db7d5168afe9da029123e985078ce6ed75a5322ecadedc6c4189
7
+ data.tar.gz: 73edfa5ead862a4d008d7867772353832106ecd3e377de05e2e047a99e4ac5156d861f7119bf09f8514596e32efd88345e1a8b1c03b8207ce33547c6409592d4
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 sera
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Resque::Await::Migration
2
+
3
+ This is a Resque Plugin that allows the Resque Worker to wait until the ActiveRecord's migration has done.
4
+
5
+ ## Installation & Usage
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'resque-await-migration'
11
+ ```
12
+
13
+ Just do this, your application's Resque Worker will wait for the migration to complete.
14
+
15
+ ## How it works
16
+
17
+ Resque::Await::Migration monitors whether ActiveRecord migration has been applied or not in a child process.
18
+
19
+ The child process is forked from the ResqueWorker process in the before_first_fork hook of Resque.
20
+ And, terminated when it is confirmed that the migration has been applied.
21
+
22
+ ### Monitoring
23
+
24
+ If the migration has not yet been applied, it sends a USR2 signal to the Resque Worker to stop the process.
25
+ When the migration has been applied, it sends a CONT signal and resumes processing.
26
+
27
+ The monitoring interval and time limit can be specified by the following environment variables.
28
+
29
+ * INTERVAL ... monitoring interval seconds. default: 5(sec)
30
+ * AWAIT_MIGRATION_LIMIT ... Time limit waiting for migration to be applied. default: 3600(sec)
31
+
32
+ ### Logging
33
+
34
+ The execution log is output to Resque.logger.
35
+
36
+ ## License
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,67 @@
1
+ require 'active_record'
2
+ require 'resque'
3
+
4
+ module Resque
5
+ module Await
6
+ module Migration
7
+
8
+ class Controller
9
+ class << self
10
+
11
+ def start
12
+
13
+ return unless (ActiveRecord::Base.connection rescue false)
14
+ return unless ActiveRecord::Base.connection.migration_context.needs_migration?
15
+
16
+ parent_pid = Process.pid
17
+ @_child_pid = fork { self.check_in_forked(parent_pid) }
18
+ @_thread = Process.detach(@_child_pid)
19
+ end
20
+
21
+ def stop_if_alive
22
+ if @_thread && @_thread.alive?
23
+ Process.kill(:KILL, @_child_pid)
24
+ end
25
+ end
26
+
27
+ def check_in_forked(parent_pid)
28
+
29
+ interval = ENV['INTERVAL'] || 5
30
+ timeout_limit = ENV['AWAIT_MIGRATION_LIMIT'] || 60 * 60 # 60 minutes
31
+ usr2_was_sent_at = Time.now.to_i
32
+
33
+ _info_log "Migration is needed. Send signal:USR2 to PID:#{parent_pid}"
34
+ Process.kill(:USR2, parent_pid)
35
+
36
+ begin
37
+ while ActiveRecord::Base.connection.migration_context.needs_migration?
38
+ sleep interval
39
+
40
+ if Time.now.to_i - usr2_was_sent_at > timeout_limit
41
+ _info_log "Time limit for awaiting migration has been exceeded. LIMIT:#{timeout_limit}"
42
+ _info_log "So, abort resque worker."
43
+
44
+ _info_log "Send signal:TERM to PID:#{parent_pid}"
45
+ Process.kill(:TERM, parent_pid)
46
+ return
47
+ end
48
+ end
49
+ _info_log "Migration has finished. Send signal:CONT to PID:#{parent_pid}"
50
+ Process.kill(:CONT, parent_pid)
51
+
52
+ rescue => e
53
+ _info_log "Caught Error: #{e}\n" + e.backtrace.join("\n")
54
+ _info_log "Send signal:CONT to PID:#{parent_pid}"
55
+ Process.kill(:CONT, parent_pid)
56
+ end
57
+ end
58
+
59
+ def _info_log(msg)
60
+ Resque.logger.info msg
61
+ end
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module Resque
2
+ module Await
3
+ module Migration
4
+ VERSION = '0.1.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ require "resque"
2
+ require 'resque/await/migration/controller'
3
+
4
+ module Resque
5
+ module Await
6
+ module Migration
7
+
8
+ def setup
9
+ Resque.before_first_fork do
10
+ Resque::Await::Migration::Controller.start
11
+ end
12
+
13
+ Resque.worker_exit do
14
+ Resque::Await::Migration::Controller.stop_if_alive
15
+ end
16
+ end
17
+ module_function :setup
18
+
19
+ end
20
+ end
21
+ end
22
+
23
+ Resque::Await::Migration.setup
@@ -0,0 +1,17 @@
1
+
2
+ namespace :resque_await_migration do
3
+ desc "マイグレーションチェックする処理を実行します"
4
+ task :check_migration => :environment do
5
+ pid = ENV['ORIGIN_PID']
6
+ helper = Resque::Await::Migration::ResqueHelper.new
7
+ helper.check_migration(pid)
8
+ end
9
+
10
+ task :beforeJob => :environment do
11
+ puts "バックグラウンド処理を実行します"
12
+ sh "ORIGIN_PID=#{$$} bundle exec rake resque_await_migration:check_migration &"
13
+ puts "バックグラウンド処理を実行しました"
14
+ end
15
+ end
16
+
17
+ Rake::Task[:'resque:work'].enhance([:'resque_await_migration:beforeJob'])
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-await-migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - bizside-developers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.2.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: 7.2.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 5.2.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: 7.2.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: mocha
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: add rake task for resque to await DB migration of activerecord
90
+ email:
91
+ - bizside-developers@lab.acs-jp.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - MIT-LICENSE
97
+ - README.md
98
+ - lib/resque/await/migration.rb
99
+ - lib/resque/await/migration/controller.rb
100
+ - lib/resque/await/migration/version.rb
101
+ - lib/tasks/check_migration.rake
102
+ homepage: https://github.com/maedadev/resque-await-migration
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.7.6.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: resque await migration
126
+ test_files: []