Skip to content

Instantly share code, notes, and snippets.

@angeorg
Last active December 16, 2015 11:29
Show Gist options
  • Select an option

  • Save angeorg/5427655 to your computer and use it in GitHub Desktop.

Select an option

Save angeorg/5427655 to your computer and use it in GitHub Desktop.
Gearman small queries processor
<?php
class QueryWorker extends Worker
{
public $numbers;
public $numbers_per_query = 10;
public function write_action($job)
{
$this->numbers[] = $job->workload();
if (count($this->numbers) >= $this->numbers_per_query)
{
echo 'Writing big block of numbers with just one query' . PHP_EOL;
print_r($this->numbers);
$this->process_numbers();
$this->numbers = array();
}
}
public function write_last_action($job)
{
echo 'Numbers left to be processed '
. '(parallel job #' . $job->workload() . '):' . PHP_EOL;
if ($this->numbers)
{
print_r($this->numbers);
$this->process_numbers();
$this->numbers = array();
}
echo '-----------------' . PHP_EOL;
}
public function process_numbers()
{
$sql = 'INSERT INTO page_ids (id) VALUES'
. ' (' . implode('),(', $this->numbers) . ')';
echo 'Processing query:' . PHP_EOL;
echo $sql . PHP_EOL;
return mysql_query($sql) or die(mysql_error());
echo 'Done' . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment