tengine_core 0.5.39 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. data/Gemfile +1 -37
  2. data/Gemfile.lock +59 -61
  3. data/README.md +8 -6
  4. data/bin/create_indexes_for_tengine_core +18 -0
  5. data/bin/tengined +3 -3
  6. data/examples/list.txt +36 -0
  7. data/examples/uc08_if_both_a_and_b_occurs.rb +7 -1
  8. data/examples/uc20_fire_with_properties.rb +19 -0
  9. data/examples/uc21_fire_with_same_key.rb +23 -0
  10. data/examples/uc22_fire_with_source_name.rb +17 -0
  11. data/examples/uc23_fire_with_level.rb +19 -0
  12. data/examples/uc24_fire_with_level_key.rb +19 -0
  13. data/examples/uc30_local_variables.rb +28 -0
  14. data/examples/uc31_instance_variables.rb +35 -0
  15. data/examples/uc32_class_variables.rb +20 -0
  16. data/examples/uc33_statics.rb +20 -0
  17. data/examples2/uc30_local_variables.rb +55 -0
  18. data/examples2/uc31_instance_variables.rb +46 -0
  19. data/examples2/uc32_class_variables.rb +22 -0
  20. data/examples2/uc33_statics.rb +23 -0
  21. data/lib/tengine/core/bootstrap.rb +1 -1
  22. data/lib/tengine/core/config/atd.rb +0 -3
  23. data/lib/tengine/core/config/core.rb +21 -20
  24. data/lib/tengine/core/config/heartbeat_watcher.rb +0 -3
  25. data/lib/tengine/core/driveable.rb +34 -12
  26. data/lib/tengine/core/driver.rb +22 -9
  27. data/lib/tengine/core/dsl_evaluator.rb +2 -15
  28. data/lib/tengine/core/dsl_filter_def.rb +40 -0
  29. data/lib/tengine/core/dsl_loader.rb +1 -1
  30. data/lib/tengine/core/event.rb +44 -29
  31. data/lib/tengine/core/event/finder.rb +1 -1
  32. data/lib/tengine/core/event_exception_reportable.rb +7 -10
  33. data/lib/tengine/core/event_wrapper.rb +1 -1
  34. data/lib/tengine/core/find_by_name.rb +1 -1
  35. data/lib/tengine/core/handler.rb +14 -4
  36. data/lib/tengine/core/handler_path.rb +1 -1
  37. data/lib/tengine/core/heartbeat_watcher.rb +3 -2
  38. data/lib/tengine/core/kernel.rb +22 -6
  39. data/lib/tengine/core/mongoid_fix.rb +2 -1
  40. data/lib/tengine/core/mutex.rb +22 -8
  41. data/lib/tengine/core/optimistic_lock.rb +6 -6
  42. data/lib/tengine/core/safe_updatable.rb +24 -26
  43. data/lib/tengine/core/schedule.rb +2 -2
  44. data/lib/tengine/core/scheduler.rb +5 -4
  45. data/lib/tengine/core/session.rb +14 -0
  46. data/lib/tengine/core/session_wrapper.rb +20 -4
  47. data/lib/tengine/core/setting.rb +3 -3
  48. data/lib/tengine/rspec/extension.rb +6 -8
  49. metadata +156 -171
  50. data/.document +0 -5
  51. data/.rspec +0 -1
  52. data/Rakefile +0 -44
  53. data/VERSION +0 -1
  54. data/lib/tengine/core/connection_test/.gitignore +0 -1
  55. data/spec/factories/tengine_core_drivers.rb +0 -10
  56. data/spec/factories/tengine_core_events.rb +0 -14
  57. data/spec/factories/tengine_core_handler_paths.rb +0 -9
  58. data/spec/factories/tengine_core_handlers.rb +0 -9
  59. data/spec/factories/tengine_core_sessions.rb +0 -9
  60. data/spec/mongoid.yml +0 -35
  61. data/spec/spec_helper.rb +0 -48
  62. data/spec/support/mongo_index_key_log.rb +0 -91
  63. data/spec/tengine/core/bootstrap_spec.rb +0 -278
  64. data/spec/tengine/core/bugfix/bind_dsl_file_in_multi_byte_dir_spec.rb +0 -21
  65. data/spec/tengine/core/bugfix/enabled_on_activation_spec.rb +0 -112
  66. data/spec/tengine/core/bugfix/receive_event_spec.rb +0 -133
  67. data/spec/tengine/core/bugfix/use_dsl_version_method.rb +0 -12
  68. data/spec/tengine/core/bugfix/use_dsl_version_method_spec.rb +0 -28
  69. data/spec/tengine/core/bugfix/use_event_in_handler_dsl.rb +0 -11
  70. data/spec/tengine/core/bugfix//351/235/236ACSII/343/201/256/343/203/206/343/202/231/343/202/243/343/203/254/343/202/257/343/203/210/343/203/252/345/220/215/source_location_encoding.rb +0 -35
  71. data/spec/tengine/core/bugfix//351/235/236ACSII/343/201/256/343/203/206/343/202/231/343/202/243/343/203/254/343/202/257/343/203/210/343/203/252/345/220/215//351/235/236ASCII/343/201/256/343/203/225/343/202/241/343/202/244/343/203/253/345/220/215_dsl.rb +0 -38
  72. data/spec/tengine/core/bugfix//351/235/236ACSII/343/201/256/343/203/207/343/202/243/343/203/254/343/202/257/343/203/210/343/203/252/345/220/215/source_location_encoding.rb +0 -35
  73. data/spec/tengine/core/bugfix//351/235/236ACSII/343/201/256/343/203/207/343/202/243/343/203/254/343/202/257/343/203/210/343/203/252/345/220/215//351/235/236ASCII/343/201/256/343/203/225/343/202/241/343/202/244/343/203/253/345/220/215_dsl.rb +0 -38
  74. data/spec/tengine/core/config/atd_spec.rb +0 -62
  75. data/spec/tengine/core/config/core_spec.rb +0 -479
  76. data/spec/tengine/core/config/heartbeat_watcher_spec.rb +0 -62
  77. data/spec/tengine/core/config/syntax_error_in_erb.yml.erb +0 -13
  78. data/spec/tengine/core/config/wrong_category_name.yml.erb +0 -13
  79. data/spec/tengine/core/config/wrong_field_name.yml.erb +0 -12
  80. data/spec/tengine/core/config/wrong_yaml.yml.erb +0 -13
  81. data/spec/tengine/core/config_spec/another_port.yml +0 -54
  82. data/spec/tengine/core/config_spec/config_with_dir_absolute_load_path.yml +0 -16
  83. data/spec/tengine/core/config_spec/config_with_dir_relative_load_path.yml +0 -16
  84. data/spec/tengine/core/config_spec/config_with_file_absolute_load_path.yml +0 -16
  85. data/spec/tengine/core/config_spec/config_with_file_relative_load_path.yml +0 -16
  86. data/spec/tengine/core/config_spec/log_config_spec.rb +0 -245
  87. data/spec/tengine/core/driveable_spec.rb +0 -240
  88. data/spec/tengine/core/driver_spec.rb +0 -159
  89. data/spec/tengine/core/dsl_loader_spec.rb +0 -172
  90. data/spec/tengine/core/dsls/uc08_if_both_a_and_b_occurs_spec.rb +0 -35
  91. data/spec/tengine/core/dsls/uc10_if_the_event_occurs_at_the_server_spec.rb +0 -58
  92. data/spec/tengine/core/dsls/uc50_commit_event_at_first_spec.rb +0 -29
  93. data/spec/tengine/core/dsls/uc52_commit_event_after_all_handler_submit_spec.rb +0 -33
  94. data/spec/tengine/core/dsls/uc52_never_commit_event_unless_all_handler_submit_spec.rb +0 -38
  95. data/spec/tengine/core/dsls/uc53_submit_outside_of_handler_spec.rb +0 -37
  96. data/spec/tengine/core/dsls/uc60_event_in_handler_spec.rb +0 -31
  97. data/spec/tengine/core/dsls/uc61_event_outside_of_handler_spec.rb +0 -37
  98. data/spec/tengine/core/dsls/uc62_session_in_driver_spec.rb +0 -36
  99. data/spec/tengine/core/dsls/uc63_session_outside_of_driver_spec.rb +0 -35
  100. data/spec/tengine/core/dsls/uc64_safety_countup_spec.rb +0 -134
  101. data/spec/tengine/core/dsls/uc70_driver_enabled_on_activation_spec.rb +0 -39
  102. data/spec/tengine/core/dsls/uc71_driver_disabled_on_activation_spec.rb +0 -36
  103. data/spec/tengine/core/dsls/uc72_setup_eventmachine_spec.rb +0 -39
  104. data/spec/tengine/core/dsls/uc80_raise_io_error_spec.rb +0 -53
  105. data/spec/tengine/core/dsls/uc81_raise_runtime_error_spec.rb +0 -49
  106. data/spec/tengine/core/event/finder_spec.rb +0 -136
  107. data/spec/tengine/core/event_exception_reportable_spec.rb +0 -33
  108. data/spec/tengine/core/event_spec.rb +0 -161
  109. data/spec/tengine/core/event_wrapper_spec.rb +0 -35
  110. data/spec/tengine/core/handler_path_spec.rb +0 -87
  111. data/spec/tengine/core/handler_spec.rb +0 -190
  112. data/spec/tengine/core/heartbeat_watcher_spec.rb +0 -131
  113. data/spec/tengine/core/io_to_logger_spec.rb +0 -30
  114. data/spec/tengine/core/kernel_spec.rb +0 -902
  115. data/spec/tengine/core/mongodb_spec.rb +0 -12
  116. data/spec/tengine/core/mongoid_fix_spec.rb +0 -52
  117. data/spec/tengine/core/mutex_spec.rb +0 -201
  118. data/spec/tengine/core/optimistic_lock_spec.rb +0 -55
  119. data/spec/tengine/core/scheculer_spec.rb +0 -121
  120. data/spec/tengine/core/selectable_attr_spec.rb +0 -30
  121. data/spec/tengine/core/session_spec.rb +0 -104
  122. data/spec/tengine/core/setting_spec.rb +0 -79
  123. data/spec/tengine/core_spec.rb +0 -13
  124. data/spec/tengine_spec.rb +0 -14
  125. data/tengine_core.gemspec +0 -251
  126. data/tmp/log/.gitignore +0 -1
  127. data/tmp/tengined_status/.gitignore +0 -1
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tengine/core'
3
+
4
+ # driverメソッドによるドライバ定義でインスタンス変数を使用する例。
5
+ #
6
+ # driverメソッドでは内部で Tengine::Core::Driveableモジュールをincludeした
7
+ # クラスを定義します。そのためインスタンス変数のスコープはクラスのスコープと同じです。
8
+ #
9
+ # 以下の例ではおかしく見えるかもしれませんが、Tengine::Core::Driveableモジュールを
10
+ # 使った定義をみれば納得して頂けると思います。
11
+ # https://github.com/tengine/tengine/blob/develop/tengine_core/examples2/uc31_instance_variables.rb
12
+
13
+ @ivar1 = "outside of driver"
14
+
15
+ driver :driver31 do
16
+
17
+ @ivar2 = "outside of handler"
18
+ puts "#{__FILE__}##{__LINE__}"
19
+ puts @ivar1.inspect # => nil
20
+
21
+ def initialize
22
+ @ivar3 = "inside of initialize"
23
+ puts "#{__FILE__}##{__LINE__}"
24
+ puts @ivar1.inspect # => nil
25
+ puts @ivar2.inspect # => nil
26
+ end
27
+
28
+ on:event31 do
29
+ puts "#{__FILE__}##{__LINE__}"
30
+ puts @ivar1.inspect # => nil
31
+ puts @ivar2.inspect # => nil
32
+ puts @ivar3.inspect # => inside of initialize
33
+ end
34
+
35
+ end
@@ -0,0 +1,20 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tengine/core'
3
+
4
+ # driverメソッドによるドライバ定義でクラス変数を使用する例。
5
+
6
+ @@cvar1 = "outside of driver"
7
+
8
+ driver :driver32 do
9
+
10
+ @@cvar2 = "outside of handler"
11
+ puts "#{__FILE__}##{__LINE__}"
12
+ puts @@cvar1.inspect # => outside of driver
13
+
14
+ on:event32 do
15
+ puts "#{__FILE__}##{__LINE__}"
16
+ puts @@cvar1.inspect # => outside of driver
17
+ puts @@cvar2.inspect # => outside of handler
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tengine/core'
3
+
4
+ # driverメソッドによるドライバ定義で定数を使用する例。
5
+
6
+ STATIC1 = "outside of driver"
7
+
8
+ driver :driver33 do
9
+
10
+ STATIC2 = "outside of handler"
11
+ puts "#{__FILE__}##{__LINE__}"
12
+ puts STATIC1 # => outside of driver
13
+
14
+ on:event33 do
15
+ puts "#{__FILE__}##{__LINE__}"
16
+ puts STATIC1 # => outside of driver
17
+ puts STATIC2 # => outside of handler
18
+ end
19
+
20
+ end
@@ -0,0 +1,55 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tengine/core'
3
+
4
+ # Tengine::Core::Driveableモジュールによるドライバ定義でローカル変数を使用する例。
5
+ #
6
+ # ドライバはRubyのクラスとして定義されるので、スコープの異なるローカル変数を
7
+ # 参照する事はできません。
8
+ #
9
+ # ドライバ外に定義したデータをドライバ内、イベントハンドラ内で使用したい場合には
10
+ # 定数やクラス変数の利用を検討してください。
11
+ # https://github.com/tengine/tengine/blob/develop/tengine_core/examples2/uc33_statics.rb
12
+ # https://github.com/tengine/tengine/blob/develop/tengine_core/examples2/uc32_class_variables.rb
13
+ #
14
+ # ローカル変数を用いてデータを共有したい場合は、driverメソッドによるドライバ定義を検討してください。
15
+ # https://github.com/tengine/tengine/blob/develop/tengine_core/examples/uc30_local_variables.rb
16
+
17
+ # lvar1の定義
18
+ lvar1 = "outside of driver"
19
+
20
+ class Driver30
21
+ include Tengine::Core::Driveable
22
+
23
+ # lvar2の定義
24
+ lvar2 = "outside of handler"
25
+ puts "#{__FILE__}##{__LINE__}"
26
+
27
+ begin
28
+ # lvar1へのアクセス
29
+ puts lvar1 # => raise NameError: undefined local variable or method `lvar1'
30
+ raise "some thing wrong"
31
+ rescue NameError => e
32
+ puts "#{e.class} #{e.message}"
33
+ end
34
+
35
+ on:event30
36
+ def puts_local_variables
37
+ puts "#{__FILE__}##{__LINE__}"
38
+ begin
39
+ # lvar1へのアクセス
40
+ puts lvar1 # => raise NameError: undefined local variable or method `lvar1'
41
+ raise "some thing wrong"
42
+ rescue NameError => e
43
+ puts "#{e.class} #{e.message}"
44
+ end
45
+
46
+ begin
47
+ # lvar2へのアクセス
48
+ puts lvar2 # => raise NameError: undefined local variable or method `lvar2'
49
+ raise "some thing wrong"
50
+ rescue NameError => e
51
+ puts "#{e.class} #{e.message}"
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,46 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tengine/core'
3
+
4
+ # Tengine::Core::Driveableモジュールによるドライバ定義でインスタンス変数を使用する例。
5
+ #
6
+ # @ivar1
7
+ # クラスの外で定義したインスタンス変数なので、クラス内のどこからも参照できません。
8
+ #
9
+ # @ivar2
10
+ # クラスのコンテキスト中で定義したインスタンス変数なので、Driverクラスオブジェクト自身のインスタンス変数であり、
11
+ # Driverのインスタンスのインスタンス変数ではないので、メソッドから参照できません。
12
+ #
13
+ # @ivar3
14
+ # initializeで定義したインスタンス変数は、Driverクラスオブジェクト自身のインスタンス変数ではなく、
15
+ # Driverのインスタンスのインスタンス変数なので、メソッドから参照できます。
16
+ #
17
+ # ドライバ外に定義したデータをドライバ内、イベントハンドラ内で使用したい場合には
18
+ # 定数やクラス変数の利用を検討してください。
19
+ # https://github.com/tengine/tengine/blob/develop/tengine_core/examples2/uc33_statics.rb
20
+ # https://github.com/tengine/tengine/blob/develop/tengine_core/examples2/uc32_class_variables.rb
21
+
22
+ @ivar1 = "outside of driver"
23
+
24
+ class Driver31
25
+ include Tengine::Core::Driveable
26
+
27
+ @ivar2 = "outside of handler"
28
+ puts "#{__FILE__}##{__LINE__}"
29
+ puts @ivar1.inspect # => nil
30
+
31
+ def initialize
32
+ @ivar3 = "inside of initialize"
33
+ puts "#{__FILE__}##{__LINE__}"
34
+ puts @ivar1.inspect # => nil
35
+ puts @ivar2.inspect # => nil
36
+ end
37
+
38
+ on:event31
39
+ def puts_instance_variables
40
+ puts "#{__FILE__}##{__LINE__}"
41
+ puts @ivar1.inspect # => nil
42
+ puts @ivar2.inspect # => nil
43
+ puts @ivar3.inspect # => inside of initialize
44
+ end
45
+
46
+ end
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tengine/core'
3
+
4
+ # Tengine::Core::Driveableモジュールによるドライバ定義でクラス変数を使用する例。
5
+
6
+ @@cvar1 = "outside of driver"
7
+
8
+ class Driver32
9
+ include Tengine::Core::Driveable
10
+
11
+ @@cvar2 = "outside of handler"
12
+ puts "#{__FILE__}##{__LINE__}"
13
+ puts @@cvar1.inspect # => outside of driver
14
+
15
+ on:event32
16
+ def puts_class_variables
17
+ puts "#{__FILE__}##{__LINE__}"
18
+ puts @@cvar1.inspect # => outside of driver
19
+ puts @@cvar2.inspect # => outside of handler
20
+ end
21
+
22
+ end
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tengine/core'
3
+
4
+ # Tengine::Core::Driveableモジュールによるドライバ定義で定数を使用する例。
5
+
6
+ STATIC1 = "outside of driver"
7
+
8
+ class Driver33
9
+ include Tengine::Core::Driveable
10
+
11
+ STATIC2 = "outside of handler"
12
+ puts "#{__FILE__}##{__LINE__}"
13
+ puts STATIC1 # => outside of driver
14
+
15
+ # イベントに対応する処理の実行する
16
+ on:event33
17
+ def puts_statics
18
+ puts "#{__FILE__}##{__LINE__}"
19
+ puts STATIC1 # => outside of driver
20
+ puts STATIC2 # => outside of handler
21
+ end
22
+
23
+ end
@@ -36,7 +36,7 @@ class Tengine::Core::Bootstrap
36
36
  end
37
37
 
38
38
  def load_dsl
39
- if dsl_version_document = Tengine::Core::Setting.first(:conditions => {:name => "dsl_version"})
39
+ if dsl_version_document = Tengine::Core::Setting.where(:name => "dsl_version").first
40
40
  dsl_version_document.value = config.dsl_version
41
41
  dsl_version_document.save!
42
42
  else
@@ -3,7 +3,6 @@ require 'tengine/core/config'
3
3
 
4
4
  require 'yaml'
5
5
  require 'optparse'
6
- require 'active_support/memoizable'
7
6
 
8
7
  require 'tengine/support/yaml_with_erb'
9
8
 
@@ -14,8 +13,6 @@ Tengine::Support::Config::Definition::Group.module_eval do
14
13
  end
15
14
 
16
15
  class Tengine::Core::Config::Atd < Tengine::Support::Config::Definition::Suite
17
- # memoize については http://wota.jp/ac/?date=20081025#p11 などを参照してください
18
- extend ActiveSupport::Memoizable
19
16
 
20
17
  class << self
21
18
  # この辺は以前のTengine::Core::Configとの互換のために用意してあります
@@ -3,7 +3,6 @@ require 'tengine/core/config'
3
3
 
4
4
  require 'yaml'
5
5
  require 'optparse'
6
- require 'active_support/memoizable'
7
6
  require 'active_support/core_ext/class/attribute_accessors'
8
7
 
9
8
  require 'tengine/support/yaml_with_erb'
@@ -15,8 +14,6 @@ Tengine::Support::Config::Definition::Group.module_eval do
15
14
  end
16
15
 
17
16
  class Tengine::Core::Config::Core < Tengine::Support::Config::Definition::Suite
18
- # memoize については http://wota.jp/ac/?date=20081025#p11 などを参照してください
19
- extend ActiveSupport::Memoizable
20
17
 
21
18
  class << self
22
19
  # この辺は以前のTengine::Core::Configとの互換のために用意してあります
@@ -228,12 +225,15 @@ EOS
228
225
  field :expire , "heartbeat expire seconds" , :type => :integer, :default => 120
229
226
  end
230
227
 
231
- def dsl_load_path
232
- original = self[:tengined][:load_path]
233
- # 本来は指定する必要はありませんが、specでDir.pwdをstubで返すようにするために、明示的に第2引数にDir.pwdを指定しています
234
- original ? File.expand_path(original, Dir.pwd) : nil
228
+ def dsl_load_path(clear_cache = false)
229
+ @dsl_load_path = nil if clear_cache
230
+ unless @dsl_load_path
231
+ original = self[:tengined][:load_path]
232
+ # 本来は指定する必要はありませんが、specでDir.pwdをstubで返すようにするために、明示的に第2引数にDir.pwdを指定しています
233
+ @dsl_load_path = original ? File.expand_path(original, Dir.pwd) : nil
234
+ end
235
+ @dsl_load_path
235
236
  end
236
- memoize :dsl_load_path
237
237
 
238
238
  def prepare_dir_and_paths(force = false)
239
239
  return if !force && @prepare_dir_and_paths_done
@@ -265,16 +265,20 @@ EOS
265
265
  end
266
266
 
267
267
  def dsl_version_path
268
- path = dsl_dir_path
269
- path ? File.expand_path("VERSION", path) : nil
268
+ unless @dsl_version_path
269
+ path = dsl_dir_path
270
+ @dsl_version_path = path ? File.expand_path("VERSION", path) : nil
271
+ end
272
+ @dsl_version_path
270
273
  end
271
- memoize :dsl_version_path
272
274
 
273
275
  def dsl_version
274
- path = dsl_version_path
275
- (path && File.exist?(dsl_version_path)) ? File.read(dsl_version_path).strip : Time.now.strftime("%Y%m%d%H%M%S")
276
+ unless @dsl_version
277
+ path = dsl_version_path
278
+ @dsl_version = (path && File.exist?(dsl_version_path)) ? File.read(dsl_version_path).strip : Time.now.strftime("%Y%m%d%H%M%S")
279
+ end
280
+ @dsl_version
276
281
  end
277
- memoize :dsl_version
278
282
 
279
283
  def relative_path_from_dsl_dir(filepath)
280
284
  path = Pathname.new(filepath)
@@ -282,19 +286,16 @@ EOS
282
286
  end
283
287
 
284
288
  def status_dir
285
- self[:tengined][:status_dir]
289
+ @status_dir ||= self[:tengined][:status_dir]
286
290
  end
287
- memoize :status_dir
288
291
 
289
292
  def activation_dir
290
- self[:tengined][:activation_dir]
293
+ @activation_dir ||= self[:tengined][:activation_dir]
291
294
  end
292
- memoize :activation_dir
293
295
 
294
296
  def confirmation_threshold
295
- Tengine::Event::LEVELS_INV[ self[:tengined][:confirmation_threshold].to_sym ]
297
+ @confirmation_threshold ||= Tengine::Event::LEVELS_INV[ self[:tengined][:confirmation_threshold].to_sym ]
296
298
  end
297
- memoize :confirmation_threshold
298
299
 
299
300
  def heartbeat_period
300
301
  # [:][:heartbeat_period].to_i
@@ -3,7 +3,6 @@ require 'tengine/core/config'
3
3
 
4
4
  require 'yaml'
5
5
  require 'optparse'
6
- require 'active_support/memoizable'
7
6
 
8
7
  require 'tengine/support/yaml_with_erb'
9
8
 
@@ -14,8 +13,6 @@ Tengine::Support::Config::Definition::Group.module_eval do
14
13
  end
15
14
 
16
15
  class Tengine::Core::Config::HeartbeatWatcher < Tengine::Support::Config::Definition::Suite
17
- # memoize については http://wota.jp/ac/?date=20081025#p11 などを参照してください
18
- extend ActiveSupport::Memoizable
19
16
 
20
17
  class << self
21
18
  # この辺は以前のTengine::Core::Configとの互換のために用意してあります
@@ -32,7 +32,7 @@ module Tengine::Core::Driveable
32
32
  :enabled_on_activation => options[:enabled_on_activation].nil? || options[:enabled_on_activation], # DSLに記述されているオプション
33
33
  :target_class_name => self.name,
34
34
  })
35
- driver.create_session
35
+ driver.session_id = Tengine::Core::Driveable.__find_session_id_for_driver_name__(driver.name)
36
36
  begin
37
37
  driver.save!
38
38
  rescue Mongoid::Errors::Validations => e
@@ -61,7 +61,7 @@ module Tengine::Core::Driveable
61
61
  :target_method_name => method_name.to_s
62
62
  }.update(options))
63
63
  args.each do |event_type_name|
64
- driver.handler_paths.create!(:event_type_name => event_type_name, :handler_id => handler.id)
64
+ driver.handler_paths.find_or_create_by(:event_type_name => event_type_name, :handler_id => handler.id)
65
65
  end
66
66
  end
67
67
 
@@ -104,7 +104,7 @@ module Tengine::Core::Driveable
104
104
  :name => self.driver_name, # self.name.gsub(/:/, 'Colon'),
105
105
  :version => Tengine::Core::Setting.dsl_version
106
106
  }
107
- @driver = Tengine::Core::Driver.first(:conditions => driver_attrs)
107
+ @driver = Tengine::Core::Driver.where(driver_attrs).first
108
108
  end
109
109
  @driver
110
110
  end
@@ -129,18 +129,21 @@ module Tengine::Core::Driveable
129
129
  event_type_names = filter_def.event_type_names
130
130
  base_method_name = event_type_names.map(&:to_s).join("_")
131
131
  driver = context.driver
132
+
132
133
  driver.reload
133
- handler = driver.handlers.new({
134
- :event_type_names => event_type_names,
135
- :target_instantiation_key => :instance_method,
136
- }.update(options))
137
- # フィルタ付きの場合は単純なイベントハンドラ名だけではメソッド名として表現できないので
138
- # handler自身のIDをメソッド名に含めます。
139
- method_name = "#{base_method_name}_#{handler.id.to_s}"
140
- handler.target_method_name = method_name.to_s
134
+ method_name = filter_def.to_method_name
135
+ attrs = {
136
+ :target_method_name => method_name,
137
+ :event_type_names => event_type_names,
138
+ # :target_instantiation_key => :instance_method, # attrsを検索にも使うので_cdの方を指定しています
139
+ :target_instantiation_cd => Tengine::Core::Handler.target_instantiation_id_by_key(:instance_method),
140
+ } # .update(options)
141
+ handler = driver.handlers.find_or_initialize_by(attrs)
142
+ options.each{|k,v| handler.send("#{k}=", v)}
141
143
  handler.save!
144
+
142
145
  event_type_names.each do |event_type_name|
143
- driver.handler_paths.create!(:event_type_name => event_type_name, :handler_id => handler.id)
146
+ driver.handler_paths.find_or_create_by(:event_type_name => event_type_name, :handler_id => handler.id)
144
147
  end
145
148
  else
146
149
  method_name = event_type_names.map(&:to_s).join("_")
@@ -210,4 +213,23 @@ module Tengine::Core::Driveable
210
213
  end
211
214
  end
212
215
 
216
+ class << self
217
+ def __remember_session_ids_for_drivers__(dsl_version)
218
+ @driver_name_to_session_id = {}
219
+ Tengine::Core::Driver.where(:version => dsl_version).each do |d|
220
+ @driver_name_to_session_id[d.name] = d.session_id
221
+ end
222
+ end
223
+
224
+ def __forget_session_ids_for_drivers__
225
+ @driver_name_to_session_id = nil
226
+ end
227
+
228
+ def __find_session_id_for_driver_name__(driver_name)
229
+ return nil unless @driver_name_to_session_id
230
+ @driver_name_to_session_id[driver_name]
231
+ end
232
+ end
233
+
234
+
213
235
  end
@@ -31,11 +31,11 @@ class Tengine::Core::Driver
31
31
  # @attribute 対象クラス名
32
32
  field :target_class_name, :type => String
33
33
 
34
- index([ [:_id, Mongo::ASCENDING], [:enabled, Mongo::ASCENDING], [:version, Mongo::ASCENDING], ])
35
- index([ [:name, Mongo::ASCENDING], [:version, Mongo::ASCENDING], ], :unique => true)
36
- index([ [:version, Mongo::ASCENDING], [:enabled_on_activation, Mongo::ASCENDING], ])
37
- index([ [:version, Mongo::ASCENDING], ])
38
- index([ [:_id, Mongo::ASCENDING], [:name, Mongo::ASCENDING], ])
34
+ index _id: 1, enabled: 1, version: 1
35
+ index({name: 1, version: 1}, {unique: true})
36
+ index version: 1, enabled_on_activation: 1
37
+ index version: 1
38
+ index _id: 1, name: 1
39
39
 
40
40
  validates(:name, :presence => true,
41
41
  :uniqueness => {:scope => :version, :message => "is already taken in same version"},
@@ -48,22 +48,35 @@ class Tengine::Core::Driver
48
48
  belongs_to :session, :index => true, :class_name => "Tengine::Core::Session"
49
49
  has_many :handler_paths, :class_name => "Tengine::Core::HandlerPath"
50
50
 
51
- after_create :update_handler_path
52
51
  before_create :create_session # has_oneによって追加されるメソッドcreate_sessionのように振る舞うメソッドです
52
+ after_create :update_handler_path
53
+
54
+ after_destroy :delete_handler_paths
55
+
56
+ def create_session
57
+ self.session ||= Tengine::Core::Session.create
58
+ end
53
59
 
54
60
  def update_handler_path
55
61
  handlers.each(&:update_handler_path)
56
62
  end
57
63
 
58
- def create_session
59
- self.session ||= Tengine::Core::Session.create
64
+ def delete_handler_paths
65
+ return if new_record?
66
+ Tengine::Core::HandlerPath.where(:driver_id => self.id).delete_all
60
67
  end
61
68
 
62
69
  class << self
63
70
  # Tengine::Core::FindByName で定義しているクラスメソッドfind_by_nameを上書きしています
64
71
  def find_by_name(name, options = {})
65
72
  version = options[:version] || Tengine::Core::Setting.dsl_version
66
- first(:conditions => {:name => name, :version => version})
73
+ where({:name => name, :version => version}).first
74
+ end
75
+
76
+ def delete_all_with_handler_paths(dsl_version)
77
+ drivers = Tengine::Core::Driver.where(:version => dsl_version)
78
+ Tengine::Core::HandlerPath.where(:driver_id.in => drivers.map(&:id)).delete_all
79
+ drivers.delete_all
67
80
  end
68
81
  end
69
82
  end