/** * Submit a Celery job via direct job request insertion into Redis. * * @return int */ public static function celerySubmit() { # this function does NO checking on inputs. Good luck! # grab the arguments. this function has variable arguments. # it is assumed that the first argument is the metrics function Celery is to # execute, while the remaining arguments are assumed to be integer arguments # to be passed to the metrics Celery function. require "Predis/Autoloader.php"; Predis\Autoloader::register(); if (func_num_args() == 0) { return -599; # incorrect number of arguements } else { $params = func_get_args(); } $args2pass = array_slice($params, 1); for ($i = 0; $i < count($args2pass); $i++) { $args2pass[$i] = (int) $args2pass[$i]; } $task = array("task" => "Metrics.metrics_base.{$params[0]}", "args" => $args2pass); $task["id"] = sha1(json_encode($task["args"]) . time()); $bodyTask = array("body" => base64_encode(json_encode($task)), "headers" => new stdClass(), "content-type" => "application/json", "properties" => array("body_encoding" => "base64", "delivery_info" => array("priority" => 0, "routing_key" => "default", "exchange" => "default"), "delivery_tag" => sha1(base64_encode(json_encode($task)) . time()), "delivery_mode" => 2), "content-encoding" => "utf-8"); # new Predis\Client try { $redis = new Predis\Client(array("host" => "127.0.0.1", "port" => 6379, "database" => 9)); } catch (Exception $e) { die($e->getMessage()); } # debugging... # echo "celery-task-meta-".$task["id"]."\n"; $redis->lPush('celery', json_encode($bodyTask)); }
/** * Prepend a value to a list * * @param string $key * @param string $value * @throws CM_Exception_Invalid */ public function lPush($key, $value) { try { $this->_redis->lPush($key, $value); } catch (Predis\Response\ServerException $e) { throw new CM_Exception_Invalid('Cannot push key to list.', null, ['key' => $key]); } }
# remote log analysers are fine things, but the overhead in establishing a # connection cannot be tolerated. # # TODO: does the RavenHandler re-establish a connection if it is dropped? require 'vendor/raven/raven/lib/Raven/Autoloader.php'; Raven_Autoloader::register(); $client = new Raven_Client(RAVEN_DSN, array('release' => '2015-08-13')); $log->pushHandler(new RavenHandler($client)); # # redis # # doesn't actually connect to Redis ... $redis = new Predis\Client(['scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379]); # delete whatever happens to be in that value now // $redis->del(QUEUE_NAME); # good for testing, don't want to do this in production. # push an initial value into the list to check everything is working define("CONTROL_SIGNAL", 0.0); $redis->lPush(QUEUE_NAME, CONTROL_SIGNAL); # this is amongst the grossest things I've ever seen. assert('$redis->exists(QUEUE_NAME); //* could not find queue ' . QUEUE_NAME); while (true) { list($_, $record) = $redis->brPop(QUEUE_NAME, TIMEOUT); if (empty($record) || $record == CONTROL_SIGNAL) { //echo "null/control signal. continuing\n"; continue; } echo "received " . $record; # the 'DEBUG' here doesn't matter, our record processor overwrites this $log->addDebug($record); } echo "done\n";