为什么不是处理工人?

时间:2015-07-21 18:33:29

标签: ruby osx-yosemite gearman

我在Mac OSX(优胜美地)

使用自制软件安装。

➜  ~  brew list gearmand
/usr/local/Cellar/gearman/1.1.12/bin/gearadmin
/usr/local/Cellar/gearman/1.1.12/bin/gearman
/usr/local/Cellar/gearman/1.1.12/homebrew.mxcl.gearman.plist
/usr/local/Cellar/gearman/1.1.12/include/libgearman/gearman.h
/usr/local/Cellar/gearman/1.1.12/include/libgearman-1.0/ (39 files)
/usr/local/Cellar/gearman/1.1.12/lib/libgearman.8.dylib
/usr/local/Cellar/gearman/1.1.12/lib/pkgconfig/gearmand.pc
/usr/local/Cellar/gearman/1.1.12/lib/ (2 other files)
/usr/local/Cellar/gearman/1.1.12/sbin/gearmand
/usr/local/Cellar/gearman/1.1.12/share/man/ (156 files)

将/ usr / local / sbin添加到我的路径(.bash-path),以便使用gearmand。

➜  ~  echo $PATH
/Users/davidvezzani/.rvm/gems/ruby-1.9.3-p551/bin:/Users/davidvezzani/.rvm/gems/ruby-1.9.3-p551@global/bin:/Users/davidvezzani/.rvm/rubies/ruby-1.9.3-p551/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Users/davidvezzani/.rvm/bin:/Users/davidvezzani/bin

检查是否在路径上。它就是。

➜  ~  which gearmand
/usr/local/sbin/gearmand

尝试启动gearmand。我期待看到一些日志声明,表明服务器已经启动,并且它正在等待处理作业。什么都没有。

➜  ~  gearmand --verbose DEBUG

它在运行吗?!

501 17847 17693   0 11:24AM ttys017    0:00.01 gearmand --verbose DEBUG

是。但我不知道它在做什么。让我们来看看gearman-ruby的一个例子。

rvm use 2.2.0
gem install gearman-ruby
mvim do_reverse.rb

提供以下内容

#  reverse_do.rb
require 'rubygems'
require 'gearman'

# Add our servers

servers = ['127.0.0.1:4730']

# Initialize a new client

client = Gearman::Client.new(servers)

puts "Sending job...";
task = Gearman::Task.new('reverse', 'Hello World!', {})
result = client.do_task task

puts "Result: " + result

运行示例客户端。

➜  work3  ruby reverse_do.rb
D, [2015-07-21T14:00:40.717160 #21521] DEBUG -- : Performing health check for 127.0.0.1:4730 (connected: false)
D, [2015-07-21T14:00:40.717248 #21521] DEBUG -- : Attempt #0 to connect to 127.0.0.1:4730
D, [2015-07-21T14:00:40.718060 #21521] DEBUG -- : Health check response for 127.0.0.1:4730 (connected: true) is [:echo_res, "ping"]
Sending job...
D, [2015-07-21T14:00:40.719181 #21521] DEBUG -- : Available job servers: [#<Gearman::Connection:0x007f960e049b78 @hostname="127.0.0.1", @port=4730, @real_socket=#<TCPSocket:fd 7>>]
D, [2015-07-21T14:00:40.719215 #21521] DEBUG -- : Using 127.0.0.1:4730 (connected: true) to submit job
D, [2015-07-21T14:00:40.719473 #21521] DEBUG -- : Got job_created from 127.0.0.1:4730 (connected: true)

客户似乎在运行,但我应该看到&#34;结果&#34;在某些时候,但我没有看到。我错过了什么?

1 个答案:

答案 0 :(得分:0)

来自Gearman documentation

  

本文档假定您对作业服务器,客户和工作人员的角色有一个高层次的了解。

three players in the gearman architecture

由于这里涉及三个玩家,在运行示例时,至少有一个Gearman作业服务器(此示例显示正在运行2),

(从终端执行:)

gearmand --verbose DEBUG -p 4730
gearmand --verbose DEBUG -p 4731

......启动你的工人,

#  worker.rb
require 'rubygems'
require 'logger'
require 'gearman'

servers = ['localhost:4730', 'localhost:4731']

w = Gearman::Worker.new(servers)
logger = Logger.new(STDOUT)

# Add a handler for a "sleep" function that takes a single argument, the
# number of seconds to sleep before reporting success.
w.add_ability("sleep") do |data,job|
 seconds = 10
 logger.info "Sleeping for #{seconds} seconds"
 (1..seconds.to_i).each do |i|
   sleep 1
   # Report our progress to the job server every second.
   job.report_status(i, seconds)
 end
 # Report success.
 true
end

w.add_ability("blah") do |data,job|
 logger.info "Blah, blah, blah..."
 # Report our progress to the job server every second.
 job.report_status(1, 1)
 # Report success.
 true
end

loop { w.work }

(从终端执行:)

ruby worker.rb

...让客户端在堆栈上推送工作。

#  reverse_do.rb
require 'gearman'

servers = ['localhost:4730', 'localhost:4731']

client = Gearman::Client.new(servers)
taskset = Gearman::TaskSet.new(client)

task = Gearman::Task.new('sleep', 20)
task.on_complete {|d| puts "Finished with client: #{d}" }

task2 = Gearman::Task.new('blah')
task2.on_complete {|d| puts "Finished with client for blah: #{d}" }

taskset.add_task(task)
taskset.add_task(task2)
taskset.wait(100)

(从终端执行:)

ruby reverse_do.rb

服务器结果。

即使通过我将详细程度变为DEBUG,我也没有看到终端登录。

工人结果。

➜  work3  ruby worker.rb
D, [2015-07-21T15:04:34.868178 #22866] DEBUG -- : Performing health check for localhost:4730 (connected: false)
D, [2015-07-21T15:04:34.868258 #22866] DEBUG -- : Attempt #0 to connect to localhost:4730
D, [2015-07-21T15:04:34.869435 #22866] DEBUG -- : Health check response for localhost:4730 (connected: true) is [:echo_res, "ping"]
D, [2015-07-21T15:04:34.869471 #22866] DEBUG -- : Performing health check for localhost:4731 (connected: false)
D, [2015-07-21T15:04:34.869493 #22866] DEBUG -- : Attempt #0 to connect to localhost:4731
D, [2015-07-21T15:04:34.869962 #22866] DEBUG -- : Health check response for localhost:4731 (connected: true) is [:echo_res, "ping"]
D, [2015-07-21T15:04:34.870030 #22866] DEBUG -- : Announced ability sleep
D, [2015-07-21T15:04:34.870107 #22866] DEBUG -- : Announced ability sleep
D, [2015-07-21T15:04:34.870160 #22866] DEBUG -- : Announced ability blah
D, [2015-07-21T15:04:34.870193 #22866] DEBUG -- : Announced ability blah
D, [2015-07-21T15:04:34.870238 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4730 (connected: true)
I, [2015-07-21T15:04:34.870417 #22866]  INFO -- : Got NO_JOB from localhost:4730 (connected: true)
D, [2015-07-21T15:04:34.870444 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4731 (connected: true)
I, [2015-07-21T15:04:34.870620 #22866]  INFO -- : Got NO_JOB from localhost:4731 (connected: true)
I, [2015-07-21T15:04:34.870657 #22866]  INFO -- : Sending PRE_SLEEP and going to sleep for 30 second(s)
D, [2015-07-21T15:04:34.870764 #22866] DEBUG -- : Polling on 2 available server(s) with a 30 second timeout
D, [2015-07-21T15:04:53.263702 #22866] DEBUG -- : Received NOOP while sleeping... waking up!
D, [2015-07-21T15:04:53.263801 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4730 (connected: true)
I, [2015-07-21T15:04:53.264074 #22866]  INFO -- : Got NO_JOB from localhost:4730 (connected: true)
D, [2015-07-21T15:04:53.264101 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4731 (connected: true)
I, [2015-07-21T15:04:53.264367 #22866]  INFO -- : Got JOB_ASSIGN with handle H:Davids-iMac.local:6 and 0 byte(s) from localhost:4731 (connected: true)
I, [2015-07-21T15:04:53.264399 #22866]  INFO -- : Blah, blah, blah...
D, [2015-07-21T15:04:53.264443 #22866] DEBUG -- : Sending WORK_COMPLETE for H:Davids-iMac.local:6 with 4 byte(s) to localhost:4731 (connected: true)
D, [2015-07-21T15:04:53.264491 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4731 (connected: true)
I, [2015-07-21T15:04:53.264710 #22866]  INFO -- : Got NO_JOB from localhost:4731 (connected: true)
I, [2015-07-21T15:04:53.264735 #22866]  INFO -- : Sending PRE_SLEEP and going to sleep for 30 second(s)
D, [2015-07-21T15:04:53.264795 #22866] DEBUG -- : Polling on 2 available server(s) with a 30 second timeout
D, [2015-07-21T15:04:53.265126 #22866] DEBUG -- : Received NOOP while sleeping... waking up!
D, [2015-07-21T15:05:03.269974 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4730 (connected: true)
I, [2015-07-21T15:05:03.270372 #22866]  INFO -- : Got JOB_ASSIGN with handle H:Davids-iMac.local:4 and 2 byte(s) from localhost:4730 (connected: true)
I, [2015-07-21T15:05:03.270405 #22866]  INFO -- : Sleeping for 10 seconds
D, [2015-07-21T15:05:13.301296 #22866] DEBUG -- : Sending WORK_COMPLETE for H:Davids-iMac.local:4 with 4 byte(s) to localhost:4730 (connected: true)
D, [2015-07-21T15:05:13.301415 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4730 (connected: true)
I, [2015-07-21T15:05:13.301655 #22866]  INFO -- : Got NO_JOB from localhost:4730 (connected: true)
D, [2015-07-21T15:05:13.301696 #22866] DEBUG -- : Sending GRAB_JOB to localhost:4731 (connected: true)
I, [2015-07-21T15:05:13.301920 #22866]  INFO -- : Got NO_JOB from localhost:4731 (connected: true)
I, [2015-07-21T15:05:13.301951 #22866]  INFO -- : Sending PRE_SLEEP and going to sleep for 30 second(s)
D, [2015-07-21T15:05:13.302023 #22866] DEBUG -- : Polling on 2 available server(s) with a 30 second timeout

客户结果。

➜  work3  ruby reverse_do.rb
D, [2015-07-21T14:55:09.896887 #22800] DEBUG -- : Performing health check for localhost:4730 (connected: false)
D, [2015-07-21T14:55:09.896973 #22800] DEBUG -- : Attempt #0 to connect to localhost:4730
D, [2015-07-21T14:55:09.898392 #22800] DEBUG -- : Health check response for localhost:4730 (connected: true) is [:echo_res, "ping"]
D, [2015-07-21T14:55:09.898428 #22800] DEBUG -- : Performing health check for localhost:4731 (connected: false)
D, [2015-07-21T14:55:09.898451 #22800] DEBUG -- : Attempt #0 to connect to localhost:4731
D, [2015-07-21T14:55:09.898963 #22800] DEBUG -- : Health check response for localhost:4731 (connected: true) is [:echo_res, "ping"]
D, [2015-07-21T14:55:09.899153 #22800] DEBUG -- : Available job servers: [#<Gearman::Connection:0x007fb6a309fd88 @hostname="localhost", @port=4730, @real_socket=#<TCPSocket:fd 9>>, #<Gearman::Connection:0x007fb6a309e258 @hostname="localhost", @port=4731, @real_socket=#<TCPSocket:fd 10>>]
D, [2015-07-21T14:55:09.899182 #22800] DEBUG -- : Using localhost:4731 (connected: true) to submit job
D, [2015-07-21T14:55:09.899464 #22800] DEBUG -- : Got job_created from localhost:4731 (connected: true)
D, [2015-07-21T14:55:19.903692 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:5: 1 / 1
D, [2015-07-21T14:55:19.903789 #22800] DEBUG -- : Received WORK_COMPLETE for H:Davids-iMac.local:5
Finished with client for blah: true
D, [2015-07-21T14:55:19.903933 #22800] DEBUG -- : Available job servers: [#<Gearman::Connection:0x007fb6a309fd88 @hostname="localhost", @port=4730, @real_socket=#<TCPSocket:fd 9>>, #<Gearman::Connection:0x007fb6a309e258 @hostname="localhost", @port=4731, @real_socket=#<TCPSocket:fd 10>>]
D, [2015-07-21T14:55:19.903993 #22800] DEBUG -- : Using localhost:4730 (connected: true) to submit job
D, [2015-07-21T14:55:19.904225 #22800] DEBUG -- : Got job_created from localhost:4730 (connected: true)
D, [2015-07-21T14:55:30.908445 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 1 / 10
D, [2015-07-21T14:55:31.911152 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 2 / 10
D, [2015-07-21T14:55:32.914036 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 3 / 10
D, [2015-07-21T14:55:33.916782 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 4 / 10
D, [2015-07-21T14:55:34.919513 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 5 / 10
D, [2015-07-21T14:55:35.922062 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 6 / 10
D, [2015-07-21T14:55:36.926507 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 7 / 10
D, [2015-07-21T14:55:37.930245 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 8 / 10
D, [2015-07-21T14:55:38.930387 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 9 / 10
D, [2015-07-21T14:55:39.933860 #22800] DEBUG -- : Received WORK_STATUS for H:Davids-iMac.local:3: 10 / 10
D, [2015-07-21T14:55:39.933983 #22800] DEBUG -- : Received WORK_COMPLETE for H:Davids-iMac.local:3
Finished with client: true

理解高级概念非常重要,因此可以理解在启动gearmand服务器之后应该执行脚本的顺序,以便可以成功执行工作示例。