Gearman: Installation

Avez vous lu la présentation de Gearman ?

Installation

Gearmand est un démon en C. Il est disponible dans une version un peu vieillotte dans les dépôts Ubuntu, mais une version un peu plus récente peut être installée depuis ceux de Gearman. A l’heure ou j’écris l’article, la version des dépôts Ubuntu est la 0.27. Celle des dépôts Gearman est la 0.34. Pour utiliser la version de chez Ubuntu, sautez juste la première commande.

$ sudo add-apt-repository ppa:gearman-developers/ppa
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install gearman-job-server

Installera gearmand et memcached, ce dernier pourra être désinstallé par la suite. Son script de lancement est « /etc/init.d/gearman-job-server ». La configuration est dans « /etc/default/gearman-job-server ».

Par défaut, le démon est lancé avec les options suivantes :

  • Fichier de PID (« /var/run/gearman/gearman.pid », dans l’init script)
  • Nom d’utilisateur (« gearman », dans l’init script)
  • Mode démon (dans l’init script)
  • Fichier log dans « /var/log/gearman-job-server/gearman.log » (dans l’init script)
  • Restriction d’accès à 127.0.0.1 (dans les defaults)

Voici le guide d’utilisation de Gearmand 0.34 :

$ gearmand --help
Allowed options:

Allowed options:

General options:
  -b [ --backlog ] arg (=32)            Number of backlog connections for 
                                        listen.
  -d [ --daemon ]                       Daemon, detach and run in the 
                                        background.
  -f [ --file-descriptors ] arg         Number of file descriptors to allow for
                                        the process (total connections will be 
                                        slightly less). Default is max allowed 
                                        for user.
  -h [ --help ]                         Print this help menu.
  -j [ --job-retries ] arg (=0)         Number of attempts to run the job 
                                        before the job server removes it. This 
                                        is helpful to ensure a bad job does not
                                        crash all available workers. Default is
                                        no limit.
  -l [ --log-file ] arg (=/var/log/gearmand.log)
                                        Log file to write errors and 
                                        information to. If the log-file 
                                        paramater is specified as 'stderr', 
                                        then output will go to stderr. If 
                                        'none', then no logfile will be 
                                        generated.
  -L [ --listen ] arg                   Address the server should listen on. 
                                        Default is INADDR_ANY.
  -P [ --pid-file ] arg (=/var/gearmand.pid)
                                        File to write process ID out to.
  -r [ --protocol ] arg                 Load protocol module.
  -R [ --round-robin ]                  Assign work in round-robin order per 
                                        worker connection. The default is to 
                                        assign work in the order of functions 
                                        added by the worker.
  -q [ --queue-type ] arg (=builtin)    Persistent queue type to use.
  --config-file arg (=/etc/gearmand.conf)
                                        Can be specified with '@name', too
  --syslog                              Use syslog.
  -t [ --threads ] arg (=4)             Number of I/O threads to use. 
                                        Default=4.
  -u [ --user ] arg                     Switch to given user after startup.
  --verbose arg (=ERROR)                Set verbose level (FATAL, ALERT, 
                                        CRITICAL, ERROR, WARNING, NOTICE, INFO,
                                        DEBUG).
  -V [ --version ]                      Display the version of gearmand and 
                                        exit.
  -w [ --worker-wakeup ] arg (=0)       Number of workers to wakeup for each 
                                        job received. The default is to wakeup 
                                        all available workers.

HTTP:
  --http-port arg (=8080) Port to listen on.

Gear:
  -p [ --port ] arg (=4730) Port the server should listen on.

builtin:

libsqlite3:
  --libsqlite3-db arg                   Database file to use.
  --libsqlite3-table arg (=gearman_queue)
                                        Table to use.

Postgres:
  --libpq-conninfo arg       PostgreSQL connection information string.
  --libpq-table arg (=queue) Table to use.

libtokyocabinet:
  --libtokyocabinet-file arg  File name of the database. [see: man tcadb, 
                              tcadbopen() for name guidelines]
  --libtokyocabinet-optimize  Optimize database on open. [default=true]

Outils binaires

Gearman dispose d’un outil en ligne de commande capable de se comporter en client comme en worker, lui aussi disponible dans les dépôts Ubuntu. Cet outil peut soit servir à tester le serveur; soit déjà l’utiliser afin de faire exécuter des scripts bash sur plusieurs machines.

$ sudo apt-get install gearman-tools
$ gearman --help
Client mode: gearman [options] [<data>]
Worker mode: gearman -w [options] [<command> [<args> ...]]

Common options to both client and worker modes.
	-f <function> - Function name to use for jobs (can give many)
	-h <host>     - Job server host
	-H            - Print this help menu
	-p <port>     - Job server port
	-t <timeout>  - Timeout in milliseconds
	-i <pidfile>  - Create a pidfile for the process

Client options:
	-b            - Run jobs in the background
	-I            - Run jobs as high priority
	-L            - Run jobs as low priority
	-n            - Run one job per line
	-N            - Same as -n, but strip off the newline
	-P            - Prefix all output lines with functions names
	-s            - Send job without reading from standard input
	-u <unique>   - Unique key to use for job

Worker options:
	-c <count>    - Number of jobs for worker to run before exiting
	-n            - Send data packet for each line
	-N            - Same as -n, but strip off the newline
	-w            - Run in worker mode

Exemple

Je vais créer un worker tout simple qui retourne la date et l’heure de la machine sur laquelle s’exécute le worker. La fonction se nommera « ma_fonction ». On pourra dire que l’utilité est très limitée, mais cela fera l’affaire pour tester que le serveur fonctionne bien.

Worker :

$ gearman -w -f ma_fonction date

Puis un client qui puisse appeler sans argument (-s) cette fonction « ma_fonction ».

Client :

$ gearman -s -f ma_fonction