Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
0334f9f6
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0334f9f6
编写于
1月 17, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add ActionDispatch::Notifications middleware.
上级
afd0c06d
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
122 addition
and
43 deletion
+122
-43
actionpack/lib/action_dispatch.rb
actionpack/lib/action_dispatch.rb
+1
-0
actionpack/lib/action_dispatch/middleware/callbacks.rb
actionpack/lib/action_dispatch/middleware/callbacks.rb
+0
-2
actionpack/lib/action_dispatch/middleware/notifications.rb
actionpack/lib/action_dispatch/middleware/notifications.rb
+24
-0
actionpack/lib/action_dispatch/middleware/show_exceptions.rb
actionpack/lib/action_dispatch/middleware/show_exceptions.rb
+2
-5
actionpack/test/dispatch/callbacks_test.rb
actionpack/test/dispatch/callbacks_test.rb
+0
-12
actionpack/test/dispatch/notifications_test.rb
actionpack/test/dispatch/notifications_test.rb
+69
-0
actionpack/test/dispatch/show_exceptions_test.rb
actionpack/test/dispatch/show_exceptions_test.rb
+0
-23
activesupport/lib/active_support/notifications.rb
activesupport/lib/active_support/notifications.rb
+1
-1
activesupport/lib/active_support/notifications/instrumenter.rb
...esupport/lib/active_support/notifications/instrumenter.rb
+9
-0
activesupport/test/notifications_test.rb
activesupport/test/notifications_test.rb
+16
-0
未找到文件。
actionpack/lib/action_dispatch.rb
浏览文件 @
0334f9f6
...
...
@@ -46,6 +46,7 @@ module ActionDispatch
autoload
:Cookies
autoload
:Flash
autoload
:Head
autoload
:Notifications
autoload
:ParamsParser
autoload
:Rescue
autoload
:ShowExceptions
...
...
actionpack/lib/action_dispatch/middleware/callbacks.rb
浏览文件 @
0334f9f6
...
...
@@ -45,8 +45,6 @@ def call(env)
run_callbacks
(
:prepare
)
if
@prepare_each_request
@app
.
call
(
env
)
end
ensure
ActiveSupport
::
Notifications
.
instrument
"action_dispatch.callback"
end
end
end
actionpack/lib/action_dispatch/middleware/notifications.rb
0 → 100644
浏览文件 @
0334f9f6
module
ActionDispatch
# Provide notifications in the middleware stack. Notice that for the before_dispatch
# and after_dispatch notifications, we just send the original env, so we don't pile
# up large env hashes in the queue. However, in exception cases, the whole env hash
# is actually useful, so we send it all.
class
Notifications
def
initialize
(
app
)
@app
=
app
end
def
call
(
stack_env
)
env
=
stack_env
.
dup
ActiveSupport
::
Notifications
.
instrument
(
"action_dispatch.before_dispatch"
,
:env
=>
env
)
ActiveSupport
::
Notifications
.
instrument!
(
"action_dispatch.after_dispatch"
,
:env
=>
env
)
do
@app
.
call
(
stack_env
)
end
rescue
Exception
=>
exception
ActiveSupport
::
Notifications
.
instrument
(
'action_dispatch.exception'
,
:env
=>
stack_env
,
:exception
=>
exception
)
raise
exception
end
end
end
\ No newline at end of file
actionpack/lib/action_dispatch/middleware/show_exceptions.rb
浏览文件 @
0334f9f6
...
...
@@ -61,11 +61,8 @@ def initialize(app, consider_all_requests_local = false)
def
call
(
env
)
@app
.
call
(
env
)
rescue
Exception
=>
exception
ActiveSupport
::
Notifications
.
instrument
'action_dispatch.show_exception'
,
:env
=>
env
,
:exception
=>
exception
do
raise
exception
if
env
[
'action_dispatch.show_exceptions'
]
==
false
render_exception
(
env
,
exception
)
end
raise
exception
if
env
[
'action_dispatch.show_exceptions'
]
==
false
render_exception
(
env
,
exception
)
end
private
...
...
actionpack/test/dispatch/callbacks_test.rb
浏览文件 @
0334f9f6
...
...
@@ -85,18 +85,6 @@ def test_before_and_after_callbacks
assert_equal
4
,
Foo
.
b
end
def
test_should_send_an_instrumentation_callback_for_async_processing
ActiveSupport
::
Notifications
.
expects
(
:instrument
).
with
(
"action_dispatch.callback"
)
dispatch
end
def
test_should_send_an_instrumentation_callback_for_async_processing_even_on_failure
ActiveSupport
::
Notifications
.
notifier
.
expects
(
:publish
)
assert_raise
RuntimeError
do
dispatch
{
|
env
|
raise
"OMG"
}
end
end
private
def
dispatch
(
cache_classes
=
true
,
&
block
)
...
...
actionpack/test/dispatch/notifications_test.rb
0 → 100644
浏览文件 @
0334f9f6
require
'abstract_unit'
class
NotificationsMiddlewareTest
<
ActionController
::
IntegrationTest
Boomer
=
lambda
do
|
env
|
req
=
ActionDispatch
::
Request
.
new
(
env
)
case
req
.
path
when
"/"
[
200
,
{},
[]]
else
raise
"puke!"
end
end
App
=
ActionDispatch
::
Notifications
.
new
(
Boomer
)
def
setup
@queue
=
ActiveSupport
::
Notifications
::
Fanout
.
new
@notifier
=
ActiveSupport
::
Notifications
::
Notifier
.
new
(
@queue
)
ActiveSupport
::
Notifications
.
notifier
=
@notifier
@events
=
[]
ActiveSupport
::
Notifications
.
subscribe
do
|*
args
|
@events
<<
args
end
@app
=
App
end
test
"publishes notifications"
do
get
"/"
ActiveSupport
::
Notifications
.
notifier
.
wait
assert_equal
2
,
@events
.
size
before
,
after
=
@events
assert_equal
'action_dispatch.before_dispatch'
,
before
[
0
]
assert_kind_of
Hash
,
before
[
4
][
:env
]
assert_equal
'GET'
,
before
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_equal
'action_dispatch.after_dispatch'
,
after
[
0
]
assert_kind_of
Hash
,
after
[
4
][
:env
]
assert_equal
'GET'
,
after
[
4
][
:env
][
"REQUEST_METHOD"
]
end
test
"publishes notifications on failure"
do
begin
get
"/puke"
rescue
end
ActiveSupport
::
Notifications
.
notifier
.
wait
assert_equal
3
,
@events
.
size
before
,
after
,
exception
=
@events
assert_equal
'action_dispatch.before_dispatch'
,
before
[
0
]
assert_kind_of
Hash
,
before
[
4
][
:env
]
assert_equal
'GET'
,
before
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_equal
'action_dispatch.after_dispatch'
,
after
[
0
]
assert_kind_of
Hash
,
after
[
4
][
:env
]
assert_equal
'GET'
,
after
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_equal
'action_dispatch.exception'
,
exception
[
0
]
assert_kind_of
Hash
,
exception
[
4
][
:env
]
assert_equal
'GET'
,
exception
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_kind_of
RuntimeError
,
exception
[
4
][
:exception
]
end
end
\ No newline at end of file
actionpack/test/dispatch/show_exceptions_test.rb
浏览文件 @
0334f9f6
...
...
@@ -104,27 +104,4 @@ class ShowExceptionsTest < ActionController::IntegrationTest
assert_response
405
assert_match
/ActionController::MethodNotAllowed/
,
body
end
test
"publishes notifications"
do
# Wait pending notifications to be published
ActiveSupport
::
Notifications
.
notifier
.
wait
@app
,
event
=
ProductionApp
,
nil
self
.
remote_addr
=
'127.0.0.1'
ActiveSupport
::
Notifications
.
subscribe
(
'action_dispatch.show_exception'
)
do
|*
args
|
event
=
args
end
get
"/"
assert_response
500
assert_match
/puke/
,
body
ActiveSupport
::
Notifications
.
notifier
.
wait
assert_equal
'action_dispatch.show_exception'
,
event
.
first
assert_kind_of
Hash
,
event
.
last
[
:env
]
assert_equal
'GET'
,
event
.
last
[
:env
][
"REQUEST_METHOD"
]
assert_kind_of
RuntimeError
,
event
.
last
[
:exception
]
end
end
activesupport/lib/active_support/notifications.rb
浏览文件 @
0334f9f6
...
...
@@ -45,7 +45,7 @@ module Notifications
class
<<
self
attr_writer
:notifier
delegate
:publish
,
:subscribe
,
:to
=>
:notifier
delegate
:instrument
,
:to
=>
:instrumenter
delegate
:instrument
,
:
instrument!
,
:
to
=>
:instrumenter
def
notifier
@notifier
||=
Notifier
.
new
...
...
activesupport/lib/active_support/notifications/instrumenter.rb
浏览文件 @
0334f9f6
...
...
@@ -20,6 +20,15 @@ def instrument(name, payload={})
result
end
# The same as instrument, but sends the notification even if the yielded
# block raises an error.
def
instrument!
(
name
,
payload
=
{})
time
=
Time
.
now
yield
(
payload
)
if
block_given?
ensure
@notifier
.
publish
(
name
,
time
,
Time
.
now
,
@id
,
payload
)
end
private
def
unique_id
SecureRandom
.
hex
(
10
)
...
...
activesupport/test/notifications_test.rb
浏览文件 @
0334f9f6
...
...
@@ -90,6 +90,22 @@ def test_instrument_returns_block_result
drain
end
def
test_instrument_with_bang_returns_result_even_on_failure
begin
instrument!
(
:awesome
,
:payload
=>
"notifications"
)
do
raise
"OMG"
end
flunk
rescue
end
drain
assert_equal
1
,
@events
.
size
assert_equal
:awesome
,
@events
.
last
.
name
assert_equal
Hash
[
:payload
=>
"notifications"
],
@events
.
last
.
payload
end
def
test_instrument_yields_the_paylod_for_further_modification
assert_equal
2
,
instrument
(
:awesome
)
{
|
p
|
p
[
:result
]
=
1
+
1
}
drain
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录