简体   繁体   English

如何在Rails中实现自动启动和退出守护程序

[英]How to implement auto starting and exiting daemons in rails

I'm a bit overwhelmed by mere amount a possible solutions the Rails community has created for my problem. Rails社区为我的问题创建了一个可能的解决方案,这让我有些不知所措。 So perhaps anyone can help me to figure out how to solve it best. 因此,也许任何人都可以帮助我找出最佳解决方案。

What I want to do is to write a Rails app that behaves kind of "dropbox". 我想做的是编写一个行为类似于“ Dropbox”的Rails应用程序。 On the one hand it should be a web interface where I can upload and download files to my web server. 一方面,它应该是一个Web界面,在这里我可以将文件上传和下载到Web服务器。 This interacts with my database and all that stuff. 这与我的数据库以及所有其他东西交互。 On the other hand I have SSH access to that server and can put files there manually. 另一方面,我可以通过SSH访问该服务器,并且可以手动将文件放置在该服务器上。 Now I want this file system actions to trigger my Rails app to do the things it would do if I'd created the file via the web interface. 现在,我希望此文件系统操作可以触发我的Rails应用程序执行通过Web界面创建文件时的操作。

So I somehow write a daemon, right? 所以我以某种方式写了一个守护进程,对吗? There are a lot of solutions, like 有很多解决方案,例如
daemons.rubyforge.org/ daemons.rubyforge.org/
github.com/mirasrael/daemons-rails github.com/mirasrael/daemons-rails
github.com/costan/daemonz github.com/costan/daemonz
github.com/kennethkalmer/daemon-kit github.com/kennethkalmer/daemon-kit

Another feature that I would like to have, is that my Rails app automatically spawns and stops my daemon as start or quit my Rails app resp. 我想拥有的另一个功能是,在启动或退出我的Rails应用程序响应时,Rails应用程序会自动生成并停止守护进程。 So "daemonz" seems the best solution. 因此,“ daemonz”似乎是最佳解决方案。 But as I googled further I found 但是当我进一步搜寻时,我发现
github.com/FooBarWidget/daemon_controller/ github.com/FooBarWidget/daemon_controller/
which seems a lot more "high tech" and already used as I deploy with passenger. 这似乎是更多的“高科技”,并且在我与乘客一起部署时已经使用。 But I don't understand if it kills my daemons as I quit Rails. 但是我不知道它在退出Rails时是否杀死了我的守护进程。 I suppose that is not the case and so I wonder how to implement this in my app. 我想情况并非如此,所以我想知道如何在我的应用程序中实现这一点。

The way to implement a "thing" to react to file system changes seems straight forward for me. 对我来说,实现对文件系统更改做出反应的“事物”的方法似乎很简单。 I'd use 我会用
github.com/guard/listen/ github.com/guard/listen/
(an alternative would be: github.com/ttilley/fssm ) (另一个选择是:github.com/ttilley/fssm)
But what I don't understand as this the first time I'm really faced with this protocol things is, if this spawns a server I'm able to communicate with or what kind of object I have to deal with. 但是,我第一次真正面对该协议时就不了解,这是,如果它产生了一个我可以与之通信的服务器,或者我必须处理哪种对象。

The last thing, I would like to implement is a kind of worker queue so that the listening for file system changes is seperated from the the actions of my rails app. 我想实现的最后一件事是一种工作队列,以便将监听文件系统更改与我的rails应用程序的操作分开。 But there are so many solutions that I'm totally overwhelmed to pick one: 但是有太多解决方案,我完全不知所措,只能选择一种:
github.com/tobi/delayed_job/ github.com/tobi/delayed_job/
github.com/defunkt/resque github.com/defunkt/resque
http://backgroundrb.rubyforge.org/ http://backgroundrb.rubyforge.org/

And what is 是什么
http://godrb.com/ http://godrb.com/
all about? 所有关于? How could that help me? 那对我有什么帮助?

Has anyone hints how to solve this? 有没有人暗示如何解决这个问题? Thanks a lot! 非常感谢!

Jan 一月

PS I'd like to post links to all the github projects but unfortunately I don't have enough 'reputation' PS:我想发布指向所有github项目的链接,但不幸的是我没有足够的“声誉”

I'd definitely look into creating a process (daemon) that monitors the relevant directory. 我肯定会考虑创建一个监视相关目录的进程(守护程序)。 Then your Rails app can just put files into it without having to know anything about the back end, and it'll work with SSH too. 然后,您的Rails应用程序可以直接将文件放入其中,而无需了解后端的任何知识,它也可以与SSH一起使用。

Your daemon can load the Rails environment & communicate with your database. 您的守护程序可以加载Rails环境并与数据库进行通信。 I'd leave all the communication between them at that level. 我会把他们之间的所有沟通都留在那个水平上。

As for making it start/stop with your rails app...are you sure? 至于使用Rails应用程序启动/停止...确定吗? I use god (the ruby gem) to start/monitor processes. 我使用上帝(红宝石宝石)来启动/监视进程。 It will "daemonize" your Ruby app for you, too. 它将也为您“守护”您的Ruby应用程序。 If you want to, you can actually tell god to stop your directory-monitor process & then exit when Rails stops. 如果愿意,您实际上可以告诉上帝停止您的目录监视进程,然后在Rails停止时退出。 And you can fire off god from a Rails initializer. 您可以从Rails初始化程序中解雇上帝。

However, if you might find yourself using SSH or some other means to put files into that directory when rails is not running, you might look into putting a script into /etc/init.d to automatically start god when the server boots up. 但是,如果您可能发现自己在不运行Rails时使用SSH或其他方式将文件放入该目录,则可以考虑将脚本放入/etc/init.d以在服务器启动时自动启动。

HTH 高温超导

I think you want something like Guard for monitoring the changes on the filesystem and performing actions when changes occur. 我认为您想要像Guard这样的工具来监视文件系统上的更改并在更改发生时执行操作。

As for god, you should definitely look into it. 至于上帝,您绝对应该研究一下。 It will make starting/stopping processes you depend on considerably easier. 这将使您所依赖的启动/停止过程变得更加容易。 We used Bluepill for a while, but there are so many bugs, we ditched it and moved to God, which IMHO is a lot more pleasant to work with, for the mostpart. 我们使用Bluepill已有一段时间,但是有很多错误,我们抛弃了它并移交给了上帝,在大多数情况下,恕我直言,与IMHO合作更愉快。

Have you tried creating a script file eg: 您是否尝试过创建脚本文件,例如:

startDaemon.rb

And then placing it: 然后将其放置:

config/initializers/ 

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM