Giter VIP home page Giter VIP logo

yii2-gearman's Introduction

yii2-gearman

This extension built on this and this. The goal of the project is opportunity of starting multiple worker processes on one machine.

Installation

It is recommended that you install the Gearman library through composer. To do so, add the following lines to your composer.json file.

{
    "require": {
       "shakura/yii2-gearman": "dev-master"
    }
}

Configuration

'components' => [
  'gearman' => [
      'class' => 'shakura\yii2\gearman\GearmanComponent',
      'servers' => [
          ['host' => '127.0.0.1', 'port' => 4730],
      ],
      'user' => 'www-data',
      'jobs' => [
          'syncCalendar' => [
              'class' => 'common\jobs\SyncCalendar'
          ],
          ...
      ]
  ]
],
...
'controllerMap' => [
    'gearman' => [
        'class' => 'shakura\yii2\gearman\GearmanController',
        'gearmanComponent' => 'gearman'
    ],
    ...
],

Job example

namespace common\jobs;

use shakura\yii2\gearman\JobBase;

class SyncCalendar extends JobBase
{
    public function execute(\GearmanJob $job = null)
    {
        // Do something
    }
}

Manage workers

yii gearman/start 1 // start the worker with unique id
yii gearman/restart 1 // restart worker
yii gearman/stop 1 // stop worker

Example using Dispatcher

Yii::$app->gearman->getDispatcher()->background('syncCalendar', new JobWorkload([
    'params' => [
        'data' => 'value'
    ]
])); // run in background
Yii::$app->gearman->getDispatcher()->execute('syncCalendar', new JobWorkload([
    'params' => [
        'data' => 'value'
    ]
])); // run synchronize

Example of Supervisor config to manage multiple workers

[program:yii-gearman-worker]
command=php [path_to_your_app]/yii gearman/start %(process_num)s
process_name=gearman-worker-%(process_num)s
priority=1
numprocs=5
numprocs_start=1
autorestart=true

yii2-gearman's People

Contributors

kazip avatar shakura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yii2-gearman's Issues

error when I start gearman

./yii gearman/start 1

Success: Process is started, but not daemonized
PHP Fatal error: Call to undefined function shakura\yii2\gearman\pcntl_signal() in /alidata/www/webroot/vendor/shakura/yii2-gearman/Application.php on line 254
PHP Fatal Error 'yii\base\ErrorException' with message 'Call to undefined function shakura\yii2\gearman\pcntl_signal()'

in /alidata/www/webroot/vendor/shakura/yii2-gearman/Application.php:254

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}

High CPU load by gearmand and worker processes

In method createLoop() of Application class, we should be able to increase the value of setTimeout
Not it is $worker->setTimeout(10); - that is 10ms. So worker asks too frequent for new work from gearman, and that leads to high cpu load. I propose to increase this value to 1000 (that means 1s).
When I set it to 1s, cpu load becomes low.

I will be great if we can set this value from outside (in the configuration).

ability to run gearman command with jobs arg

I want to run several gearman clients, however some of them should be restricted to which jobs they run.

Eg, my jobs are:

  • build
  • quote
  • push
  • webhook
  • email

I want one client dedicated to email because I don't want all the clients getting clogged with quotes.

The problem is I have no way to start the gearman command with a limited set of jobs that it can handle.

I get Unknown command: gearman/start

Hello. I get this error when I try to launch gearman demon

Unknown command: gearman/start

main.php

` 'controllerMap' => [
'gearman' => [
'class' => 'shakura\yii2\gearman\GearmanController',
'gearmanComponent' => 'gearman'
],
],

'components' => [

    'gearman' => [
        'class' => 'shakura\yii2\gearman\SyncCalendar',
        'servers' => [
            ['host' => '127.0.0.1', 'port' => 4730],
        ],

        'user' => 'www-data',
        'jobs' => [
            'syncCalendar' => [
                'class' => 'console\jobs\Mail'
            ],
        ]
    ],`

SyncCalendar:

`<?php

namespace common\jobs;

use shakura\yii2\gearman\JobBase;
use Yii;

class SyncCalendar extends JobBase
{
public function execute(\GearmanJob $job = null)
{
Yii::warning(serialize($job));
}
}
`

Is this ready to use

Hi

Great work I was looking for something similar. I just wanted to know, if this is ready to use or it is work under progress. Because as suggested by you, symphony dependency was removed. But while installing using composer in yii2, it sill clones the symphony dependency..

restart has no effect

Hello,
I've updated my worker code, but saying "yii gearman/restart 1 --fork=true" is not updating my workers code.

What can I do ?

Regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.