Best practice for Rails App to run a long task in the background?

后端 未结 14 1443
时光取名叫无心
时光取名叫无心 2020-12-07 20:49

I have a Rails application that unfortunately after a request to a controller, has to do some crunching that takes awhile. What are the best practices in Rails for providin

相关标签:
14条回答
  • 2020-12-07 21:18

    This really does sound like something that you should have a background process running rather than an application instance(passenger/mongrel whichever you use) as that way your application can stay doing what it's supposed to be doing, serving requests, while a background task of some kind, Workling is good, handles the number crunching. I know that this doesn't deal with the issue of progress, but unless it is absolutely essential I think that is a small price to pay.

    You could have a user click the action required, have that action pass the request to the Workling queue, and have it send some kind of notification to the user when it is completed, maybe an email or something. I'm not sure about the practicality of that, just thinking out loud, but my point is that it really seems like that should be a background task of some kind.

    0 讨论(0)
  • 2020-12-07 21:18

    I personally use active_messaging plugin with a activemq server (stomp or rest protocol). This has been extremely stable for us, processing millions of messages a month.

    0 讨论(0)
  • 2020-12-07 21:19

    I recommend using Resque gem with it's resque-status plug-in for your heavy background processes.

    Resque

    Resque is a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later.

    Resque-status

    resque-status is an extension to the resque queue system that provides simple trackable jobs.

    Once you run a job on a Resque worker using resque-status extension, you will be able to get info about your ongoing progresses and ability to kill a specific process very easily. See examples:

    status.pct_complete #=> 0
    status.status #=> 'queued'
    status.queued? #=> true
    status.working? #=> false
    status.time #=> Time object        
    status.message #=> "Created at ..."
    

    Also resque and resque-status has a cool web interface to interact with your jobs which is so cool.

    0 讨论(0)
  • 2020-12-07 21:20

    There is the brand new Growl4Rails ... that is for this specific use case (among others as well).

    http://www.writebetterbits.com/2009/01/update-to-growl4rails.html

    0 讨论(0)
  • 2020-12-07 21:21

    This looks quite an old thread. However, what I have down in my app, which required to run multiple Countdown Timers for different pages, was to use Ruby Thread. The timer must continue running even if the page was closed by users.

    Ruby makes it easy to write multi-threaded programs with the Thread class. Ruby threads are a lightweight and efficient way to achieve parallelism in your code. I hope this will help other wanderers who is looking to achieve background: parallelism/concurrent services in their app. Likewise Ajax makes it a lot easier to call a specific Rails [custom] action every second.

    0 讨论(0)
  • 2020-12-07 21:24

    I'm using Windows Vista 64 bit for my development machine; however, my production machine is Ubuntu 8.04 LTS. Should I consider switching to Linux for my development machine? Will the solutions presented work on both?

    Have you considered running Linux in a VM on top of Vista?

    0 讨论(0)
提交回复
热议问题