public function onKnownPayloadReceived($plugin, Payload $payload, $is_known) { /** @var TasksPool $tasks_pool */ $tasks_pool = $this->container['parsley.tasks_pool']; $task_name = $payload->getName(); $task = $tasks_pool->get($task_name); $args = Caller::unpackArguments($payload->getArguments(), $task->getArgumentsOrder()); // TODO: event + subscriber // $task->before($payload, $args); $this->fireSegmentEvent('before.payload.handling: ' . $task_name, [$payload, $args, $task]); $exception = null; $result = null; try { // TODO: event + subscriber $result = Caller::call($task, 'callback', $args); } catch (\Exception $e) { $exception = $e; } if ($exception) { // TODO: event + subscriber // $task->failure($exception, $args, $payload); $this->fireSegmentEvent('failed.payload.handling: ' . $task_name, [$payload, $exception, $args, $task]); } else { // TODO: event + subscriber //$task->after($result, $args, $payload); $this->fireSegmentEvent('successful.payload.handling: ' . $task_name, [$payload, $result, $args, $task]); } // TODO: event + subscriber // $task->always($result, $args, $payload, $exception); $this->fireSegmentEvent('done.payload.handling: ' . $task_name, [$payload, $exception, $result, $args, $task]); }
/** * @param \Parsley\Plugins\SegmentInterface $segment * @param Payload $payload */ public function onSend($segment, $payload) { $props = $payload->getProperties(); /** @var \Parsley\Application $application */ $application = $this->container['parsley.application']; // TODO: dirty hack, get rid of it if (empty($props->getMessageId()) && $props->getMessageId() != 0) { // TODO: make id maker be accessible via container (or plugin?) // http://php.net/manual/en/function.uniqid.php#94959 $uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); $props->setMessageId($uuid); } $props->setAppId($application->getName()); $props->setType('payload'); $props->setContentType($this->container['config']->get('parsley.payload.content_type', 'application/json')); // if (!$props->getAppId()) { // $props->setAppId($segment->getName()); // } // // if (!$props->getType()) { // $props->setType('payload'); // } // // if (!$props->getContentType()) { // $props->setContentType($this->container['config']->get('parsley.payload.content_type', 'application/json')); // } }
/** * @param \Parsley\Plugins\SegmentInterface $segment * @param \Parsley\Core\Payload $payload * @param mixed $result * @param array $args * @param \Parsley\Core\Task $task * @param mixed $group_id * * @return mixed */ public function onChildAfter($segment, $payload, $result, $args, $task, $group_id) { echo " ~Parent {$this->getName()} (#{$group_id}) notified with child's 'after' {$payload->getName()}", PHP_EOL; if ($payload->getName() != 'Parsley.Examples.Tasks.HelloWorld') { return; } $tasks = ['Parsley.Examples.Tasks.HiUniverse']; $no = 0; $arguments = [['hi' => date("Y-m-d H:i:s"), 'universe' => 'from_async ' . $this->getName()]]; $this->group(function () use($tasks, $arguments) { /** @var \Parsley\Application $application */ $application = $this->container['parsley.application']; foreach ($tasks as $id => $name) { $payload = new Payload($name, $arguments[$id], new Properties()); $application->send($payload); $args = empty($arguments[$id]) ? 'no arguments ' : implode(', ', $arguments[$id]); echo "Schedule (from context of {$this->getName()}) {$name} with {$args}", PHP_EOL; } }); echo PHP_EOL; }
/** * @param SegmentInterface $segment * @param \Parsley\Core\Payload $payload */ public function onPayloadReceive($segment, $payload) { $this->fireSegmentEvent('receive', [$payload]); /** @var \Parsley\Core\TasksPool $tasks_pool */ $tasks_pool = $this->container['parsley.tasks_pool']; $task_name = $payload->getName(); // if not registered - drop it, because it should not be here at all $found = $tasks_pool->isRegistered($payload->getName()); if ($found) { $this->fireSegmentEvent("known.payload.received: {$task_name}", [$payload, $found]); } else { $this->fireSegmentEvent("unknown.payload.received: {$task_name}", [$payload, $found]); } $this->fireSegmentEvent('received', [$payload, $found]); }
/** * @param \Parsley\Core\Payload $payload * * @return bool */ protected function getGroupId($payload) { return $payload->getHeaders()['parsley_group_id']; }
protected function checkDelivery(Payload $payload) { if (!$payload->hasDelivery()) { throw new ParsleyException("Payload has no delivery"); } }
/** * @param Payload $payload * * @return array */ public function toArray(Payload $payload) { return ['name' => $payload->getName(), 'arguments' => $payload->getArguments(), 'attributes' => $payload->getAttributes()]; }