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
@@ -67,25 +67,12 @@ module Tengine::Core::DslEvaluator
67
67
  def __setup_core_ext__
68
68
  Symbol.class_eval do
69
69
  def and(other)
70
- Tengine::Core::DslFilterDef.new(
71
- [self.to_s, other.to_s],
72
- {
73
- 'method' => :and,
74
- 'children' => [
75
- { 'pattern' => self, 'method' => :find_or_mark_in_session },
76
- { 'pattern' => other, 'method' => :find_or_mark_in_session },
77
- ]
78
- })
70
+ Tengine::Core::DslFilterDef.new_and(self, other)
79
71
  end
80
72
  alias_method :&, :and
81
73
 
82
74
  def at(soruce_pattern)
83
- Tengine::Core::DslFilterDef.new(
84
- [self.to_s],
85
- {
86
- 'method' => :match_source_name?,
87
- 'pattern' => soruce_pattern.to_s,
88
- })
75
+ Tengine::Core::DslFilterDef.new_at(self, soruce_pattern)
89
76
  end
90
77
  end
91
78
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require 'tengine/core'
2
3
 
3
4
  class Tengine::Core::DslFilterDef
@@ -8,4 +9,43 @@ class Tengine::Core::DslFilterDef
8
9
  @filter = filter
9
10
  end
10
11
 
12
+ BRACE_FORMAT = 's_%s_e'.freeze # メソッド名にはカッコが使えないので変わりに表現する方法として(と)をそれぞれs_と_eで表します。
13
+
14
+ FORMATTERS = {
15
+ :and => lambda{|event_type_names, filter| "%s_and_%s" % event_type_names },
16
+ :at => lambda{|event_type_names, filter| "%s_at_%s" % [event_type_names.first, filter['pattern'].to_s] },
17
+ }
18
+
19
+ def to_method_name
20
+ event_type_name_args =
21
+ event_type_names.map{|obj| obj.is_a?(Tengine::Core::DslFilterDef) ? obj.to_method_name : obj.to_s}
22
+ formatter = FORMATTERS[ filter['type'] ]
23
+ BRACE_FORMAT % formatter.call(event_type_name_args, filter)
24
+ end
25
+
26
+ class << self
27
+ def new_and(left, right)
28
+ Tengine::Core::DslFilterDef.new(
29
+ [left, right],
30
+ {
31
+ 'type' => :and,
32
+ 'method' => :and,
33
+ 'children' => [
34
+ { 'pattern' => left, 'method' => :find_or_mark_in_session },
35
+ { 'pattern' => right, 'method' => :find_or_mark_in_session },
36
+ ]
37
+ })
38
+ end
39
+
40
+ def new_at(event_type_name, source_pattern)
41
+ Tengine::Core::DslFilterDef.new(
42
+ [event_type_name],
43
+ {
44
+ 'type' => :at,
45
+ 'method' => :match_source_name?,
46
+ 'pattern' => source_pattern.to_s,
47
+ })
48
+ end
49
+
50
+ end
11
51
  end
@@ -58,7 +58,7 @@ module Tengine::Core::DslLoader
58
58
  return
59
59
  end
60
60
 
61
- if dsl_version_document = Tengine::Core::Setting.first(:conditions => {:name => "dsl_version"})
61
+ if dsl_version_document = Tengine::Core::Setting.where({:name => "dsl_version"}).first
62
62
  dsl_version_document.value = config.dsl_version
63
63
  dsl_version_document.save!
64
64
  else
@@ -32,17 +32,16 @@ class Tengine::Core::Event
32
32
 
33
33
  # 複数の経路から同じ意味のイベントが複数個送られる場合に
34
34
  # これらを重複して登録しないようにユニーク制約を設定
35
- index :key, unique: true
36
- # :unique => trueのindexを設定しているので、uniquenessのバリデーションは設定しません
37
- validates :key, :presence => true #, :uniqueness => true
38
-
39
- index([ [:event_type_name, Mongo::ASCENDING], [:confirmed, Mongo::ASCENDING], ])
40
- index([ [:event_type_name, Mongo::ASCENDING], [:level, Mongo::ASCENDING], [:occurred_at, Mongo::DESCENDING], ])
41
- index([ [:event_type_name, Mongo::ASCENDING], [:occurred_at, Mongo::ASCENDING], ])
42
- index([ [:event_type_name, Mongo::ASCENDING], [:source_name, Mongo::ASCENDING], ])
43
- index([ [:level, Mongo::ASCENDING], [:sender_name, Mongo::ASCENDING], [:occurred_at, Mongo::DESCENDING], ])
44
- index([ [:level, Mongo::ASCENDING], [:occurred_at, Mongo::DESCENDING], ])
45
- index([ [:source_name, Mongo::ASCENDING], [:level, Mongo::ASCENDING], [:occurred_at, Mongo::DESCENDING], ])
35
+ index({ key: 1}, {unique: true})
36
+ validates :key, :presence => true, :uniqueness => true
37
+
38
+ index event_type_name: 1, confirmed:1
39
+ index event_type_name: 1, level:1, occurred_at: -1
40
+ index event_type_name: 1, occurred_at: 1
41
+ index event_type_name: 1, source_name:1
42
+ index level: 1, sender_name: 1, occurred_at: -1
43
+ index level: 1, occurred_at: -1
44
+ index source_name: 1, level: 1, occurred_at: -1
46
45
 
47
46
  # selectable_attrを使ってます
48
47
  # see http://github.com/akm/selectable_attr
@@ -86,11 +85,7 @@ class Tengine::Core::Event
86
85
  retries = -1
87
86
  results = nil
88
87
 
89
- case collection.driver.db.connection when Mongo::ReplSetConnection then
90
- safemode = { :w => "majority", :wtimeout => wtimeout, } # mongodb 2.0+, 参加しているレプリカセットの多数派に書き込んだ時点でOK扱い
91
- else
92
- safemode = true
93
- end
88
+ safemode = Tengine::Core::SafeUpdatable.safemode(collection, wtimeout)
94
89
 
95
90
  while true do
96
91
  return false if retries >= retry_max # retryしすぎ
@@ -111,29 +106,47 @@ class Tengine::Core::Event
111
106
  hash['created_at'] ||= Time.at(Time.now.to_i)
112
107
  hash['updated_at'] = Time.at(Time.now.to_i)
113
108
 
109
+ results = nil
114
110
  begin
115
- results = collection.driver.update(
116
- { :key => the_event.key, :lock_version => the_event.lock_version },
117
- { "$set" => hash },
118
- { :upsert => true, :safe => safemode, :multiple => false }
119
- )
120
- rescue Mongo::OperationFailure => e
111
+ # Can't, no results returned...
112
+ # results = with(safe: safemode).where(
113
+ # key: the_event.key, lock_version: the_event.lock_version
114
+ # ).update(
115
+ # "$set" => hash,
116
+ # flags: [ :upsert ]
117
+ # )
118
+ mongo_session.with(safe: safemode) do |ss|
119
+ col = ss[collection.name]
120
+ results = ss.context.update(
121
+ col.database.name,
122
+ col.name,
123
+ { key: the_event.key, lock_version: the_event.lock_version },
124
+ { "$set" => hash },
125
+ flags: [ :upsert ]
126
+ )
127
+ end
128
+ rescue Moped::Errors::OperationFailure => e
121
129
  # upsert = trueだがindexのunique制約があるので重複したkeyは
122
130
  # 作成不可、lock_versionの更新失敗はこちらに来る。これは意
123
131
  # 図した動作なのでraiseしない。
124
132
  Tengine.logger.debug "retrying due to mongodb error #{e}"
125
133
  # lock_versionが存在しない可能性(そのような古いDBを引きずっている等)
126
- collection.driver.update(
127
- { :key => the_event.key, :lock_version => { "$exists" => false } },
128
- { "$set" => { :lock_version => -(2**63) } },
129
- { :upsert => false, :safe => $safemode, :multiple => false }
130
- )
134
+ mongo_session.with(safe: safemode) do |ss|
135
+ col = ss[collection.name]
136
+ results = ss.context.update(
137
+ col.database.name,
138
+ col.name,
139
+ { "$query" => { key: the_event.key, lock_version: { "$exists" => false} } },
140
+ { "$set" => { lock_version: -(2**63) } },
141
+ )
142
+ end
143
+ # again
131
144
  else
132
145
  if results["error"]
133
- raise Mongo::OperationFailure, results["error"]
146
+ raise Moped::Errors::OperationFailure, results["error"]
134
147
  elsif results["upserted"]
135
148
  # *hack* _idを消してupsertしたので、このとき_idは新しくなっている
136
- the_event.write_attributes "_id" => results["upserted"]
149
+ the_event._id = results["upserted"]
137
150
  the_event.reload
138
151
  return the_event
139
152
  else
@@ -142,5 +155,7 @@ class Tengine::Core::Event
142
155
  end
143
156
  end
144
157
  end
158
+ rescue Exception
159
+ p $!
145
160
  end
146
161
  end
@@ -15,7 +15,7 @@ class Tengine::Core::Event::Finder
15
15
  :confirmed,
16
16
  :sender_name,
17
17
  :properties,
18
- :reflesh_interval, # 更新間隔
18
+ :refresh_interval, # 更新間隔
19
19
  ].freeze
20
20
 
21
21
  ATTRIBUTE_NAMES.each{|name| attr_accessor(name)}
@@ -71,18 +71,15 @@ module Tengine::Core::EventExceptionReportable
71
71
 
72
72
  end
73
73
 
74
- module InstanceMethods
75
- def report_on_exception(dsl_context, event)
76
- begin
77
- yield
78
- rescue Exception => e
79
- Tengine.logger.error("[#{e.class.name}] #{e.message}\n " << e.backtrace.join("\n "))
80
- if reporter = Tengine::Core::Kernel.event_exception_reporter
81
- reporter.call(self, dsl_context, event, e)
82
- end
74
+ def report_on_exception(dsl_context, event)
75
+ begin
76
+ yield
77
+ rescue Exception => e
78
+ Tengine.logger.error("[#{e.class.name}] #{e.message}\n " << e.backtrace.join("\n "))
79
+ if reporter = Tengine::Core::Kernel.event_exception_reporter
80
+ reporter.call(self, dsl_context, event, e)
83
81
  end
84
82
  end
85
-
86
83
  end
87
84
 
88
85
  end
@@ -8,7 +8,7 @@ class Tengine::Core::EventWrapper
8
8
  end
9
9
 
10
10
  [:event_type_name, :key, :source_name, :occurred_at,
11
- :level, :confirmed, :sender_name, :properties,].each do |attr_name|
11
+ :level, :level_key, :confirmed, :sender_name, :properties,].each do |attr_name|
12
12
  class_eval(<<-EOS)
13
13
  def #{attr_name}; @source.#{attr_name}; end
14
14
  EOS
@@ -19,7 +19,7 @@ module Tengine::Core::FindByName
19
19
 
20
20
  module ClassMethods
21
21
  def find_by_name(name)
22
- first(:conditions => {:name => name})
22
+ where({:name => name}).first
23
23
  end
24
24
 
25
25
  def find_by_name!(name, *args, &block)
@@ -103,9 +103,19 @@ class Tengine::Core::Handler
103
103
  @filter = filter
104
104
  @event = event
105
105
  @session = Tengine::Core::SessionWrapper.new(session)
106
+ @session_reloaded = false
106
107
  @current = @filter
107
108
  end
108
109
 
110
+ def session
111
+ unless @session_reloaded
112
+ @session.reload
113
+ @session_reloaded = true
114
+ end
115
+ @session
116
+ end
117
+
118
+
109
119
  def visit
110
120
  Tengine.logger.debug("visiting #{@current.inspect}")
111
121
  send(@current['method'])
@@ -131,13 +141,13 @@ class Tengine::Core::Handler
131
141
  name = @current['pattern'].to_s
132
142
  key = "mark_#{name}"
133
143
  if name == @event.event_type_name
134
- unless @session.system_properties[key]
135
- @session.system_update(key => true)
136
- Tengine.logger.debug("session.system_updated #{@session.system_properties.inspect}")
144
+ if session.system_properties[key].nil?
145
+ session.system_update(key => true)
146
+ Tengine.logger.debug("session.system_updated #{session.system_properties.inspect}")
137
147
  end
138
148
  return true
139
149
  else
140
- return @session.system_properties[key]
150
+ return session.system_properties[key]
141
151
  end
142
152
  end
143
153
 
@@ -11,7 +11,7 @@ class Tengine::Core::HandlerPath
11
11
 
12
12
  scope(:event_type_name, lambda{|v| where(:event_type_name => v)})
13
13
 
14
- index([ [:event_type_name, Mongo::ASCENDING], [:_id, Mongo::ASCENDING], ])
14
+ index event_type_name: 1, _id: 1
15
15
 
16
16
  class << self
17
17
  def find_handlers(event_type_name)
@@ -116,8 +116,9 @@ class Tengine::Core::HeartbeatWatcher
116
116
  def start
117
117
  @config.setup_loggers
118
118
 
119
- Mongoid.config.from_hash @config[:db]
120
- Mongoid.config.option :persist_in_safe_mode, :default => true
119
+ Mongoid.configure do |c|
120
+ c.send :load_configuration, @config[:db]
121
+ end
121
122
 
122
123
  require 'amqp'
123
124
  Mongoid.logger = AMQP::Session.logger = Tengine.logger
@@ -34,6 +34,7 @@ class Tengine::Core::Kernel
34
34
  update_status(:waiting_activation)
35
35
  wait_for_activation(&block)
36
36
  else
37
+ require 'profile' if ENV['tengined_profile']
37
38
  activate(&block)
38
39
  end
39
40
  end
@@ -78,10 +79,10 @@ class Tengine::Core::Kernel
78
79
  alias_method :context, :dsl_context
79
80
 
80
81
  def evaluate
82
+ Tengine::Core::Driver.delete_all_with_handler_paths(dsl_context.config.dsl_version)
81
83
  dsl_context.__evaluate__
82
84
  end
83
85
 
84
-
85
86
  def bind
86
87
  # dsl_context.__evaluate__
87
88
  # Tengine::Core::stdout_logger.debug("Hanlder bindings:\n" << dsl_context.to_a.inspect)
@@ -132,6 +133,8 @@ class Tengine::Core::Kernel
132
133
  end
133
134
  mq.subscribe(:ack => true, :nowait => false, :confirm => confirm) do |headers, msg|
134
135
  process_message(headers, msg)
136
+ # headers.ack
137
+ # Tengine.logger.debug("headers: #{headers.inspect}\n#{msg}")
135
138
  end
136
139
  end
137
140
 
@@ -170,7 +173,7 @@ class Tengine::Core::Kernel
170
173
  save_event(raw_event)
171
174
  end
172
175
 
173
- rescue Mongo::OperationFailure, Mongoid::Errors::Validations => e
176
+ rescue Moped::Errors::OperationFailure, Mongoid::Errors::Validations => e
174
177
  Tengine.logger.warn("failed to store an event.\n[#{e.class.name}] #{e.message}")
175
178
  event = nil
176
179
  rescue Exception => e
@@ -184,7 +187,7 @@ class Tengine::Core::Kernel
184
187
 
185
188
  begin
186
189
  n = Tengine::Core::Event.where(:key => raw_event.key, :sender_name => raw_event.sender_name).count
187
- rescue Mongo::ConnectionFailure, Mongo::OperationTimeout, Mongo::OperationFailure => e
190
+ rescue Moped::Errors::ConnectionFailure, Moped::Errors::OperationFailure => e
188
191
  Tengine.logger.error("giving up processing an event due to #{e} (#{e.class.name})")
189
192
  n = 0
190
193
  end
@@ -204,6 +207,19 @@ class Tengine::Core::Kernel
204
207
  ack_policy = ack_policy_for(event)
205
208
  safety_processing_headers(headers, event, ack_policy) do
206
209
  ack if ack_policy == :at_first
210
+
211
+ # ドライバを再ロードするならハンドラを検索する前に行います。
212
+ unless config.tengined.cache_drivers
213
+ Tengine::Core::Driveable.__remember_session_ids_for_drivers__(config.dsl_version)
214
+ begin
215
+ Tengine::Core::Driver.delete_all_with_handler_paths(dsl_context.config.dsl_version)
216
+ ActiveSupport::Dependencies.clear
217
+ evaluate
218
+ ensure
219
+ Tengine::Core::Driveable.__forget_session_ids_for_drivers__
220
+ end
221
+ end
222
+
207
223
  handlers = find_handlers(event)
208
224
  safty_handlers(handlers) do
209
225
  delegate(event, handlers)
@@ -213,7 +229,7 @@ class Tengine::Core::Kernel
213
229
  end
214
230
  close_if_shutting_down
215
231
  true
216
- rescue Mongo::ConnectionFailure, Mongo::OperationTimeout, Mongo::OperationFailure => e
232
+ rescue Moped::Errors::ConnectionFailure, Moped::Errors::OperationFailure => e
217
233
  Tengine.logger.error("giving up processing an event due to #{e} (#{e.class.name})")
218
234
  Tengine.logger.info("requeue an event #{raw_event.inspect}")
219
235
  headers.reject(:requeue => true)
@@ -325,7 +341,7 @@ class Tengine::Core::Kernel
325
341
  raw_event.attributes.update(:confirmed => (raw_event.level.to_i <= config.confirmation_threshold)))
326
342
  Tengine.logger.debug("saved an event #{event.inspect}")
327
343
  event
328
- rescue Mongo::OperationFailure => e
344
+ rescue Moped::Errors::OperationFailure => e
329
345
  Tengine.logger.error("failed to save an event #{raw_event.inspect}\n[#{e.class.name}] #{e.message}")
330
346
  # FIXME!!
331
347
  # このままではログに埋もれてしまうのでなんとかすべき。
@@ -402,6 +418,7 @@ class Tengine::Core::Kernel
402
418
  # * もうngが保存されているとき
403
419
  if event.new_record? or event.event_type_name != raw_event.event_type_name
404
420
  event.write_attributes raw_event.attributes.update(:confirmed => (raw_event.level.to_i <= config.confirmation_threshold))
421
+ event.occurred_at = Time.now
405
422
  else
406
423
  nil
407
424
  end
@@ -464,7 +481,6 @@ class Tengine::Core::Kernel
464
481
  end
465
482
  end
466
483
  after_delegate.call if after_delegate.respond_to?(:call)
467
- ActiveSupport::Dependencies.clear unless config.tengined.cache_drivers
468
484
  end
469
485
 
470
486
  def close_if_shutting_down
@@ -3,9 +3,10 @@
3
3
  require 'tengine_core'
4
4
  require 'mongoid'
5
5
  require 'mongoid/version'
6
- require 'mongoid/cursor'
7
6
 
8
7
  if Mongoid::VERSION < "3.0.0"
8
+ require 'mongoid/cursor'
9
+
9
10
  class Mongoid::Cursor
10
11
  # https://github.com/mongoid/mongoid/pull/1609
11
12
  def each
@@ -1,5 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'tengine/core'
3
+ require 'moped' # part of mongoid 3
3
4
 
4
5
  require_relative 'safe_updatable'
5
6
 
@@ -54,13 +55,26 @@ class Tengine::Core::Mutex::Mutex
54
55
  field :ttl, :type => Float
55
56
  field :waiters, :type => Array
56
57
 
57
- def self.find_or_create name, ttl
58
- collection.driver.update(
59
- { :_id => name },
60
- { "$set" => { :ttl => ttl, }, },
61
- { :upsert => true, :safe => true, :multiple => false, }
62
- )
63
- return find(name)
58
+ class << self
59
+ def static_bson_objectids
60
+ @static_bson_objectids ||= {}
61
+ end
62
+
63
+ def clear_static_bson_objectids
64
+ @static_bson_objectids = nil
65
+ end
66
+
67
+ def add_static_bson_objectid(name, objectid)
68
+ static_bson_objectids[name] = objectid.is_a?(Moped::BSON::ObjectId) ? objectid :
69
+ Moped::BSON::ObjectId.from_string(objectid.to_s)
70
+ end
71
+
72
+ def find_or_create name, ttl
73
+ id = Moped::BSON::ObjectId.legal?(name) ? name : static_bson_objectids[name] or
74
+ raise ArgumentError, "Unknown name for BSON::ObjectId: #{name.inspect}"
75
+ collection.find(_id: id).upsert(_id: id, ttl: ttl)
76
+ return find(id)
77
+ end
64
78
  end
65
79
 
66
80
  private
@@ -117,7 +131,7 @@ class Tengine::Core::Mutex
117
131
  raise TypeError, "finite numeric expected (got #{t})" unless t.finite?
118
132
  raise ArgumentError, "TTL doesn't make sense." unless t > 0
119
133
 
120
- return oldnew(Tengine::Core::Mutex::Mutex.find_or_create(name, t), BSON::ObjectId.new, 0)
134
+ return oldnew(Tengine::Core::Mutex::Mutex.find_or_create(name, t), Moped::BSON::ObjectId.new, 0)
121
135
  end
122
136
  end
123
137