/** * actually sends a message to to the daemon and returns the sent message * * @param string $key * @param int $value * @param string $type * @param int $sampleRate */ private function send($key, $value, $type, $sampleRate) { if (strlen($this->namespace) != 0) { $key = sprintf('%s.%s', $this->namespace, $key); } $message = sprintf("%s:%s|%s", $key, $value, $type); $sample = mt_rand() / mt_getrandmax(); if ($sample > $sampleRate) { return; } // overwrite sampleRate if all metrics should be sampled if ($this->sampleRateAllMetrics < 1) { $sampleRate = $this->sampleRateAllMetrics; } if ($sampleRate < 1) { $sampledData = sprintf('%s|@%s', $message, $sampleRate); } else { $sampledData = $message; } if (!$this->isBatch) { $this->connection->send($sampledData); } else { $this->batch[] = $sampledData; } }
/** * @param string $_table */ public function truncate($_table) { /* ## LOGGER ## */ if (isset($this->logger)) { $this->logger->DEBUG('truncate: ' . $_table); } if (empty($_table)) { throw new UndefinedTabelException('null'); } $table = $this->connection->escape($_table); $sql = 'TRUNCATE TABLE `' . $table . '`'; $result = $this->connection->send($sql); }
/** * @param string $_index */ public function delete($_index) { /* ## LOGGER ## */ if (isset($this->logger)) { $this->logger->DEBUG('delete'); } if (empty($_index)) { throw new UndefinedRowException('null'); } $table = $this->connection->escape($this->table); $primary = $this->connection->escape($this->primary); $index = $this->connection->escape($_index); $sql = 'DELETE FROM `' . $table . '` WHERE `' . $primary . '` = \'' . $index . '\';'; $result = $this->connection->send($sql); if ($this->connection->getAffectedRows() <= 0) { throw new UndefinedRowException('undefined ' . $primary . '=' . $index); } }
/** * Mark your job as failing * * If your job fails for some reason (e.g. a query fails) you need to run * this function and exit from your run() method. This will tell Gearman * (and the client by proxy) that the job has failed. * * @param resource $socket * @param string $handle * * @see Net\Gearman\Connection::send() */ private function jobFail($socket, $handle) { Connection::send($socket, 'work_fail', array('handle' => $handle)); }
/** * ends batch-send-recording and sends the recorded messages to the connection * * @return void */ public function endBatch() { $this->_isBatch = false; $this->_connection->send(join("\n", $this->_batch)); $this->_batch = array(); }
/** * Submit a task to Gearman * * @param Task $task Task to submit to Gearman * * @return void * @see \Net\Gearman\Task, \Net\Gearman\Client::runSet() */ protected function submitTask(Task $task) { switch ($task->type) { case Task::JOB_LOW: $type = 'submit_job_low'; break; case Task::JOB_LOW_BACKGROUND: $type = 'submit_job_low_bg'; break; case Task::JOB_HIGH_BACKGROUND: $type = 'submit_job_high_bg'; break; case Task::JOB_BACKGROUND: $type = 'submit_job_bg'; break; case Task::JOB_EPOCH: $type = 'submit_job_epoch'; break; case Task::JOB_HIGH: $type = 'submit_job_high'; break; default: $type = 'submit_job'; break; } $arg = $task->arg; $params = array('func' => $task->func, 'uniq' => $task->uniq, 'arg' => $arg); if ($task->type == Task::JOB_EPOCH) { $params['epoch'] = $task->epoch; } $s = $this->getConnection(); Connection::send($s, $type, $params); if (!is_array(Connection::$waiting[(int) $s])) { Connection::$waiting[(int) $s] = array(); } array_push(Connection::$waiting[(int) $s], $task); }