Skip to content

Commit d699894

Browse files
committed
around_action
1 parent 3691672 commit d699894

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

around_action.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class BenchmarkFilter
2+
def self.filter(controller)
3+
timer = Time.now
4+
Rails.logger.debug "---#{controller.controller_name} #{controller.action_name}"
5+
yield # 這裡執行Action動作
6+
elapsed_time = Time.now - timer
7+
Rails.logger.debug "---#{controller.controller_name} #{controller.action_name} finished in %0.2f" % elapsed_time
8+
end
9+
end
10+
11+
class EventsControler < ApplicationController
12+
around_action BenchmarkFilter
13+
end
14+
15+
# around_action 可以加一个类,也可以加具体的方法
16+
17+
def save_notice
18+
log_id = SecureRandom.uuid
19+
20+
request_options = {}
21+
request_options[:log_id] = log_id
22+
request_options[:request_at] = Time.now
23+
request_options[:params] = params.except(:action, :controller)
24+
YtxappInboxNoticeWorker.perform_async :ivr_request, request_options
25+
26+
consuming = Benchmark.measure{ yield }.real
27+
28+
response_options = {}
29+
response_options[:log_id] = log_id
30+
response_options[:consuming] = consuming
31+
response_options[:response_at] = Time.now
32+
response_options[:response_body] = response.body
33+
YtxappInboxNoticeWorker.perform_in 20.seconds, :ivr_response, response_options
34+
end
35+
36+
around_action :save_notice
37+
38+
# save_notice 方法中需要有yield,action就是回调执行在yield中

0 commit comments

Comments
 (0)