We are using RabbitMQ as our message queue system, ruby client is workling. This week we encountered a strange issue, we create a notification, and define an after_create callback to ask workling to find that notification and then push the notification to twitter or facebook, it works fine except that sometimes it will raise an error said "can't find the notification with the specified ID"
class Notification < ActiveRecord::Base
after_create :asyns_send_notification
......
def async_send_notification
NotificationWorker.async_send_notification({:notification_id => id})
end
end
class NotificationWorker < Workling::Base
def send_notification(params)
notification = Notification.find(params[:notification_id])
......
end
end
It's wierd the notification_id is passed to NotificationWorker, that means the notification is already created, the notification is supposed to be existed.
After talking with MySQL DBA, we find the problem is the find sql is executed before insert transaction is committed.
Let me describe it step by step.
- Notification sends "Transaction Begin" command
- Notification sends "INSERT" command
- Notification gets "next sequence value" as new object id
- Notification sends "new object id" to NotificationWorker
- NotificationWorker sends "SELECT" command to find notification object
- Notification sends "Transaction Commit" command
As you seen, at step 5, the new notification is not existed in the mysql database yet, so the error "Not found" will be raised.
To solve this issue, we can use after_commit callback.
In rails 2.x, we should install after_commit gem, in rails 3.x, after_commit callback is supported by default.
class Notification < ActiveRecord::Base
after_commit_on_create :asyns_send_notification
......
def async_send_notification
NotificationWorker.async_send_notification({:notification_id => id})
end
end
So Notification asks NotificationWorker to run only after the transaction is committed.
相关推荐
使用RSpec 测试Rails 程序.pdf
rspec 下载请注意:英文文档
akamai-rspec 使用为您的配置编写测试。如何使用设置将'akamai-rspec'添加到您的Gemfile中。 在您的spec-helper.rb ,配置Akamai域: AkamaiRSpec::Request.prod_domain = "www.example.com.edgesuite.net"或者 ...
rspec-dns, 使用RSpec轻松测试你的DNS rspec-dns rspec是一个rspec插件,用于方便的DNS测试。 它使用dnsruby而不是标准库来解析名称解析。安装如果你正在使用 bundler,请将此行添加到你的应用程序的Gemfile 中:gem
使用RSpec测试Rails程序。本书基于Rails 4.1,使用完整应用 来演示TDD的流程。
rspec-api-blueprint-formatter, 从RSpec测试自动生成API文档 ! RSpec APIBlueprint格式化程序从RSpec测试自动生成API文档 !像这样it 'retrievs the patients medications' do retrieve_medications
在他们下载的chm始终打不开或有问题。 ... 使用Cucumber+Rspec玩转BDD(1)——用户注册 使用Cucumber+Rspec玩转BDD(2)——邮件激活 ...使用Cucumber+Rspec玩转BDD(3)——用户...使用Cucumber+Rspec玩转BDD(7)——测试重构
RSpec驱动的API测试框架 寻找项目维护者 我正在寻找项目维护人员,以帮助使机载设备保持最新状态且无错误,同时避免功能升级和保持向后兼容性。 如果您想提供帮助,请评论。 安装 安装机载: $ gem install ...
原版The RSpec Book并附有源码
rspec-puppet, 针对 Puppet 清单的RSpec测试 你的Puppet的RSpec测试&模块 安装gem install rspec-puppet注意 ruby 1.8用户: 尽管 rspec-puppet本身支持 ruby 1.8,但你需要将rspec
rspec出入门者学习理解,介绍的非常的详细、清楚; 欢迎下载学习~
Reddit 克隆这是我关于使用 RSpec 和 Capybara 测试 reddit 克隆的截屏视频的存储库。 您可以在找到有关如何制作的视频住在
您创建网页的一部分,然后浏览到该页面以对其进行测试,以确保它按照您的预期运行。 然后添加另一个功能。 并测试这两个功能。 然后你添加第三个功能并测试...只是第三个功能。 想象一下,如果您有一组针对您的代码...
RSpec的性能测试匹配器可设置对速度,资源使用和可伸缩性的期望。 RSpec :: Benchmark由: 用于测量执行时间和每秒迭代次数的。 用于估计计算复杂度的。 用于测量对象和内存分配。 为什么? 集成和单元测试可...
带有 RSpec 模板的 Rails 用法: $ rails new [application_name] -m https://raw.githubusercontent.com/wdi-sf-september-2014/rails_with_rspec_template/master/template.rb -TBd [database] 请参阅 template...
rspec_api_documentation, 从RSpec自动生成API文档 RSpec Doc为你的Rails API生成漂亮的。查看一个示例文件。更改请查看维基以了解最新的更改。安装将rspec_api_documentation添加到你的文件gem 'rspec_a
BDD(Rails 4 + RSpec + Cucumber) 脚步: 克隆这个 repo ... 安装 gem $ bundle install --path vendor/bundle ... 使用 Cucumber 运行验收测试 $ bundle exec cucumber features/home_page.feature
将此添加到您的Gemfile中: gem 'rspec_api_helpers'然后执行: $ bundle或将其自己安装为: $ gem install rspec_api_helpers将其包含在您的spec_helper中 RSpec . configure do | config | # ... config . ...
测试验证您编写的代码的行为并产生所需的结果。 您将在 Learn 使用测试中完成的许多实验。 乍一看,它可能感觉像是一个抽象的概念,但值得开始理解。 这样做将帮助您提高 Learn 的效率。 除此之外,理解测试很重要,...
ruby(rails)单元测试相关的gem,rspec、cucumeber。BDD