Esempio n. 1
0
function thread_shutdown()
{
    $isError = false;
    if ($error = error_get_last()) {
        switch ($error['type']) {
            case E_ERROR:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_USER_ERROR:
                $isError = true;
                break;
        }
    }
    if ($isError) {
        date_default_timezone_set('Europe/Ljubljana');
        if (defined("THREAD") && defined("THREAD_TYPE")) {
            print date("d.m.Y H:i:s") . " :: " . THREAD_TYPE . " Thread :: " . str_pad(THREAD, 3, "0", STR_PAD_LEFT) . " :: Script execution halted ({$error['message']})\n";
        } else {
            print date("d.m.Y H:i:s") . " :: Script execution halted ({$error['message']})\n";
        }
        if (defined("THREAD") && defined("THREAD_TYPE")) {
            require_once dirname(__FILE__) . '/../common/lib/Net/Gearman/Client.php';
            print date("d.m.Y H:i:s") . " :: " . THREAD_TYPE . " Thread :: " . str_pad(THREAD, 3, "0", STR_PAD_LEFT) . " :: Starting workers restart...\n";
            $set = new Net_Gearman_Set();
            $task = new Net_Gearman_Task("Worker_Restarter", 1);
            $task->type = Net_Gearman_Task::JOB_BACKGROUND;
            $set->addTask($task);
            $client = new Net_Gearman_Client(array('127.0.0.1:4730'));
            $client->runSet($set);
        }
    }
}
Esempio n. 2
0
 /**
  * shutdown handler
  * @return void
  *
  */
 public function onShutdownHandler()
 {
     if ($e = error_get_last()) {
         if (defined("THREAD") && defined("THREAD_TYPE")) {
             require_once dirname(__FILE__) . '/../../../common/lib/Net/Gearman/Client.php';
             print date("d.m.Y H:i:s") . " :: " . THREAD_TYPE . " Thread :: " . str_pad(THREAD, 3, "0", STR_PAD_LEFT) . " :: Starting workers restart...\n";
             $set = new Net_Gearman_Set();
             $task = new Net_Gearman_Task("Worker_Restarter", 1);
             $task->type = Net_Gearman_Task::JOB_BACKGROUND;
             $set->addTask($task);
             $client = new Net_Gearman_Client(array('127.0.0.1:4730'));
             $client->runSet($set);
         }
         $this->raiseEvent('onEndRequest', new CEvent($this));
     }
 }
Esempio n. 3
0
 function __construct()
 {
     $post_id = $_REQUEST['id'];
     $this->db = $db = get_db_connectiuon();
     list($sessionkey, $cookies) = get_login_cookie($db);
     $this->sessionkey = $sessionkey;
     $this->page = $page = getPage();
     $pid = getmypid();
     $this->postid = $postid = (int) $_REQUEST['postid'];
     if (preg_match('%^http://([a-z\\-_.]+\\.)+\\w{2,4}(/mobile)?/post/\\d+$%i', $link = (string) $_REQUEST['permalink'])) {
         $this->permalink = $link;
     } else {
         $link = '';
     }
     $uniqkey = "{$pid}-{$postid}-{$sessionkey}";
     $set = new Net_Gearman_Set();
     $task = new Net_Gearman_Task('reblog', array(cookies => $cookies, postid => $postid, sessionkey => $sessionkey, permalink => $this->permalink, token => $_REQUEST['token'], uniqkey => $uniqkey), $uniqkey, Net_Gearman_Task::JOB_BACKGROUND);
     //   $task->attachCallback('result');
     $set->addTask($task);
     $client = new Net_Gearman_Client(array('localhost:37003'));
     $client->runSet($set);
 }
Esempio n. 4
0
 /**
  *  publish a message
  *  
  *  @param  string  $msg   
  *  @return boolean
  */
 public function _publish($msg)
 {
     $gearman = $this->getPublisher();
     // publish the message...
     $task = new Net_Gearman_Task($this->getField('bind_name'), $msg, $this->getUid(), Net_Gearman_Task::JOB_BACKGROUND);
     $set = new Net_Gearman_Set();
     $set->addTask($task);
     // turn off strict errors since Net Gearman throws a lot of strict errors, this is
     // a terrible way to do it, but I don't want to edit the original code...
     $current_error_level = error_reporting();
     $change_error_level = (bool) ($current_error_level & E_STRICT);
     if ($change_error_level) {
         error_reporting($current_error_level ^ E_STRICT);
     }
     //if
     $gearman->runSet($set);
     if ($change_error_level) {
         error_reporting($current_error_level);
     }
     //if
     $job_handle = $task->handle;
     return !empty($job_handle);
 }
Esempio n. 5
0
<?php

require_once 'Net/Gearman/Client.php';
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
$postid = $_REQUEST['postid'];
if ($email == '' or $password == '' or $postid == '') {
    print 0;
    exit;
}
$pid = getmypid();
$set = new Net_Gearman_Set();
$task = new Net_Gearman_Task('reblog', array('email' => $email, 'password' => $password, 'postid' => $postid), "{$pid}-{$postid}-{$email}", Net_Gearman_Task::JOB_BACKGROUND);
#$task->attachCallback('result');
$set->addTask($task);
$client = new Net_Gearman_Client(array('localhost:37003'));
$client->runSet($set);
print 1;
Esempio n. 6
0
 /**
  * Handle the response read in
  *
  * @param array $resp The raw array response
  * @param resource $s The socket
  * @param object $tasks The tasks being ran
  *
  * @return void
  * @throws Net_Gearman_Exception
  */
 protected function handleResponse($resp, $s, Net_Gearman_Set $tasks)
 {
     if (isset($resp['data']['handle']) && $resp['function'] != 'job_created') {
         $task = $tasks->getTask($resp['data']['handle']);
     }
     switch ($resp['function']) {
         case 'work_complete':
             $tasks->tasksCount--;
             $task->complete(json_decode($resp['data']['result'], true));
             break;
         case 'work_status':
             $n = (int) $resp['data']['numerator'];
             $d = (int) $resp['data']['denominator'];
             $task->status($n, $d);
             break;
         case 'work_fail':
             $tasks->tasksCount--;
             $task->fail();
             break;
         case 'job_created':
             $task = array_shift(Net_Gearman_Connection::$waiting[(int) $s]);
             $task->handle = $resp['data']['handle'];
             if ($task->type == Net_Gearman_Task::JOB_BACKGROUND) {
                 $task->finished = true;
             }
             $tasks->handles[$task->handle] = $task->uniq;
             break;
         case 'error':
             throw new Net_Gearman_Exception('An error occurred');
         default:
             throw new Net_Gearman_Exception('Invalid function ' . $resp['function']);
     }
 }
Esempio n. 7
0
 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;
 }
Esempio n. 8
0
 /**
  * See that task has handle and server assigned.
  *
  * @return void
  */
 public function testTaskStatus()
 {
     $client = new Net_Gearman_Client();
     $task = new Net_Gearman_Task('Reverse', range(1, 5));
     $task->type = Net_Gearman_Task::JOB_BACKGROUND;
     $set = new Net_Gearman_Set();
     $set->addTask($task);
     $client->runSet($set);
     $this->assertNotEquals('', $task->handle);
     $this->assertNotEquals('', $task->server);
 }
Esempio n. 9
0
 protected function create_gearman_set($jobs)
 {
     $task_type = Net_Gearman_task::JOB_BACKGROUND;
     $set = new Net_Gearman_Set();
     foreach ($jobs as $job) {
         #don't bother attaching callbacks since it is a background job
         $task = new Net_Gearman_Task("loader", $job, md5(serialize($job)), $task_type);
         $set->addTask($task);
     }
     return $set;
 }