gearman-ruby 3.0.8 → 4.0.2

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.
Files changed (54) hide show
  1. checksums.yaml +8 -8
  2. data/.travis.yml +5 -0
  3. data/CHANGELOG.md +6 -4
  4. data/README.md +111 -0
  5. data/examples/client.rb +1 -2
  6. data/examples/client_reverse_nohost.rb +30 -0
  7. data/examples/{client_reverse.rb → client_reverse_wait.rb} +9 -11
  8. data/examples/worker.rb +8 -5
  9. data/examples/worker_reverse_string.rb +12 -17
  10. data/gearman-ruby.gemspec +3 -5
  11. data/lib/gearman.rb +17 -77
  12. data/lib/gearman/client.rb +129 -147
  13. data/lib/gearman/connection.rb +158 -0
  14. data/lib/gearman/connection_pool.rb +131 -0
  15. data/lib/gearman/exceptions.rb +24 -0
  16. data/lib/gearman/logging.rb +19 -0
  17. data/lib/gearman/packet.rb +61 -0
  18. data/lib/gearman/task.rb +1 -1
  19. data/lib/gearman/task_set.rb +67 -0
  20. data/lib/gearman/version.rb +1 -1
  21. data/lib/gearman/worker.rb +185 -412
  22. data/lib/gearman/worker/ability.rb +55 -0
  23. data/lib/gearman/worker/callbacks.rb +39 -0
  24. data/lib/gearman/worker/job.rb +44 -0
  25. data/spec/client_spec.rb +32 -20
  26. data/spec/connection_pool_spec.rb +55 -0
  27. data/spec/spec_helper.rb +5 -0
  28. data/spec/task_spec.rb +10 -0
  29. data/spec/taskset_spec.rb +2 -2
  30. metadata +18 -37
  31. data/HOWTO +0 -146
  32. data/README +0 -9
  33. data/TODO +0 -8
  34. data/VERSION.yml +0 -4
  35. data/examples/calculus_client.rb +0 -39
  36. data/examples/calculus_worker.rb +0 -45
  37. data/examples/client.php +0 -23
  38. data/examples/client_background.rb +0 -14
  39. data/examples/client_data.rb +0 -16
  40. data/examples/client_epoch.rb +0 -23
  41. data/examples/client_exception.rb +0 -19
  42. data/examples/client_prefix.rb +0 -17
  43. data/examples/gearman_environment.sh +0 -25
  44. data/examples/scale_image.rb +0 -31
  45. data/examples/scale_image_worker.rb +0 -34
  46. data/examples/server.rb +0 -15
  47. data/examples/worker_data.rb +0 -16
  48. data/examples/worker_exception.rb +0 -14
  49. data/examples/worker_prefix.rb +0 -25
  50. data/examples/worker_reverse_to_file.rb +0 -18
  51. data/examples/worker_signals.rb +0 -36
  52. data/lib/gearman/taskset.rb +0 -293
  53. data/lib/gearman/util.rb +0 -211
  54. data/spec/util_spec.rb +0 -67
data/README DELETED
@@ -1,9 +0,0 @@
1
- gearman-ruby
2
- ===============
3
-
4
- Library for the Gearman distributed job system
5
-
6
- COPYRIGHT
7
- =========
8
-
9
- Copyright (c) 2009 XING AG. See LICENSE for details.
data/TODO DELETED
@@ -1,8 +0,0 @@
1
- - Failover strategies
2
- - Client:
3
- * If connected for the first time, try to connect to at least one server from the server array
4
- * If already connected to a server, and it goes down, the client should go down as well
5
- - Worker:
6
- * If connected for the first time, try to connect to as many servers as it can.
7
- Loop trough the bad servers, trying to reconnect to them as well.
8
- * If a already connected to a server, and it goes down, wait and try to reconnect again.
@@ -1,4 +0,0 @@
1
- ---
2
- :major: 3
3
- :minor: 0
4
- :patch: 5
@@ -1,39 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require '../lib/gearman'
4
- #Gearman::Util.debug = true
5
-
6
- # Connect to the local server (at the default port 7003)
7
- client = Gearman::Client.new('localhost')
8
- taskset = Gearman::TaskSet.new(client)
9
-
10
- # Get something to echo
11
- puts '[client] Write a basic arithmetic operation:'
12
- input = gets
13
-
14
- operations = input.chomp.scan(/\d+[\+\-\*\/]\d+/).compact
15
- puts "[client] The following operations were found: #{operations.inspect}"
16
-
17
- # Setup a task for operation
18
- operations.each do |op|
19
- # Determining the operation
20
- case op
21
- when /\+/
22
- type, data = 'addition', op.split('+')
23
- when /\-/
24
- type, data = 'subtraction', op.split('-')
25
- when /\*/
26
- type, data = 'multiplication', op.split('*')
27
- when /\//
28
- type, data = 'division', op.split('/')
29
- end
30
-
31
- task = Gearman::Task.new(type, Marshal.dump(data.map {|v| v.to_i}))
32
- task.on_complete {|r| puts "[client] #{type} result is: #{r}" }
33
-
34
- # Sending the task to the server
35
- puts "[client] Sending values: #{data.inspect}, to the '#{type}' worker"
36
- taskset.add_task(task)
37
- taskset.wait(100)
38
- end
39
-
@@ -1,45 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require '../lib/gearman'
4
-
5
- #Gearman::Util.debug = true
6
-
7
- worker = Gearman::Worker.new('localhost')
8
- worker.reconnect_sec = 2
9
-
10
- # Additon ability
11
- worker.add_ability('addition') do |data,job|
12
- values = Marshal.load(data)
13
- puts "[addition_worker] Calculating #{values.inspect}..."
14
- # sleep 5
15
- values.first + values.last
16
- end
17
-
18
- # Subtraction ability
19
- worker.add_ability('subtraction') do |data,job|
20
- values = Marshal.load(data)
21
- puts "[subtraction_worker] Calculating #{values.inspect}..."
22
- # sleep 5
23
- values.first - values.last
24
- end
25
-
26
- # Multiplication worker
27
- worker.add_ability('multiplication') do |data,job|
28
- values = Marshal.load(data)
29
- puts "[multiplication_worker] Calculating #{values.inspect}..."
30
- # sleep 5
31
- values.first * values.last
32
- end
33
-
34
- # Division worker
35
- worker.add_ability('division') do |data,job|
36
- values = Marshal.load(data)
37
- puts "[division_worker] Calculating #{data.inspect}..."
38
- # sleep 5
39
- values.first / values.last
40
- end
41
-
42
- # Running the workers
43
- loop do
44
- worker.work
45
- end
@@ -1,23 +0,0 @@
1
- <?php
2
-
3
- require_once 'Net/Gearman/Client.php';
4
-
5
- $set = new Net_Gearman_Set();
6
-
7
- function result($func, $handle, $result) {
8
- var_dump($func);
9
- var_dump($handle);
10
- var_dump($result);
11
- }
12
-
13
- $task = new Net_Gearman_Task('Sleep', array(
14
- 'seconds' => 20
15
- ));
16
-
17
- $task->attachCallback('result');
18
- $set->addTask($task);
19
-
20
- $client = new Net_Gearman_Client(array('localhost:4730', 'localhost:4731'));
21
- $client->runSet($set);
22
-
23
- ?>
@@ -1,14 +0,0 @@
1
- require 'rubygems'
2
- #require 'gearman'
3
- require '../lib/gearman'
4
-
5
- servers = ['localhost:4730',]
6
-
7
- client = Gearman::Client.new(servers)
8
- taskset = Gearman::TaskSet.new(client)
9
-
10
- task = Gearman::Task.new('sleep', 20, { :background => true })
11
- task.on_complete {|d| puts d }
12
-
13
- taskset.add_task(task)
14
- taskset.wait(100)
@@ -1,16 +0,0 @@
1
- require 'rubygems'
2
- #require 'gearman'
3
- require '../lib/gearman'
4
- Gearman::Util.debug = true
5
-
6
- servers = ['localhost:4730']
7
-
8
- client = Gearman::Client.new(servers)
9
- taskset = Gearman::TaskSet.new(client)
10
-
11
- task = Gearman::Task.new('chunked_transfer')
12
- task.on_data {|d| puts d }
13
- task.on_complete {|d| puts d }
14
-
15
- taskset.add_task(task)
16
- taskset.wait(100)
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Client using Gearman SUBMIT_JOB_EPOCH (currently requires the gearmand branch lp:~jewart/gearmand/scheduled_jobs_support/)
4
-
5
- require 'rubygems'
6
- require '../lib/gearman'
7
-
8
- (1..100).each do
9
- # Connect to the local server (at the default port 4730)
10
- client = Gearman::Client.new('localhost')
11
- taskset = Gearman::TaskSet.new(client)
12
-
13
- data = rand(36**8).to_s(36)
14
- # Set scheduled time to some time in the future
15
- time = Time.now() + rand(10)
16
- puts "Time as seconds: #{time.to_i}"
17
- task = Gearman::Task.new("reverse_string", data)
18
- task.schedule(time)
19
-
20
- # Sending the task to the server
21
- puts "[client] Sending task: #{task.inspect}, to the 'reverse_string' worker"
22
- taskset.add_task(task)
23
- end
@@ -1,19 +0,0 @@
1
- require 'rubygems'
2
- require '../lib/gearman'
3
- Gearman::Util.debug = true
4
-
5
- servers = ['localhost:4730']
6
-
7
- client = Gearman::Client.new(servers)
8
- taskset = Gearman::TaskSet.new(client)
9
-
10
- task = Gearman::Task.new('fail_with_exception', "void")
11
- task.retry_count = 2
12
- task.on_complete {|d| puts d }
13
- task.on_exception {|ex| puts "This should never be called" }
14
- task.on_warning {|warning| puts "WARNING: #{warning}" }
15
- task.on_retry { puts "PRE-RETRY HOOK: retry no. #{task.retries_done}" }
16
- task.on_fail { puts "TASK FAILED, GIVING UP" }
17
-
18
- taskset.add_task(task)
19
- taskset.wait(100)
@@ -1,17 +0,0 @@
1
- require 'rubygems'
2
- #require 'gearman'
3
- require '../lib/gearman'
4
- Gearman::Util.debug = true
5
-
6
- servers = ['localhost:4730', 'localhost:4731']
7
-
8
- ability_name_with_prefix = Gearman::Util.ability_name_with_prefix("test","sleep")
9
-
10
- client = Gearman::Client.new(servers)
11
- taskset = Gearman::TaskSet.new(client)
12
-
13
- task = Gearman::Task.new(ability_name_with_prefix, 20)
14
- task.on_complete {|d| puts d }
15
-
16
- taskset.add_task(task)
17
- taskset.wait(100)
@@ -1,25 +0,0 @@
1
- #!/bin/bash
2
-
3
- # Start Gearmand
4
- echo ' + Starting Gearmand'
5
- gearmand --daemon --pidfile=/tmp/gearmand.pid
6
-
7
- # Start the client and the worker(s)
8
- echo ' + Starting calculus_worker.rb'
9
- ruby calculus_worker.rb &
10
-
11
- sleep 3
12
-
13
- echo ' + Starting calculus_client.rb'
14
- ruby calculus_client.rb
15
-
16
- echo ' +++ Example finished +++ '
17
-
18
- # Stop Gearmand
19
- echo ' - Stopping Gearmand'
20
- kill -9 `cat /tmp/gearmand.pid`
21
-
22
- # Stop the workers
23
- echo ' - Stopping calculus_worker.rb'
24
- kill -9 `ps ax|grep calculus_worker|grep ruby|awk -F' ' '{print $1}'`
25
-
@@ -1,31 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- $: << '../lib'
4
- require 'gearman'
5
- require 'optparse'
6
-
7
- servers = 'localhost:4730'
8
- format = 'PNG'
9
- width, height = 100, 100
10
-
11
- opts = OptionParser.new
12
- opts.banner = "Usage: #{$0} [options] <input> <output>"
13
- opts.on('-f FORMAT', '--format', 'Scaled image format') { format }
14
- opts.on('-h HEIGHT', '--height', 'Scaled image height') { height }
15
- opts.on('-s SERVERS', '--servers',
16
- 'Servers, comma-separated host:port') { servers }
17
- opts.on('-w WIDTH', '--width', 'Scaled image width') { width }
18
- opts.parse!
19
-
20
- if ARGV.size != 2
21
- $stderr.puts opts.banner
22
- exit 1
23
- end
24
-
25
- client = Gearman::Client.new(servers.split(','), 'example')
26
- taskset = Gearman::TaskSet.new(client)
27
- arg = [width, height, format, File.read(ARGV[0])].join("\0")
28
- task = Gearman::Task.new('scale_image', arg)
29
- task.on_complete {|d| File.new(ARGV[1],'w').write(d) }
30
- taskset.add_task(task)
31
- taskset.wait(10)
@@ -1,34 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- $: << '../lib'
4
- require 'gearman'
5
- require 'optparse'
6
- require 'RMagick'
7
-
8
- Gearman::Util.debug = true
9
- servers = 'localhost:7003'
10
-
11
- opts = OptionParser.new
12
- opts.banner = "Usage: #{$0} [options]"
13
- opts.on('-s SERVERS', '--servers',
14
- 'Job servers, comma-separated host:port') { servers }
15
- opts.parse!
16
-
17
- worker = Gearman::Worker.new(servers.split(','), 'example')
18
-
19
- worker.add_ability('scale_image') do |data,job|
20
- width, height, format, data = data.split("\0", 4)
21
- width = width.to_f
22
- height = height.to_f
23
- image = Magick::Image.from_blob(data)[0]
24
- orig_ratio = image.columns.to_f / image.rows
25
- new_ratio = width / height
26
- w = new_ratio < orig_ratio ? width : orig_ratio / new_ratio * width
27
- h = new_ratio > orig_ratio ? height : new_ratio / orig_ratio * height
28
- puts "Got #{image.inspect}; resizing to #{w}x#{h} #{format}"
29
- image.resize!(w, h)
30
- image.format = format
31
- image.to_blob
32
- end
33
-
34
- loop { worker.work }
@@ -1,15 +0,0 @@
1
- require 'rubygems'
2
- # require 'gearman'
3
- # require 'gearman/server'
4
- require '../lib/gearman'
5
- require '../lib/gearman/server'
6
- require 'pp'
7
-
8
- Gearman::Util.debug = true
9
- w = Gearman::Server.new('localhost:4730')
10
-
11
- loop {
12
- pp "Status: ", w.status
13
- pp "Workers: ", w.workers
14
- sleep 5
15
- }
@@ -1,16 +0,0 @@
1
- require 'rubygems'
2
- require '../lib/gearman'
3
-
4
- Gearman::Util.debug = true
5
-
6
- servers = ['localhost:4730']
7
- worker = Gearman::Worker.new(servers)
8
-
9
- worker.add_ability('chunked_transfer') do |data, job|
10
- 5.times do |i|
11
- sleep 1
12
- job.send_data("CHUNK #{i}")
13
- end
14
- "EOD"
15
- end
16
- loop { worker.work }
@@ -1,14 +0,0 @@
1
- require 'rubygems'
2
- require '../lib/gearman'
3
-
4
- Gearman::Util.debug = true
5
-
6
- servers = ['localhost:4730']
7
- w = Gearman::Worker.new(servers)
8
-
9
- # Add a handler for a "sleep" function that takes a single argument, the
10
- # number of seconds to sleep before reporting success.
11
- w.add_ability('fail_with_exception') do |data,job|
12
- raise Exception.new("Exception in worker (args: #{data.inspect})")
13
- end
14
- loop { w.work }
@@ -1,25 +0,0 @@
1
- require 'rubygems'
2
- #require 'gearman'
3
- require '../lib/gearman'
4
-
5
- Gearman::Util.debug = true
6
-
7
- servers = ['localhost:4730', 'localhost:4731']
8
- w = Gearman::Worker.new(servers)
9
-
10
- ability_name_with_prefix = Gearman::Util.ability_name_with_prefix("test","sleep")
11
-
12
- # Add a handler for a "sleep" function that takes a single argument, the
13
- # number of seconds to sleep before reporting success.
14
- w.add_ability(ability_name_with_prefix) do |data,job|
15
- seconds = data
16
- (1..seconds.to_i).each do |i|
17
- sleep 1
18
- print i
19
- # Report our progress to the job server every second.
20
- job.report_status(i, seconds)
21
- end
22
- # Report success.
23
- true
24
- end
25
- loop { w.work }
@@ -1,18 +0,0 @@
1
- require 'rubygems'
2
- #require 'gearman'
3
- require '../lib/gearman'
4
-
5
- servers = ['localhost:4730']
6
- w = Gearman::Worker.new(servers)
7
-
8
- # Add a handler for a "sleep" function that takes a single argument, the
9
- # number of seconds to sleep before reporting success.
10
- w.add_ability('reverse_to_file') do |data,job|
11
- puts "Data: #{data.inspect}"
12
- word, file = data.split("\0")
13
- puts "Word: #{word}"
14
- puts "File: #{file}"
15
- # Report success.
16
- true
17
- end
18
- loop { w.work }
@@ -1,36 +0,0 @@
1
- require 'rubygems'
2
- #require 'gearman'
3
- require '../lib/gearman'
4
-
5
- Gearman::Util.debug = true
6
-
7
- servers = ['localhost:4730', 'localhost:4731']
8
- w = Gearman::Worker.new(servers)
9
-
10
- # Add a handler for a "sleep" function that takes a single argument, the
11
- # number of seconds to sleep before reporting success.
12
- w.add_ability('sleep') do |data,job|
13
- seconds = data
14
- (1..seconds.to_i).each do |i|
15
- sleep 1
16
- Gearman::Util.logger.info i
17
- # Report our progress to the job server every second.
18
- job.report_status(i, seconds)
19
- end
20
- # Report success.
21
- true
22
- end
23
-
24
- # Trap signals while is working
25
- %w(HUP USR1 ALRM TERM).each do |signal|
26
- trap(signal) do
27
- puts "Received signal #{signal} - setting worker_enabled to false. Worker status is [#{w.status}]"
28
- w.worker_enabled = false
29
- if w.status == :waiting
30
- trap(signal, "DEFAULT")
31
- Process.kill( signal, $$ )
32
- end
33
- end
34
- end
35
-
36
- loop { w.work or break }