/** * Notify the Client * * @param int $type one of NOTIFY_* * @param string $data * @return bool */ public function notify($type, $data = null) { if ($type == JobInterface::NOTIFY_COMPLETE) { return $this->job->sendComplete($data); } if ($type == JobInterface::NOTIFY_DATA) { return $this->job->sendData($data); } if ($type == JobInterface::NOTIFY_EXCEPTION) { return $this->job->sendException($data); } if ($type == JobInterface::NOTIFY_FAIL) { return $this->job->sendFail(); } if ($type == JobInterface::NOTIFY_WARNING) { return $this->job->notifyWarning($data); } return false; }
/** * Realizuje wysyłkę wiadomości w pakiecie * @param GearmanJob $job */ public function sendpackage(GearmanJob $job) { $params = json_decode($job->workload()); list($mode, $packageDataId, $packageId) = $params; $package = new Logic_Package($mode); $wsPackage = new WsPackage(); $wsPackageRow = $wsPackage->findOne($packageId); $package->updatePackageStatus($packageId, WsPackageStatus::STATUS_STARTED); $wsServiceSetRow = $wsPackageRow->findParentRow('WsServiceSet'); $rawData = $package->getRawDataToSend($packageDataId); $client = Logic_Client::getInstanceByUser($package->getLastPackageStatus($packageId)->created_by); $package->updatePackageDataStatus($packageDataId, array('id_status' => WsPackageStatus::STATUS_STARTED)); $service; switch ($mode) { case Logic_Package::SMS_MODE: $loadBalancer = $client->getLoadBalancer(Wsclient::GROUP_SMS, $wsServiceSetRow->ws_service_group_id); $hasErrors = false; $service = $loadBalancer->getService(); do { if (null === $service) { $package->updatePackageDataStatus($packageDataId, array('id_status' => WsPackageStatus::STATUS_ERROR)); return $job->sendException("Unable to get service! Check backup service!"); } $sms = new Logic_Sms($service, $wsPackageRow->ip, $wsServiceSetRow->id); try { if ($wsPackageRow->getLastStatus()->id_status == WsPackageStatus::STATUS_STARTED) { $response = $sms->package($rawData, $packageId); $package->updatePackageDataStatus($packageDataId, array('id_status' => WsPackageStatus::STATUS_SENT, 'external_package_id' => $response->packageId, 'ws_service_id' => $service->id)); $package->confirmPackageSend($wsPackageRow); } break; } catch (Exception $exc) { $hasErrors = true; $package->updatePackageDataStatus($packageDataId, array('id_status' => WsPackageStatus::STATUS_ERROR)); $service = $loadBalancer->getService(); sleep(1); } } while (true === $hasErrors || !(true === is_object($response) && true === property_exists($response, 'packageId'))); break; } unset($client); }
/** * Executes a [Request] object and returns the response * * // Create a request * $request = Request::factory('/users/load/1'); * * // Execute the request * $response = Request_Async_Gearman::execute_request($request); * * Used as a callback by Request_Async_Gearman to handle the request * processing. * * @param Kohana_Request request to process * @return Kohana_Response the response from the request */ public static function execute_request(GearmanJob $job) { // Unserialise the request $request = unserialize($job->workload()); // Send starting status $job->sendStatus(1, 2); // Encapsulate request execution try { // Get the response $response = $request->execute()->headers('X-Request-Uri', $request->uri()); } catch (Exception $e) { // Send the exception to Gearman $job->sendException($e->getMessage()); return; } // Send complete status $job->sendStatus(2, 2); // Send the response $job->sendData(serialize($response)); }
/** * Send exception to server or client error message. * * @param Exception|string $exception * @return bool */ public function sendException($exception) { return $this->_job->sendException($exception); }