masamune 0.11.7 → 0.11.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 089f245bbbb77dd701c5179c0ddfd39bd8a6268a
4
- data.tar.gz: 05d83327ec4635886d81c342194de1b2d6215375
3
+ metadata.gz: de2a4fd6bb5a2135bcee2683501ac65a90f91ec9
4
+ data.tar.gz: 9d2ad4ecc076e523b47486a9d03ff4d2089f98f6
5
5
  SHA512:
6
- metadata.gz: c94f89286c6910fe8f84a61966614ea67a645ff30f30fc2f0f16fe3651ff78def7d7c486a31785aae98ffcb66ee39832f84817488573ad8543ca095a39bced1e
7
- data.tar.gz: 6bb39f3a29afdcba70ad493de7c6e0128bf302442a52ea0e3b2f0423ad4d736e4aa82598096b74e4494cdab913085480ff034d3e19a7757fa673f1481869f9e3
6
+ metadata.gz: 5d4f7a56bb6ad4fbff0f0203474a79285e056e9562a5c13ca29c15932e8698c68c0a741078a6604d4d91bbcbe863cf775fbb01ae75b4f094b11b76145994c107
7
+ data.tar.gz: abe67971495a36ec39ff4da7ffe8e45185e45574390861640d0d566a8cbffb1f7c9507ddaa402eb7cf87077c08dadff8fb49e2c647bf377ad96c20beacef2ef1
@@ -53,28 +53,28 @@ module Masamune
53
53
  @mutex ||= Mutex.new
54
54
  end
55
55
 
56
- def with_exclusive_lock(name, options = {}, &block)
56
+ def with_exclusive_lock(name)
57
57
  raise 'filesystem path :run_dir not defined' unless filesystem.has_path?(:run_dir)
58
58
  lock_name = [name, configuration.lock].compact.join(':')
59
59
  logger.debug("acquiring lock '#{lock_name}'")
60
60
  lock_file = lock_file(lock_name)
61
61
  lock_mode = File::LOCK_EX
62
- lock_mode |= File::LOCK_NB if options[:non_blocking]
62
+ lock_mode |= File::LOCK_NB
63
63
  lock_status = lock_file.flock(lock_mode)
64
64
  if lock_status == 0
65
65
  yield if block_given?
66
66
  else
67
- logger.error "acquire lock attempt failed for '#{lock_name}'" unless options[:non_blocking]
67
+ logger.error "acquire lock attempt failed for '#{lock_name}'"
68
68
  end
69
69
  ensure
70
- if lock_file
70
+ if lock_file && lock_status == 0
71
71
  logger.debug("releasing lock '#{lock_name}'")
72
72
  lock_file.flock(File::LOCK_UN)
73
73
  end
74
74
  end
75
75
 
76
76
  def with_process_lock(name)
77
- with_exclusive_lock("#{name}_#{Process.pid}", non_blocking: true) do
77
+ with_exclusive_lock("#{name}_#{Process.pid}") do
78
78
  yield
79
79
  end
80
80
  end
@@ -31,6 +31,7 @@ module Masamune::Tasks
31
31
  # FIXME need to add an unnecessary namespace until this issue is fixed:
32
32
  # https://github.com/wycats/thor/pull/247
33
33
  namespace :elastic_mapreduce
34
+ skip_lock!
34
35
 
35
36
  desc 'elastic_mapreduce', 'Launch an ElasticMapReduce ssh session'
36
37
  class_option :template, :type => :string, :aliases => '-t', :desc => 'Execute named template command'
@@ -32,6 +32,7 @@ module Masamune::Tasks
32
32
  # FIXME need to add an unnecessary namespace until this issue is fixed:
33
33
  # https://github.com/wycats/thor/pull/247
34
34
  namespace :hive
35
+ skip_lock!
35
36
 
36
37
  desc 'hive', 'Launch a Hive session'
37
38
  method_option :file, :aliases => '-f', :desc => 'SQL from files'
@@ -31,6 +31,7 @@ module Masamune::Tasks
31
31
  # FIXME need to add an unnecessary namespace until this issue is fixed:
32
32
  # https://github.com/wycats/thor/pull/247
33
33
  namespace :postgres
34
+ skip_lock!
34
35
 
35
36
  desc 'psql', 'Launch a Postgres session'
36
37
  method_option :file, :aliases => '-f', :desc => 'SQL from files'
@@ -33,6 +33,7 @@ module Masamune::Tasks
33
33
  # FIXME need to add an unnecessary namespace until this issue is fixed:
34
34
  # https://github.com/wycats/thor/pull/247
35
35
  namespace :shell
36
+ skip_lock!
36
37
 
37
38
  desc 'shell', 'Launch an interactive shell'
38
39
  method_option :dump, :type => :boolean, :desc => 'Dump SQL schema', :default => false
data/lib/masamune/thor.rb CHANGED
@@ -103,11 +103,11 @@ module Masamune
103
103
  class_option :lock, :desc => 'Optional job lock name', :type => :string
104
104
  class_option :initialize, :aliases => '--init', :desc => 'Initialize configured data stores', :type => :boolean, :default => false
105
105
  class_option :'--', :desc => 'Extra pass through arguments'
106
- def initialize(_args=[], _options={}, _config={})
106
+ def initialize(_args = [], _options = {}, _config = {})
107
107
  self.environment.parent = self
108
108
  self.filesystem.environment = self
109
109
  self.current_namespace = self.class.namespace unless self.class.namespace == 'masamune'
110
- self.current_task_name = _config[:current_command].name
110
+ self.current_task_name = _config[:current_command].try(:name)
111
111
  self.current_command_name = current_namespace ? current_namespace + ':' + current_task_name : current_task_name
112
112
  self.class.instance = self
113
113
 
@@ -162,16 +162,35 @@ module Masamune
162
162
  end
163
163
 
164
164
  def invoke_command(command, *args)
165
- environment.with_exclusive_lock(command.name, non_blocking: true) do
165
+ return super if self.class.skip_lock?
166
+ lock_name = qualify_task_name(command.name) + '_command'
167
+ environment.with_exclusive_lock(lock_name) do
166
168
  super
167
169
  end
168
170
  end
169
171
 
170
172
  def invoke(name = nil, *args)
171
- environment.with_exclusive_lock(name, non_blocking: top_level?) do
173
+ lock_name = qualify_task_name(name) + '_task'
174
+ environment.with_exclusive_lock(lock_name) do
172
175
  super
173
176
  end
174
177
  end
178
+
179
+ def qualify_task_name(task_name)
180
+ task, namespace = *task_name.split(':').reverse
181
+ namespace ||= current_namespace
182
+ "#{namespace}:#{task.gsub(/_task\Z/, '')}"
183
+ end
184
+
185
+ class << self
186
+ def skip_lock!
187
+ @skip_lock = true
188
+ end
189
+
190
+ def skip_lock?
191
+ @skip_lock
192
+ end
193
+ end
175
194
  end
176
195
 
177
196
  private
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Masamune
24
- VERSION = '0.11.7'
24
+ VERSION = '0.11.8'
25
25
  end
@@ -56,7 +56,7 @@ describe Masamune::Environment do
56
56
  before do
57
57
  instance.filesystem.add_path(:run_dir, run_dir)
58
58
  expect(instance.logger).to receive(:error).with(/acquire lock attempt failed for 'some_lock'/)
59
- expect_any_instance_of(File).to receive(:flock).twice.and_return(1)
59
+ expect_any_instance_of(File).to receive(:flock).once.and_return(1)
60
60
  end
61
61
 
62
62
  it { expect { |b| instance.with_exclusive_lock('some_lock', &b) }.to_not raise_error }
@@ -235,4 +235,37 @@ describe Masamune::Thor do
235
235
  it { is_expected.to eq([[], ['--more']]) }
236
236
  end
237
237
  end
238
+
239
+ context '#qualify_task_name' do
240
+ let(:thor_class) do
241
+ Class.new(Thor) do
242
+ include Masamune::Thor
243
+ end
244
+ end
245
+
246
+ let(:instance) { thor_class.new([], {}, {}) }
247
+
248
+ before do
249
+ expect(instance).to receive(:current_namespace).at_most(:once).and_return('namespace')
250
+ end
251
+
252
+ subject do
253
+ instance.qualify_task_name(name)
254
+ end
255
+
256
+ context 'without namespace' do
257
+ let(:name) { 'other' }
258
+ it { is_expected.to eq('namespace:other') }
259
+ end
260
+
261
+ context 'with namespace' do
262
+ let(:name) { 'namespace:other' }
263
+ it { is_expected.to eq('namespace:other') }
264
+ end
265
+
266
+ context 'with task suffix' do
267
+ let(:name) { 'namespace:other_task' }
268
+ it { is_expected.to eq('namespace:other') }
269
+ end
270
+ end
238
271
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: masamune
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.7
4
+ version: 0.11.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Andrews
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-02 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor