如何在 Rails 应用程序中创建 redis 侦听器 [英] How to create a redis listener inside a Rails app

查看:47
本文介绍了如何在 Rails 应用程序中创建 redis 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景如下:

我有一个 Rails 应用程序,它有一个用于记录系统事件的活动记录模型.这些系统事件可以来自 rails 应用程序或来自单独的 Ruby 应用程序.ruby 应用程序当前将其事件发布到 Redis 队列.

I have a Rails app which has an active record model for writing down system events. Those system events can come from the rails app or from a separate Ruby app. The ruby app currently publishes its events to a Redis queue.

Rails 应用程序中是否可以启动 Redis 侦听器并订阅队列?

Is there a way within the Rails app to start a Redis listener and subscribe to the queue?

推荐答案

正如@sergio 所提到的,您的 rails 应用程序不是 redis 客户端 的地方.rails web 应用程序是一个服务器端应用程序,它响应来自客户端的请求.这就是为什么,您需要一个单独的进程(最好是一个守护进程)作为您的 redis 服务器的客户端.

As mentioned by @sergio your rails application is not the place for a redis client. A rails web application is a server side application which responds to requests from clients. That is why, what you need is a separate process (preferably a daemon) which acts as a client to your redis server.

要守护您的 redis 客户端,您可以使用 daemons gem.我通常将我的守护进程放在 /lib/daemons/

To daemonize your redis client you can use the daemons gem. I typically place my daemons in <app-root>/lib/daemons/

您可以在守护进程中加载​​完整的 Rails 环境,方法是在开头包含以下几行:

You can load your complete Rails environment in your daemon process by including these lines at the beginning :

require File.dirname(__FILE__) + "/../config/application"
Rails.application.require_environment!

这样您就可以访问您的模型并可以通过您的模型类与您的数据库进行交互.

That way you will have access to your models and can interact with your DB through your model classes.

这篇关于如何在 Rails 应用程序中创建 redis 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆