Adding a new task using GearmanManager?

I am still new to the whole Gearman and GearmanManager loop. I have a working server and I have verified that my jobs are running if they are already in my queue (MySQL table) when the server starts. However, I need to be able to add a new job to the queue from PHP and from within the worker, if possible.

I now have a piece of work that I will create when deploying our new codebase. This will be the first job to run, and its goal is to collect some data for reports and save it.

This needs to run every hour per hour, so I want to use a column when_to_run

. I've been pondering the documentation for Gearman, but I'm still confused as to how I was supposed to add the job to the queue.

I've tried working:

<?php
$gm = new GearmanClient;
$gm->addServer();
$gm->doBackground('Metadata_Ingest_Report', '', com_create_guid());

      

On the side of the note, yes, I have php-pecl-uuid installed.

The above code just hangs and does nothing. No task is added to the database, nothing happens.

This is because I don't quite understand how the job is dispatched and I am doing my best for RTM, but I had no luck.

So, if there is anything you can point me to, or if someone has time to explain how I should be setting up and adding jobs to the MySQL queue so the GearmanManager workers pick them up, that would be awesome.

Edit: So you need to make a call $gm->addServer('127.0.0.1')

. According to the documentation, 127.0.0.1

it is assumed to be the default, but PHP 5.4.11 doesn't seem to work. Now I can start tasks if I call $gm->runTasks()

after $gm->addTask()

. I would expect to just call $gm->addTask()

and the task will be added to the database and GearmanManager will see it and run it. More digging ...

Regards,
Andrew

+3


source to share


2 answers


So it seems that the function is when_to_run

not mapped to pecl-gearman

. Because of this, we cannot plan jobs in the future using their built-in methods. It also seems that the library does not create database records like it (I would imagine it could actually be Gearmand, not dumping jobs to the DB before starting them.

To get around this, we decided to do the following.

Planning for future assignments

  • Manually INSERT task into gearman_queue

    .
  • Run CRON every minute to ping the queue table and download jobs that have when_to_run <= time()

  • Fire these tasks through addTask($function, $payload)

    and runTasks()

    . $payload

    contains the UUID from the database.
  • GearmanManager takes the job and gives the payload to its respective workers.
  • The worker starts, then, when finished, removes the item from the database with DELETE

    .

Start work immediately



  • Manually INSERT task into gearmand_queue

    using when_to_run

    NULL

    .
  • Run addTask($function, $payload)

    and runTasks()

    . $payload

    contains the UUID from the database.
  • GearmanManager picks up the job and gives the payload to its active workers.
  • The worker starts, then, when finished, removes the item from the database with DELETE

    .

Conclusion

Gearmand Job Server, GearmanManager and pecl-gearman seem to be out of sync when it comes to what is supported and how it is done. For the most part, I think this question lies at the core of pecl-gearman talking to Gearmand.

I also opened a feature request in the pecl-gearman project for when_to_run

: https://bugs.php.net/bug.php?id=64120

+4


source


Before the added task, the start relay server is running.

for linux:

/usr/bin/gearmand -d  -L 127.0.0.1 -q libdrizzle /
--libdrizzle-user=your_db_user --libdrizzle-password=your_db_pass /
--libdrizzle-db=your_db_name --libdrizzle-mysql -vvvv

      

after adding the task, create a worker See how work.php is:



<?php

$worker= new GearmanWorker(); 

$worker->addServer(); 

while ($worker->work());

function Metadata_Ingest_Report( $job )
{ 
    // do something
}

      

and running this worker

/usr/bin/php worker.php

      

0


source







All Articles