public function processContent() { $body = $this->response->getBody(); $domDocument = new Query($body); $limit = 5; foreach ($domDocument->execute('ul.prod_grd.three_per_row.prod_lst_square_ic li a.lst_a') as $key) { if (--$limit <= 0) { //break; } $href = $key->getAttribute('href'); if (!parse_url($href, PHP_URL_HOST)) { $urlHost = parse_url($this->url, PHP_URL_HOST); $urlScheme = parse_url($this->url, PHP_URL_SCHEME) . '://'; $href = $urlScheme . $urlHost . $href; } $task = new \Net_Gearman_Task('parse', array('arg' => array('url' => $href, 'file' => $this->fileName)), null, \Net_Gearman_Task::JOB_NORMAL); $task->attachCallback($this->getTaskFinishedCallback()); $this->set->addTask($task); } if ($this->set->count() > 0) { $filename = $this->config->getSystemConfig()->getParseDir() . $this->fileName; if (file_exists($filename)) { rename($filename, $this->config->getSystemConfig()->getParseDir() . basename($this->fileName, '.csv') . '_toupdate.csv'); } $this->set->attachCallback($this->getJobFinishedCallback()); Logger::addMessage('Jobs pushed to server: ' . $this->set->count()); $this->jobTotal = $this->set->count(); $this->client->runSet($this->set); } }
/** * Run the complete callback. * * @return void */ public function testCompleteCallback() { $task = new Net_Gearman_Task('foo', array('foo' => 'bar')); $this->assertEquals(null, $task->complete('foo')); // Attach a callback for real $task->attachCallback('Net_Gearman_TaskTest_testCallBack'); // build result and call complete again $json = json_decode('{"foo":"bar"}'); $task->complete($json); $this->assertEquals($json, $task->result); $this->assertEquals(array('func' => 'foo', 'handle' => '', 'result' => $json), $GLOBALS['Net_Gearman_TaskTest']); unset($GLOBALS['Net_Gearman_TaskTest']); }
public function addTask($class, $function, $params = array()) { $Job = new Job(); $Job->job_set_id = $this->JobSet->id; $Job->enqueue = date('Y-m-d H:i:s'); $Job->timeout = 30; $Job->needed = 1; if ($Job->save()) { $task_id = $Job->id; $parameters = array('class' => $class, 'function' => $function, 'params' => $params, 'WorkerJobSetIdentifier' => $this->randomHash, 'task_id' => $task_id, 'background' => $this->background); $Job->parameters = base64_encode(serialize($parameters)); $Job->save(); $task = new Net_Gearman_Task('AllClasses', $Job->parameters, $this->randomHash . $task_id); if ($this->background) { $task->type = Net_Gearman_Task::JOB_BACKGROUND; } $task->attachCallback('WorkerJobSetReturnResult'); $this->taskList->addTask($task); $this->jobCounter++; return $Job->id; } else { return -1; } }
<?php require_once 'Net/Gearman/Client.php'; $set = new Net_Gearman_Set(); function result($func, $handle, $result) { var_dump($func); var_dump($handle); var_dump($result); } $sql = array("SELECT * FROM users WHERE username = '******'", "SELECT * FROM users WHERE username LIKE 'joe%' LIMIT 10", "SELECT * FROM items WHERE deleted = 0 LIMIT 10"); foreach ($sql as $s) { $task = new Net_Gearman_Task('SQL', array('sql' => $s)); $task->attachCallback('result'); $set->addTask($task); } $client = new Net_Gearman_Client(array('dev01')); $client->runSet($set);
protected function create_gearman_set($queries, $table, $complete_cb = null, $failure_cb = null, &$set, $action = "store_resultset", &$state) { if ($this->async) { $task_type = Net_Gearman_task::JOB_BACKGROUND; $query_id = $state->query_id; } else { $task_type = Net_Gearman_task::JOB_NORMAL; $query_id = null; } $sets = array(); if (!$set) { $set = new Net_Gearman_Set(); } if (isset($state->force_shard) && !empty($state->force_shard)) { $shards = $state->force_shard; } else { $shards = $state->shards; } $this->create_agg_table($state); for ($i = 0; $i < count($queries); ++$i) { foreach ($shards as $shard) { $task = new Net_Gearman_Task("store_resultset", array('engine' => $state->engine, 'table_name' => $table, 'sql' => $queries[$i], 'coord_odku' => $state->coord_odku, 'shard' => $shard, 'tmp_shard' => $state->tmp_shard, 'agg_key_cols' => $state->agg_key_cols, 'when' => microtime(true), 'action' => $action, 'query_id' => $query_id), uniqid(md5($queries[$i]), true), $task_type); $task->attachCallback($complete_cb); $task->attachCallback($failure_cb, Net_Gearman_Task::TASK_FAIL); $set->addTask($task); $state->store_resultset_count++; } $sets[] = $set; } return $sets; }
<?php require_once 'Net/Gearman/Client.php'; function complete($func, $handle, $result) { var_dump($func); var_dump($handle); var_dump($result); } function fail($task_object) { var_dump($task_object); } $client = new Net_Gearman_Client('localhost:7003'); $set = new Net_Gearman_Set(); $jobs = array('AddTwoNumbers' => array('1', '2'), 'Multiply' => array('3', '4')); foreach ($jobs as $job => $args) { $task = new Net_Gearman_Task($job, $args); $task->attachCallback("complete", Net_Gearman_Task::TASK_COMPLETE); $task->attachCallback("fail", Net_Gearman_Task::TASK_FAIL); $set->addTask($task); } $client->runSet($set);
<?php require_once dirname(__FILE__) . '/../common/lib/Net/Gearman/Client.php'; // create set for tasks $set = new Net_Gearman_Set(); $config = array(); $results = array(); for ($i = 100; $i < 140; $i++) { print "Starting process: id " . $i . "\n"; $task = new Net_Gearman_Task("AllClasses", array("class" => "Svg2Pdf", "function" => "reverse", "params" => array("RandomString" . $i)), rand(100, 200) . "_" . $i); //$task->type = Net_Gearman_Task::JOB_BACKGROUND; // uncomment to run job in background $task->attachCallback("result"); // add callback function, only available type != JOB_BACKGROUND // $results[$task->uniq]; $set->addTask($task); // add task to the set } $client = new Net_Gearman_Client(array('localhost:4730')); $client->runSet($set); if ($set->finished()) { print "All tasks done...\n"; $handles = $set->handles; // print_r($handles); // for ($i = 0; $i < $iterator->count(); $i++) { // print $handles[$keys[$i]]."\n"; // print_r($iterator->$handles[$keys[$i]]); // $results[] = $iterator->$handles[$keys[$i]]->result; // } $reorder = array(); // print_r($results); // $keys = array_keys($results);