resque 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of resque might be problematic. Click here for more details.

Files changed (61) hide show
  1. data/.kick +26 -0
  2. data/HISTORY.md +3 -0
  3. data/LICENSE +20 -0
  4. data/README.markdown +638 -0
  5. data/Rakefile +61 -0
  6. data/TODO.md +60 -0
  7. data/bin/resque +57 -0
  8. data/bin/resque-web +47 -0
  9. data/config.ru +8 -0
  10. data/deps.rip +5 -0
  11. data/examples/async_helper.rb +31 -0
  12. data/examples/demo/README.markdown +71 -0
  13. data/examples/demo/Rakefile +3 -0
  14. data/examples/demo/app.rb +27 -0
  15. data/examples/demo/config.ru +19 -0
  16. data/examples/demo/job.rb +12 -0
  17. data/examples/existing_classes_as_jobs.rb +3 -0
  18. data/examples/instance.rb +11 -0
  19. data/examples/simple.rb +30 -0
  20. data/init.rb +1 -0
  21. data/lib/resque.rb +184 -0
  22. data/lib/resque/errors.rb +7 -0
  23. data/lib/resque/failure.rb +57 -0
  24. data/lib/resque/failure/base.rb +54 -0
  25. data/lib/resque/failure/hoptoad.rb +88 -0
  26. data/lib/resque/failure/redis.rb +28 -0
  27. data/lib/resque/helpers.rb +57 -0
  28. data/lib/resque/job.rb +91 -0
  29. data/lib/resque/server.rb +154 -0
  30. data/lib/resque/server/public/idle.png +0 -0
  31. data/lib/resque/server/public/jquery-1.3.2.min.js +19 -0
  32. data/lib/resque/server/public/jquery.relatize_date.js +95 -0
  33. data/lib/resque/server/public/nav-bg.png +0 -0
  34. data/lib/resque/server/public/ranger.js +7 -0
  35. data/lib/resque/server/public/reset.css +51 -0
  36. data/lib/resque/server/public/style.css +67 -0
  37. data/lib/resque/server/public/tab_b.gif +0 -0
  38. data/lib/resque/server/public/tab_r.gif +0 -0
  39. data/lib/resque/server/public/tabs.css +189 -0
  40. data/lib/resque/server/public/working.png +0 -0
  41. data/lib/resque/server/views/error.erb +1 -0
  42. data/lib/resque/server/views/failed.erb +29 -0
  43. data/lib/resque/server/views/key.erb +17 -0
  44. data/lib/resque/server/views/layout.erb +43 -0
  45. data/lib/resque/server/views/next_more.erb +12 -0
  46. data/lib/resque/server/views/overview.erb +2 -0
  47. data/lib/resque/server/views/queues.erb +40 -0
  48. data/lib/resque/server/views/stats.erb +62 -0
  49. data/lib/resque/server/views/workers.erb +72 -0
  50. data/lib/resque/server/views/working.erb +66 -0
  51. data/lib/resque/stat.rb +53 -0
  52. data/lib/resque/tasks.rb +24 -0
  53. data/lib/resque/version.rb +3 -0
  54. data/lib/resque/worker.rb +406 -0
  55. data/tasks/redis.rake +125 -0
  56. data/tasks/resque.rake +2 -0
  57. data/test/redis-test.conf +132 -0
  58. data/test/resque_test.rb +160 -0
  59. data/test/test_helper.rb +90 -0
  60. data/test/worker_test.rb +212 -0
  61. metadata +124 -0
@@ -0,0 +1,125 @@
1
+ # Inspired by rabbitmq.rake the Redbox project at http://github.com/rick/redbox/tree/master
2
+ require 'fileutils'
3
+ require 'open-uri'
4
+
5
+ class RedisRunner
6
+
7
+ def self.redisdir
8
+ "/tmp/redis/"
9
+ end
10
+
11
+ def self.redisconfdir
12
+ '/etc/redis.conf'
13
+ end
14
+
15
+ def self.dtach_socket
16
+ '/tmp/redis.dtach'
17
+ end
18
+
19
+ # Just check for existance of dtach socket
20
+ def self.running?
21
+ File.exists? dtach_socket
22
+ end
23
+
24
+ def self.start
25
+ puts 'Detach with Ctrl+\ Re-attach with rake redis:attach'
26
+ sleep 1
27
+ exec "dtach -A #{dtach_socket} redis-server #{redisconfdir}"
28
+ end
29
+
30
+ def self.attach
31
+ exec "dtach -a #{dtach_socket}"
32
+ end
33
+
34
+ def self.stop
35
+ sh 'echo "SHUTDOWN" | nc localhost 6379'
36
+ end
37
+
38
+ end
39
+
40
+ namespace :redis do
41
+
42
+ desc 'About redis'
43
+ task :about do
44
+ puts "\nSee http://code.google.com/p/redis/ for information about redis.\n\n"
45
+ end
46
+
47
+ desc 'Start redis'
48
+ task :start do
49
+ RedisRunner.start
50
+ end
51
+
52
+ desc 'Stop redis'
53
+ task :stop do
54
+ RedisRunner.stop
55
+ end
56
+
57
+ desc 'Restart redis'
58
+ task :restart do
59
+ RedisRunner.stop
60
+ RedisRunner.start
61
+ end
62
+
63
+ desc 'Attach to redis dtach socket'
64
+ task :attach do
65
+ RedisRunner.attach
66
+ end
67
+
68
+ desc 'Install the lastest verison of Redis from Github (requires git, duh)'
69
+ task :install => [:about, :download, :make] do
70
+ %w(redis-benchmark redis-cli redis-server).each do |bin|
71
+ sh "sudo cp /tmp/redis/#{bin} /usr/bin/"
72
+ end
73
+
74
+ puts "Installed redis-benchmark, redis-cli and redis-server to /usr/bin/"
75
+
76
+ unless File.exists?('/etc/redis.conf')
77
+ sh 'sudo cp /tmp/redis/redis.conf /etc/'
78
+ puts "Installed redis.conf to /etc/ \n You should look at this file!"
79
+ end
80
+ end
81
+
82
+ task :make do
83
+ sh "cd #{RedisRunner.redisdir} && make clean"
84
+ sh "cd #{RedisRunner.redisdir} && make"
85
+ end
86
+
87
+ desc "Download package"
88
+ task :download do
89
+ sh 'rm -rf /tmp/redis/' if File.exists?("#{RedisRunner.redisdir}/.svn")
90
+ sh 'git clone git://github.com/antirez/redis.git /tmp/redis' unless File.exists?(RedisRunner.redisdir)
91
+ sh "cd #{RedisRunner.redisdir} && git pull" if File.exists?("#{RedisRunner.redisdir}/.git")
92
+ end
93
+
94
+ end
95
+
96
+ namespace :dtach do
97
+
98
+ desc 'About dtach'
99
+ task :about do
100
+ puts "\nSee http://dtach.sourceforge.net/ for information about dtach.\n\n"
101
+ end
102
+
103
+ desc 'Install dtach 0.8 from source'
104
+ task :install => [:about] do
105
+
106
+ Dir.chdir('/tmp/')
107
+ unless File.exists?('/tmp/dtach-0.8.tar.gz')
108
+ require 'net/http'
109
+
110
+ url = 'http://downloads.sourceforge.net/project/dtach/dtach/0.8/dtach-0.8.tar.gz'
111
+ open('/tmp/dtach-0.8.tar.gz', 'wb') do |file| file.write(open(url).read) end
112
+ end
113
+
114
+ unless File.directory?('/tmp/dtach-0.8')
115
+ system('tar xzf dtach-0.8.tar.gz')
116
+ end
117
+
118
+ Dir.chdir('/tmp/dtach-0.8/')
119
+ sh 'cd /tmp/dtach-0.8/ && ./configure && make'
120
+ sh 'sudo cp /tmp/dtach-0.8/dtach /usr/bin/'
121
+
122
+ puts 'Dtach successfully installed to /usr/bin.'
123
+ end
124
+ end
125
+
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'resque/tasks'
@@ -0,0 +1,132 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile ./test/redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9736
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ dir ./test/
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
116
+
117
+ # Use object sharing. Can save a lot of memory if you have many common
118
+ # string in your dataset, but performs lookups against the shared objects
119
+ # pool so it uses more CPU and can be a bit slower. Usually it's a good
120
+ # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
131
+ shareobjects no
132
+ shareobjectspoolsize 1024
@@ -0,0 +1,160 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ context "Resque" do
4
+ setup do
5
+ Resque.redis.flush_all
6
+
7
+ Resque.push(:people, { 'name' => 'chris' })
8
+ Resque.push(:people, { 'name' => 'bob' })
9
+ Resque.push(:people, { 'name' => 'mark' })
10
+ end
11
+
12
+ test "can put jobs on a queue" do
13
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
14
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
15
+ end
16
+
17
+ test "can grab jobs off a queue" do
18
+ Resque::Job.create(:jobs, 'some-job', 20, '/tmp')
19
+
20
+ job = Resque.reserve(:jobs)
21
+
22
+ assert_kind_of Resque::Job, job
23
+ assert_equal SomeJob, job.payload_class
24
+ assert_equal 20, job.args[0]
25
+ assert_equal '/tmp', job.args[1]
26
+ end
27
+
28
+ test "can put jobs on a queue by way of an ivar" do
29
+ assert_equal 0, Resque.size(:ivar)
30
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
31
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
32
+
33
+ job = Resque.reserve(:ivar)
34
+
35
+ assert_kind_of Resque::Job, job
36
+ assert_equal SomeIvarJob, job.payload_class
37
+ assert_equal 20, job.args[0]
38
+ assert_equal '/tmp', job.args[1]
39
+
40
+ assert Resque.reserve(:ivar)
41
+ assert_equal nil, Resque.reserve(:ivar)
42
+ end
43
+
44
+ test "jobs have a nice #inspect" do
45
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
46
+ job = Resque.reserve(:jobs)
47
+ assert_equal '(Job{jobs} | SomeJob | [20, "/tmp"])', job.inspect
48
+ end
49
+
50
+ test "can put jobs on a queue by way of a method" do
51
+ assert_equal 0, Resque.size(:method)
52
+ assert Resque.enqueue(SomeMethodJob, 20, '/tmp')
53
+ assert Resque.enqueue(SomeMethodJob, 20, '/tmp')
54
+
55
+ job = Resque.reserve(:method)
56
+
57
+ assert_kind_of Resque::Job, job
58
+ assert_equal SomeMethodJob, job.payload_class
59
+ assert_equal 20, job.args[0]
60
+ assert_equal '/tmp', job.args[1]
61
+
62
+ assert Resque.reserve(:method)
63
+ assert_equal nil, Resque.reserve(:method)
64
+ end
65
+
66
+ test "needs to infer a queue with enqueue" do
67
+ assert_raises Resque::NoQueueError do
68
+ Resque.enqueue(SomeJob, 20, '/tmp')
69
+ end
70
+ end
71
+
72
+ test "can put items on a queue" do
73
+ assert Resque.push(:people, { 'name' => 'jon' })
74
+ end
75
+
76
+ test "can pull items off a queue" do
77
+ assert_equal({ 'name' => 'chris' }, Resque.pop(:people))
78
+ assert_equal({ 'name' => 'bob' }, Resque.pop(:people))
79
+ assert_equal({ 'name' => 'mark' }, Resque.pop(:people))
80
+ assert_equal nil, Resque.pop(:people)
81
+ end
82
+
83
+ test "knows how big a queue is" do
84
+ assert_equal 3, Resque.size(:people)
85
+
86
+ assert_equal({ 'name' => 'chris' }, Resque.pop(:people))
87
+ assert_equal 2, Resque.size(:people)
88
+
89
+ assert_equal({ 'name' => 'bob' }, Resque.pop(:people))
90
+ assert_equal({ 'name' => 'mark' }, Resque.pop(:people))
91
+ assert_equal 0, Resque.size(:people)
92
+ end
93
+
94
+ test "can peek at a queue" do
95
+ assert_equal({ 'name' => 'chris' }, Resque.peek(:people))
96
+ assert_equal 3, Resque.size(:people)
97
+ end
98
+
99
+ test "can peek multiple items on a queue" do
100
+ assert_equal({ 'name' => 'bob' }, Resque.peek(:people, 1, 1))
101
+
102
+ assert_equal([{ 'name' => 'bob' }, { 'name' => 'mark' }], Resque.peek(:people, 1, 2))
103
+ assert_equal([{ 'name' => 'chris' }, { 'name' => 'bob' }], Resque.peek(:people, 0, 2))
104
+ assert_equal([{ 'name' => 'chris' }, { 'name' => 'bob' }, { 'name' => 'mark' }], Resque.peek(:people, 0, 3))
105
+ assert_equal({ 'name' => 'mark' }, Resque.peek(:people, 2, 1))
106
+ assert_equal nil, Resque.peek(:people, 3)
107
+ assert_equal [], Resque.peek(:people, 3, 2)
108
+ end
109
+
110
+ test "knows what queues it is managing" do
111
+ assert_equal %w( people ), Resque.queues
112
+ Resque.push(:cars, { 'make' => 'bmw' })
113
+ assert_equal %w( cars people ), Resque.queues
114
+ end
115
+
116
+ test "queues are always a list" do
117
+ Resque.redis.flush_all
118
+ assert_equal [], Resque.queues
119
+ end
120
+
121
+ test "keeps track of resque keys" do
122
+ assert_equal ["queue:people", "queues"], Resque.keys
123
+ end
124
+
125
+ test "badly wants a class name, too" do
126
+ assert_raises Resque::NoClassError do
127
+ Resque::Job.create(:jobs, nil)
128
+ end
129
+ end
130
+
131
+ test "keeps stats" do
132
+ Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
133
+ Resque::Job.create(:jobs, BadJob)
134
+ Resque::Job.create(:jobs, GoodJob)
135
+
136
+ Resque::Job.create(:others, GoodJob)
137
+ Resque::Job.create(:others, GoodJob)
138
+
139
+ stats = Resque.info
140
+ assert_equal 8, stats[:pending]
141
+
142
+ @worker = Resque::Worker.new(:jobs)
143
+ @worker.register_worker
144
+ 2.times { @worker.process }
145
+ job = @worker.reserve
146
+ @worker.working_on job
147
+
148
+ stats = Resque.info
149
+ assert_equal 1, stats[:working]
150
+ assert_equal 1, stats[:workers]
151
+
152
+ @worker.done_working
153
+
154
+ stats = Resque.info
155
+ assert_equal 3, stats[:queues]
156
+ assert_equal 3, stats[:processed]
157
+ assert_equal 1, stats[:failed]
158
+ assert_equal ['localhost:9736'], stats[:servers]
159
+ end
160
+ end
@@ -0,0 +1,90 @@
1
+ dir = File.dirname(File.expand_path(__FILE__))
2
+ $LOAD_PATH.unshift dir + '/../lib'
3
+ $TESTING = true
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require 'resque'
7
+
8
+
9
+ #
10
+ # make sure we can run redis
11
+ #
12
+
13
+ if `which redis-server`.chomp.empty?
14
+ puts '', "** can't find `redis-server` in your path"
15
+ puts "** try running `sudo rake install`"
16
+ abort ''
17
+ end
18
+
19
+
20
+ #
21
+ # start our own redis when the tests start,
22
+ # kill it when they end
23
+ #
24
+
25
+ at_exit do
26
+ unless $! || Test::Unit.run?
27
+ status = Test::Unit::AutoRunner.run
28
+ pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
29
+ puts "Killing test redis server..."
30
+ `rm -f #{dir}/dump.rdb`
31
+ Process.kill("KILL", pid.to_i)
32
+ exit status
33
+ end
34
+ end
35
+
36
+ puts "Starting redis for testing at localhost:9736..."
37
+ `redis-server #{dir}/redis-test.conf`
38
+ Resque.redis = 'localhost:9736'
39
+
40
+
41
+ ##
42
+ # test/spec/mini 2
43
+ # http://gist.github.com/25455
44
+ # chris@ozmm.org
45
+ #
46
+ def context(*args, &block)
47
+ return super unless (name = args.first) && block
48
+ require 'test/unit'
49
+ klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
50
+ def self.test(name, &block)
51
+ define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
52
+ end
53
+ def self.xtest(*args) end
54
+ def self.setup(&block) define_method(:setup, &block) end
55
+ def self.teardown(&block) define_method(:teardown, &block) end
56
+ end
57
+ klass.class_eval &block
58
+ end
59
+
60
+
61
+ #
62
+ # fixture classes
63
+ #
64
+
65
+ class SomeJob
66
+ def self.perform(repo_id, path)
67
+ end
68
+ end
69
+
70
+ class SomeIvarJob < SomeJob
71
+ @queue = :ivar
72
+ end
73
+
74
+ class SomeMethodJob < SomeJob
75
+ def self.queue
76
+ :method
77
+ end
78
+ end
79
+
80
+ class BadJob
81
+ def self.perform
82
+ raise "Bad job!"
83
+ end
84
+ end
85
+
86
+ class GoodJob
87
+ def self.perform(name)
88
+ "Good job, #{name}"
89
+ end
90
+ end