forked from ncr/background-fu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.txt
177 lines (121 loc) · 4.88 KB
/
README.txt
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
= BackgroundFu
* http://github.com/ncr/background-fu
* git://github.com/ncr/background-fu.git
* http://trix.lighthouseapp.com/projects/9809-backgroundfu
* 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
== 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.