public
Description: Running Long Background Tasks In Ruby On Rails Made Dead Simple
Homepage:
Clone URL: git://github.com/ncr/background-fu.git
Click here to lend your support to: background-fu and make a donation at www.pledgie.com !
opencomputing (author)
Mon May 18 02:18:07 -0700 2009
ncr (committer)
Mon May 18 02:27:05 -0700 2009
commit  8822b81078f4c3c4922fc9ed57697b320ccac645
tree    09ff7837b50a990b0fc24e079ffd9a6a017dc9c7
parent  e83f775e448fda24b2df3140576faa9d1d74671e
background-fu / README.txt
100644 190 lines (129 sloc) 5.43 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
= BackgroundFu
 
* http://github.com/ncr/background-fu
* git://github.com/ncr/background-fu.git
* http://trix.lighthouseapp.com/projects/9809-backgroundfu
* ncr@trix.pl
* http://trix.pl
 
== DESCRIPTION:
 
Background tasks in Ruby On Rails made dead simple.
 
== FEATURES/PROBLEMS:
 
* Running long background tasks outside of request-response cycle.
* Very easy to setup and fun to use (See examples below).
* Clean and straightforward approach (database-based priority queue).
* Uses database table (migration included) to store jobs reliably.
* Capistrano tasks included.
* Generators with migrations and example views (Prototype required) included (to be used with concurrency enabled).
* Multiple worker daemons available.
* Easy to deploy in distributed environments.
* Enables prioritizing and simple scheduling.
* Optional worker monitoring (good for AJAX progress bars).
* Proven its stability and reliability in production use.
 
 
== SYNOPSIS:
 
  ruby ./script/generate background
  rake db:migrate
  
  # to run in production mode use RAILS_ENV=production ruby ./script/daemons start
  ruby ./script/daemons start
 
  # then try in console:
    
  job_id = Job.enqueue!(ExampleWorker, :add, 1, 2).id
 
  # after few seconds when background daemon completes the job
 
  Job.find(job_id).result # result of the job should equal 3
  
  If you want to use default generated views, update your config/routes.rb:
  
  map.namespace "admin" do |admin|
    admin.resources :jobs
  end
  
  Then you can point your browser to http://localhost:3000/admin/jobs
 
== EXAMPLES:
 
  # In lib/workers/example_worker.rb:
 
  # Simple, non-monitored worker.
  class ExampleWorker
  
    def add(a, b)
      a + b
    end
 
  end
 
  # In lib/workers/example_monitored_worker.rb:
 
  # Remeber to include BackgroundFu::WorkerMonitoring.
  class ExampleMonitoredWorker
 
    include BackgroundFu::WorkerMonitoring
  
    def long_and_monitored
      my_progress = 0
    
      record_progress(my_progress)
 
      while(my_progress < 100)
        my_progress += 1
        record_progress(my_progress)
        sleep 1
      end
    
      record_progress(100)
    end
  
  end
 
  # In a controller:
 
    def create
      session[:job_id] = Job.enqueue!(ExampleWorker, :add, 1, 2).id
      # or try the monitored worker: session[:job_id] = Job.enqueue!(ExampleMonitoredWorker, :long_and_monitored)
    end
 
    def show
      @job = Job.find(session[:job_id])
      @result = @job.result if @job.finished?
      # or check progress if your worker is monitored: @progress = @job.progress
    end
  
    def index
      @jobs = Job.find(:all)
    end
  
    def destroy
      Job.find(session[:job_id]).destroy
    end
 
== HANDY CAPISTRANO TASKS:
 
  namespace :deploy do
 
    desc "Run this after every successful deployment"
    task :after_default do
      restart_background_fu
    end
 
  end
 
  desc "Restart BackgroundFu daemon"
  task :restart_background_fu do
    run "RAILS_ENV=production ruby #{current_path}/script/daemons stop"
    run "RAILS_ENV=production ruby #{current_path}/script/daemons start"
  end
 
== CONFIGURATION:
 
  By default, completed background jobs that are more than one week old
  will be cleaned out of the database on application startup, and the daemon
  will check for queued background jobs every 5 seconds.
  
  To override these settings, modify your config/daemons.yml file:
  
  background_fu:
    cleanup_interval: :on_startup # :on_startup | :continuous
    monitor_interval: 5 # Check every x seconds
 
== BONUS FEATURES:
 
There are bonus features available if you set
config.active_record.allow_concurrency = true
in your environment.
 
These features are:
 * monitoring progress (perfect for ajax progress bars)
 * stopping a running worker in a merciful way.
 
Generated admin views are meant to be used only when bonus features (concurrency) are enabled.
 
Read the code (short and easy) to discover them.
 
== REQUIREMENTS:
 
* rails
* daemons
 
== INSTALL:
 
* As a Rails plugin: ./script/plugin install git://github.com/ncr/background-fu.git
* As as a gem to be 'vendorized' starting from Rails 2.1: refer to documentation on rake gems:unpack:dependencies.
 
== CONTRIBUTING:
 
If you want to help improve this plugin, feel free to contact me. Fork the project on GitHub, implement a feature, send me a pull request.
 
== LICENSE:
 
(The MIT License)
 
Copyright (c) Jacek Becela
 
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.