gearman_admin_client 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c85a42f59821a35635895328155ce084e2919774
4
- data.tar.gz: 4b7f6df73a5a470499a15587c154c2f77a88330a
3
+ metadata.gz: 74da28ff05fbcdf32efcce54d642209f8ac58b74
4
+ data.tar.gz: 2156744bcbcf9ef0b2699ca2cf6bc1c29f55e99a
5
5
  SHA512:
6
- metadata.gz: 62060e5e5af46ae99751b8feb490a58730a6ccd944f9357caf81df72c37ddaabe8d74c99ee115d6d39eb0673fc1cd47c73b488653c097b8188b4e355d8102fb0
7
- data.tar.gz: 21653ef53e5e3b1820ed7a34029d8f89065d846922551cbf45865acc88cd55224331b322472cdc872ded8441dfc8a92dc2797b945edf10e86133c0c7ae2175b9
6
+ metadata.gz: 8ff4567818c90bbb8fdcf1dcd08480f233be00384523ed04c1959c18a38f8a89ac2f12ca9c71db6b473ae717ffd06067beb35f0cd009a3acb389de250ab60fbe
7
+ data.tar.gz: ee0f826c914e6b5f2cf5396da2c934421456b66a8e45f642ad18617e41c34331d11b579bc770fd197c3e807573ad09048f12cd801b595a51e576ee15a54cdaa6
@@ -1,24 +1,30 @@
1
+ require 'celluloid/io'
1
2
  require 'forwardable'
2
3
 
3
4
  class GearmanAdminClient
4
5
  class Connection
6
+ include Celluloid::IO
5
7
  extend Forwardable
6
8
 
7
9
  def_delegators :io, :close, :closed?, :eof?
8
10
 
11
+ finalizer :disconnect
12
+
9
13
  attr_reader :io
10
14
 
11
- def initialize(io)
12
- @io = io
15
+ def initialize(address)
16
+ @address = address
13
17
  end
14
18
 
15
19
  def write(command)
16
- IO::select([], [io])
20
+ connect if disconnected?
21
+
17
22
  io.puts(command)
18
23
  end
19
24
 
20
25
  def read
21
- IO::select([io])
26
+ connect if disconnected?
27
+
22
28
  io.gets
23
29
  end
24
30
 
@@ -33,5 +39,22 @@ class GearmanAdminClient
33
39
  output
34
40
  end
35
41
 
42
+ def connect
43
+ host, port = @address.split(':')
44
+
45
+ @io = TCPSocket.new(host, port)
46
+ end
47
+
48
+ def disconnect
49
+ if @io
50
+ @io.close unless @io.closed?
51
+ @io = nil
52
+ end
53
+ end
54
+
55
+ def disconnected?
56
+ @io.nil?
57
+ end
58
+
36
59
  end
37
60
  end
@@ -1,3 +1,3 @@
1
1
  class GearmanAdminClient
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,130 +1,119 @@
1
- require 'socket'
1
+ require 'celluloid'
2
2
 
3
3
  require 'gearman_admin_client/worker'
4
4
  require 'gearman_admin_client/registered_function'
5
5
  require 'gearman_admin_client/connection'
6
6
 
7
7
  class GearmanAdminClient
8
+ include Celluloid
8
9
 
9
- DISCONNECTED = :DISCONNECTED unless defined? DISCONNECTED
10
+ trap_exit :disconnect
11
+ finalizer :disconnect
10
12
 
11
- class BadAddress < RuntimeError ; end
12
-
13
- attr_reader :address
13
+ attr_reader :address, :connection
14
14
 
15
15
  def initialize(address)
16
16
  @address = address
17
- @connection = DISCONNECTED
17
+ @connect = Connection.method(:new_link)
18
+ build_connection
18
19
  end
19
20
 
20
21
  def workers
21
- connect! do |connection|
22
- connection.write('workers')
23
- output = connection.drain.split("\n")
24
-
25
- workers = output.map do |line|
26
- if line.end_with?(':')
27
- function_names = []
28
- remainder = line
29
- else
30
- segments = line.split(':')
31
-
32
- function_names = segments.pop.strip.split(' ')
33
-
34
- remainder = segments.join(':')
35
- end
36
-
37
- fd, ip_address, client_id = remainder.split(' ').map(&:strip)
38
-
39
- Worker.new(
40
- :file_descriptor => fd,
41
- :ip_address => ip_address,
42
- :client_id => client_id,
43
- :function_names => function_names
44
- )
22
+ connection.write('workers')
23
+ output = connection.drain.split("\n")
24
+
25
+ workers = output.map do |line|
26
+ if line.end_with?(':')
27
+ function_names = []
28
+ remainder = line
29
+ else
30
+ segments = line.split(':')
31
+
32
+ function_names = segments.pop.strip.split(' ')
33
+
34
+ remainder = segments.join(':')
45
35
  end
36
+
37
+ fd, ip_address, client_id = remainder.split(' ').map(&:strip)
38
+
39
+ Worker.new(
40
+ :file_descriptor => fd,
41
+ :ip_address => ip_address,
42
+ :client_id => client_id,
43
+ :function_names => function_names
44
+ )
46
45
  end
47
46
  end
48
47
 
49
48
  def status
50
- connect! do |connection|
51
- connection.write('status')
52
- output = connection.drain.split("\n")
53
-
54
- output.map do |line|
55
- function_name, total, running, workers = line.split("\t")
56
-
57
- RegisteredFunction.new(
58
- :name => function_name,
59
- :jobs_in_queue => total,
60
- :running_jobs => running,
61
- :available_workers => workers
62
- )
63
- end
49
+ connection.write('status')
50
+ output = connection.drain.split("\n")
51
+
52
+ output.map do |line|
53
+ function_name, total, running, workers = line.split("\t")
54
+
55
+ RegisteredFunction.new(
56
+ :name => function_name,
57
+ :jobs_in_queue => total,
58
+ :running_jobs => running,
59
+ :available_workers => workers
60
+ )
64
61
  end
65
62
  end
66
63
 
67
64
  def server_version
68
- connect! do |connection|
69
- connection.write('version')
70
- connection.read.strip
71
- end
65
+ connection.write('version')
66
+ connection.read.strip
72
67
  end
73
68
 
74
69
  def shutdown(options = {})
75
- connect! do |connection|
76
- command = ['shutdown']
70
+ command = ['shutdown']
77
71
 
78
- if options.fetch(:graceful, false)
79
- command << 'graceful'
80
- end
72
+ if options.fetch(:graceful, false)
73
+ command << 'graceful'
74
+ end
81
75
 
82
- connection.write(command.join(' '))
83
- connection.read.strip
76
+ connection.write(command.join(' '))
77
+ connection.read.strip
84
78
 
85
- connection.eof? && disconnect!
86
- end
79
+ connection.eof? && disconnect
87
80
 
88
81
  true
89
82
  end
90
83
 
91
84
  def max_queue_size(function_name, queue_size = nil)
92
- connect! do |connection|
93
- command = ['maxqueue', function_name, queue_size].compact
85
+ command = ['maxqueue', function_name, queue_size].compact
94
86
 
95
- connection.write(command.join(' '))
96
- connection.read.strip
97
- end
87
+ connection.write(command.join(' '))
88
+ connection.read.strip
98
89
  end
99
90
 
100
- def disconnected?
101
- DISCONNECTED == @connection
102
- end
91
+ def disconnect(actor = nil, reason = nil)
92
+ if @connection && @connection.alive?
93
+ @connection.terminate
94
+ end
103
95
 
104
- def disconnect!
105
- @connection.close
106
- @connection = DISCONNECTED
96
+ if reason
97
+ build_connection
98
+ end
107
99
  end
108
100
 
109
- private
110
-
111
- def connect!(&and_then)
112
- if disconnected?
113
- just_open_a_socket do |socket|
114
- @connection = Connection.new(socket)
115
- end
101
+ def disconnected?
102
+ if @connection
103
+ not @connection.alive?
116
104
  end
105
+ end
117
106
 
118
- and_then.call(@connection)
107
+ def build_connection
108
+ @connection = @connect.call(@address)
119
109
  end
120
110
 
121
- def just_open_a_socket
122
- host, port = address.split(':')
111
+ def connect(&and_then)
112
+ disconnect
113
+ build_connection
123
114
 
124
- if host && port
125
- yield TCPSocket.new(host, port)
126
- else
127
- raise BadAddress, "expected address to look like HOST:PORT"
115
+ if and_then
116
+ and_then.call(@connection)
128
117
  end
129
118
  end
130
119
 
@@ -163,7 +163,7 @@ describe GearmanAdminClient do
163
163
 
164
164
  expect(client).to_not be_disconnected
165
165
 
166
- client.disconnect!
166
+ client.disconnect
167
167
 
168
168
  expect(client).to be_disconnected
169
169
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gearman_admin_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Cobb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-09 00:00:00.000000000 Z
11
+ date: 2013-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.0.0.beta0
69
+ - !ruby/object:Gem::Dependency
70
+ name: celluloid
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: celluloid-io
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: A Ruby wrapper around the Gearman admin protocol
70
98
  email:
71
99
  - bcobb@uwalumni.com