-
Notifications
You must be signed in to change notification settings - Fork 75
/
README
133 lines (85 loc) · 5.5 KB
/
README
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
Workling
=============
This little library lets you take code outside of your request/response cycle. You can configure how it should be run. Currently,
there is a Spawn runner and a Starling runner.
This kind of sidesteps the issue of not knowing which Asynchronous code running method is the best for your rails app. Potentially,
you could swap in any mechanism you like. I asked Evan Weaver (http://blog.evanweaver.com/) what he thought:
"...If you really need a queue, use Starling, which Blaine Cook of Twitter
released, like, yesterday. Or SQS if you need really huge storage.
If you just want to fire and forget a local process as you say, I
think Spawn is pretty good ( http://rubyforge.org/projects/spawn ). I
haven't actually used it but seems like the best of the forking bunch.
That should eliminate the startup overhead. On the other hand, you
don't get any message reliability or cross-machine scheduling.
..I agree that BDrb is shady (actually all of Drb is shady). ap4r is too
bloated. Thruqueue is promising if you make it past the crazy
dependencies list. BackgroundFu is like a worse Spawn."
Example
=======
First, create a worker in app/workers:
class AnalyticsWorker < Workling::Base
def potential_invited(options)
Hit.create :potential_user_id => options[:potential_user_id], :action => "invited"
end
def potential_converted(options)
Hit.create :potential_user_id => options[:potential_user_id], :action => "converted"
end
end
then, call it like this anywhere in your code:
Workling::Remote.run(:analytics_worker, :potential_invited, :potential_user_id => 1234)
Runners
=======
configure runners in your environment.rb:
Workling::Remote.dispatcher = Workling::Remote::Runners::NotRemoteRunner.new (this runner just executes everything normally)
SpawnRunner
===========
this uses http://rubyforge.org/projects/spawn to take the process out of the request cycle.
configure it like this:
SpawnRunner.options = { :method => :spawn }
StarlingRunner
==============
This uses Twitter's Starling to enable your asynch code to run on different VMs. Activate it like this in your environment:
Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new
This takes care of several things:
1: mapping of queue names to worker code. this is done with Workling::Starling::Routing::ClassAndMethodRouting, but you can use your own by sublassing
Workling::Starling::Routing::Base. Some examples of Worker to queue routing:
AnalyticsWorker class above routes the queues 'analytics_worker:potential_invited' and 'analytics_worker:potential_converted'
to those methods on AnalyticsWorker. If you put your worker in a module, the queue will start with the_module_name:.
2: there's a client daemon that waits for messages and dispatches these to the responsible workers. if you intend to run this
on a remote machine, then just check out your rails project there and start it up like this:
Other runners
=============
workling comes with a spawn based runner. this currently seems to be one of the better spawning/forking options. use this if you don’t want to run a separate starling server in your setup.
Getting started with starling and workling
==========================================
start by installing starling and a memcached client. then install workling into your rails application. install the spawn plugin if you want to use spawn as your runner.
sudo gem install starling --include-dependencies
sudo gem install memcache-client --include-dependencies
./script/plugin install http://svn.playtype.net/plugins/workling/
./script/plugin install http://spawn.rubyforge.org/svn/spawn/
now you can start the little twitter birdie, followed by workling. and off you go!
mkdir /var/spool/starling
sudo starling -d
script/workling_starling_client start
also make sure you configure your starling server to your needs in config/starling.yml before you push this onto prodcution.
Progress indicators and return stores
=====================================
Your worklings can write back to a return store. This allows you to write progress indicators, or access results from your workling. As above, this is fairly slim. The cool thing is, that you can swap in any return store implementation you like without changing your code. For tests, there is a memory return store, for production use there is currently a starling return store. You can easily add a new return store (over the database for instance) by subclassing Workling::Return::Store::Base. Configure it like this in your test environment:
Workling::Return::Store.instance = Workling::Return::Store::MemoryReturnStore.new
Here is an example workling that crawls an addressbook and puts results in a return store. Worling makes sure you have options[:uid] in your hash - pass this into the return store with your results.
require 'blackbook'
class NetworkWorker < Workling::Base
def search(options)
results = Blackbook.get(options[:key], options[:username], options[:password])
Workling::Return.set(options[:uid], results)
end
end
call your workling as above:
@uid = Workling::Remote.run(:network_worker, :search, { :key => :gmail, :username => "[email protected]", :password => "bar" })
or simply:
@uid = NetworkWorker.asynch_search({ :key => :gmail, :username => "[email protected]", :password => "bar" })
you can now use the @uid to query the return store:
results = Workling::Return.get(@uid)
of course, you can use this for progress indicators. just put the progress into the return store.
enjoy!
Copyright (c) 2008 play/type GmbH, released under the MIT license