background
文件大小: unknow
源码售价: 5 个金币 积分规则     积分充值
资源说明:background
== Background

A web application that does a "pretend email sending" in the background. The background process then pings back to the client when the action is done (essentially a sleep for some seconds).

Uses resque, redis, faye to accomplish this.

== Installation

1. Install redis 

2. Do a bundle install to install gems resque, faye. Do a db set up (just creates a users table)

   bundle install
   rake db:migrate

3. Run the rake job to start listening to workers (resque side) - these workers listen to requests coming from the rails web app via user browser requests.

  RAILS_ENV=development rake resque:work QUEUE='*'

4. Start the faye server 

rackup faye.ru -s thin -E development

5. Start the web server

rails s

6. For checking the resque job queue - go to 

  http://localhost:3000/resque/queues


7. Create a new user by going to http://localhost:3000/users and clicking on New User link. To submit an email job for the user with user id 1

  Go to http://localhost:3000/users/1/ and click on the Email link

  You should get an alert from the server side in a few seconds. That is it - you have
  exercised the power of faye and redis to submit a job and get a message back
  from the server to the client asynchronously`.

When you click on the email link, following events happen:

. In the email action of users controller, we submit a job to Emailer worker
. The worker (app/workers/emailer.rb) picks the job in which it sleeps for a few seconds and then sends the "email sent" message to the faye server on the channel "/messages/1". In this example, the channel id is simply the user id but in real application, it would also have a random guid component so that we do not confuse between different messages and all.
. On the client, we are already listening on this channel - so we  get the message and give an alert - following is the logic in layout/application.html.erb that does this



8. As mentioned, in real application, you would create a separate channel (includes a random guid) for each job - so the clients do not interfere with each other. You would also make the worker more light weight by not loading the entire rails environment for each worker task.

本源码包内暂不包含可直接显示的源代码文件,请下载源码包。