This topic contains 4 replies, has 2 voices, and was last updated by kiatsiong.ng 3 years, 3 months ago.
-
Topic
-
I can see the cron schedule is loaded with oro:cron:definitions:load. However, it doesn’t get added to the Job List (table oro_message_queue_job).
This is the command class:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191<?phpnamespace Stars\Bundle\LoggerEventBundle\Command;use Doctrine\DBAL\Connection;use Doctrine\DBAL\ParameterType;use Doctrine\Dbal\Statement;use Doctrine\ORM\EntityManagerInterface;use Oro\Bundle\CronBundle\Command\CronCommandInterface;use Symfony\Component\Console\Command\Command;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Input\InputOption;use Symfony\Component\Console\Output\OutputInterface;use Stars\Bundle\EmgsiheDbBundle\Dbal\Connection as ConnEmgsihe;use Stars\Bundle\LoggerEventBundle\Entity\ActionData;class InsertActionDataCommand extends Command implements CronCommandInterface{/*** Command name*/const COMMAND_NAME = 'oro:cron:sc-logger-event:insert-action-data';/*** The max number of events to read from STARS*/const LIMIT_EVENTS = 80;/*** Connection for emgsihe DB** @var Connection*/private $connection;/*** @var EntityManagerInterface*/private $entityManager;public function __construct(ConnEmgsihe $connEmgsihe, EntityManagerInterface $entityManager){$this->connection = $connEmgsihe->getConnection();$this->entityManager = $entityManager;parent::__construct();}/*** {@inheritDoc}*/public function isActive(){return true;}/*** {@inheritDoc}*/public function getDefaultDefinition(){return '*/1 * * * *';}/*** {@inheritdoc}*/public function configure(){$this->setName(static::COMMAND_NAME)->setDescription('Insert unserialized action_data to table sc_stars_logger_event_action_data')->addOption('limit','l',InputOption::VALUE_OPTIONAL,'The max number of events to read from STARS.',self::LIMIT_EVENTS);}/*** {@inheritDoc}*/protected function execute(InputInterface $input, OutputInterface $output){$limit = (int) $input->getOption('limit') ?: self::LIMIT_EVENTS;$t1 = microtime(true);$cnt = $this->insertByEntity($limit);$dt = (microtime(true) - $t1);$output->writeln(sprintf('<comment>Rows Inserted:</comment> %d', $cnt));$output->writeln(sprintf('<comment>Seconds Took:</comment> %f', $dt));}/*** Get the event_id for retrieving the next records from emgsihe DB*/public function getEventId(): int{$sql = 'SELECT event_id FROM sc_stars_logger_event_action_dataORDER BY event_id DESCLIMIT 1';$stmt = $this->entityManager->getConnection()->prepare($sql);$stmt->execute();$id = (int) $stmt->fetchColumn() + 1;return $id;}/*** Get the events from emgsihe DB*/public function getReadStatement(int $limit, int $fromEventId): Statement{$fromEventId = $fromEventId ?: $this->getEventId();$sql = 'SELECT event_id, action_data FROM scicom_logger_eventWHERE event_id >= :fromEventIdLIMIT :limit';$stmt = $this->connection->prepare($sql);$stmt->bindValue('fromEventId', $fromEventId, ParameterType::INTEGER);$stmt->bindValue('limit', $limit, ParameterType::INTEGER);$stmt->execute();return $stmt;}/*** Insert rows by entity*/public function insertByEntity(int $limit = self::LIMIT_EVENTS, int $fromEventId = 0): int{$stmt = $this->getReadStatement($limit , $fromEventId);$cnt = 0;while ($row = $stmt->fetch()) {$data = $this->prepEntityFields($row['action_data']);$cnt += count($data);foreach ($data as $datum) {$entity = new ActionData();$entity->setEventId((int) $row['event_id'])->setField($datum['field'])->setFrom($datum['from'])->setTo($datum['to']);$this->entityManager->persist($entity);}$this->entityManager->flush();}return $cnt;}/*** Prepare entity fields, try to correct unserialize error*/protected function prepEntityFields($actionData): array{// Ensure we have UTF-8 encodingif (!preg_match('%^(?:[\x09\x0A\x0D\x20-\x7E] # ASCII| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16)*$%xs', $actionData)) {$actionData = iconv('CP1252', 'UTF-8', $actionData);}$data = @unserialize($actionData);if ($data === null) {$data2 = preg_replace_callback('!s:\d+:"(.*?)";!s', function($m) { return "s:" . strlen($m[1]) . ':"'.$m[1].'";'; }, $actionData);$data = @unserialize($data2);}if (empty($data)) {$data = [['field' => 'serialize_error','from' => $actionData,'to' => null]];}return $data;}}I can run the command in CLI:
123[kiat@reporting misoro]$ sudo -u nginx php bin/console oro:cron:sc-logger-event:insert-action-dataRows Inserted: 2187Seconds Took: 2.670428What did I miss?
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.