English 中文(简体)
Any way to access Gearman administration?
原标题:
  • 时间:2010-05-02 04:18:07
  •  标签:
  • php
  • gearman

I want to be able to query a gearman server to determine how many instances of a worker I have running (basically I want to make sure that RunTaskA is available and RunTaskB is available if there are no workers handling those tasks, I want to be able to send an alert out.

Is there any way to do this?

Also: Mad props if you know of a PHP way to query the gearman server.

Edit: I know about the PHP gearman extension that is available natively, but I am not looking for a task submission extension, I need something that allows me to query the gearman server and see how many workers are serving a specific task.

最佳回答
class Waps_Gearman_Server {

    /**
     * @var string
     */
    protected $host = "127.0.0.1";
    /**
     * @var int
     */
    protected $port = 4730;

    /**
     * @param string $host
     * @param int $port
     */
    public function __construct($host=null,$port=null){
        if( !is_null($host) ){
            $this->host = $host;
        }
        if( !is_null($port) ){
            $this->port = $port;
        }
    }

    /**
     * @return array | null
     */
    public function getStatus(){
        $status = null;
        $handle = fsockopen($this->host,$this->port,$errorNumber,$errorString,30);
        if($handle!=null){
            fwrite($handle,"status
");
            while (!feof($handle)) {
                $line = fgets($handle, 4096);
                if( $line==".
"){
                    break;
                }
                if( preg_match("~^(.*)[ 	](d+)[ 	](d+)[ 	](d+)~",$line,$matches) ){
                    $function = $matches[1];
                    $status[ operations ][$function] = array(
                         function  => $function,
                         total  => $matches[2],
                         running  => $matches[3],
                         connectedWorkers  => $matches[4],
                    );
                }
            }
            fwrite($handle,"workers
");
            while (!feof($handle)) {
                $line = fgets($handle, 4096);
                if( $line==".
"){
                    break;
                }
                // FD IP-ADDRESS CLIENT-ID : FUNCTION
                if( preg_match("~^(d+)[ 	](.*?)[ 	](.*?) : ?(.*)~",$line,$matches) ){
                    $fd = $matches[1];
                    $status[ connections ][$fd] = array(
                         fd  => $fd,
                         ip  => $matches[2],
                         id  => $matches[3],
                         function  => $matches[4],
                    );
                }
            }
            fclose($handle);
        }

        return $status;
    }

}
问题回答

For quick checking, I use this bash one-liner:

# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730

This opens a connection to a gearman instance running on localhost, and sends the "status" query. This contains the name and number of jobs on that instance. The information can then be processed with grep/awk/wc etc. for reporting and alerting.

I also do the same with the "workers" query which shows all connected workers.

# (echo workers ; sleep 0.1) | netcat 127.0.0.1 4730

The sleep is to keep the connection open long enough for the reply.

The full list of administrative commands, and what the output means is at http://gearman.org/index.php?id=protocol Just search for "Administrative Protocol"

To expand on d5ve s answer, since netcat will sit and wait on the socket, you can add a -w parameter with a maximum number of seconds to run. So if you re querying localhost:

# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730 -w 1

... otherwise you never get back to a command prompt.

As far as i know there are no such extension in gearman, Managing and monitoring worker script is your responsibility you can try one of these for this purpose -

Supervisord is a python appliation for running application in background and monitoring them.

OR you can use brian moon s gearman manager

I think you need this http://github.com/liorbk/php

Gearman has a php extension. Have you looked into that yet?

PHP Link

Stumbled on it today, haven t tested it myself but it looks promising.

https://github.com/yugene/Gearman-Monitor

In Python you could do the following:

import gearman

admin_client = gearman.GearmanAdminClient([ 127.0.0.1:4730 ,])
status = admin_client.get_status()
for w in status:
   if w["task"] == "YOUR_TASK_NAME":
      print(w)

Note: you have to install package named "gearman" using pip or easy_install to avoid any exceptions running the above code.

Also, Check the following admin clients that is simplifying administering gearman in general.

When everything else fails, you can use the gearadmin tool found in the gearman-tools package in Ubuntu by calling exec() to execute it in the new process. Here is a reference to its output format.

This assumes PHP and Gearman are running on the same server.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签