/** * Creates a NodeRef from ElementSlug & NodeSlug in $params. * * @param array $params User defined request parameters (or noderef - 'element:slug') * @throws MediaServiceException If invalid Slugs or Element * @return NodeRef */ protected function getNodeRef($params) { if (is_array($params)) { if (empty($params['ElementSlug'])) { throw new MediaServiceException('ElementSlug must be specified'); } if (empty($params['NodeSlug'])) { throw new MediaServiceException('NodeSlug must be specified'); } $elementSlug = $params['ElementSlug']; $nodeSlug = $params['NodeSlug']; } else { $ar = explode(':', $params); if (count($ar) < 2) { throw new MediaServiceException("Invalid node ref: {$params}"); } $elementSlug = $ar[0]; $nodeSlug = $ar[1]; } $element = $this->ElementService->getBySlug($elementSlug); if ($element == null) { throw new MediaServiceException("Invalid Element"); } $noderef = new NodeRef($element, $nodeSlug); return $noderef; }
/** * populate #thumbnails-json using gearman * * Parameters: * element: required param to specify element or @aspect * interval: nodes to process per pass; default 100 * limit: nodes to process per job: default 5000 * offset: node to skip to for the job: default 0 * @return void */ public function syncJsonThumbnailsDistributed() { $interval = $this->Request->getParameter('interval') or $interval = 100; $interval = intval($interval); $limit = $this->Request->getParameter('limit') or $limit = 5000; $limit = intval($limit); $initialOffset = $this->Request->getParameter('offset') or $initialOffset = 0; $initialOffset = intval($initialOffset); $elementField = $this->Request->getParameter('element'); $aspect = $this->Request->getParameter('aspect'); if (!empty($elementField)) { if (strpos($elementField, '@') === 0) { $elements = $this->ElementService->findAllWithAspect($elementField); if (empty($elements)) { echo "No Elements found for aspect [{$elementField}]\n"; return; } } else { $element = $this->ElementService->getBySlug($elementField); if (empty($element)) { echo "Element not found for slug [{$elementField}]\n"; return; } } $elements = array($elementField); $offset = $initialOffset; } else { if (!empty($aspect)) { $elementsForAspect = $this->ElementService->findAllWithAspect($aspect); if (empty($elementsForAspect)) { echo "No Elements found for aspect [{$aspect}]\n"; return; } $elements = array(); foreach ($elementsForAspect as $e) { $elements[] = $e->Slug; } $offset = 0; } else { echo "Error - must specify one of element or aspect\n"; return; } } $elementList = implode(',', $elements); echo "\nProcessing elements: {$elementList}\n"; echo "offset = {$offset}, interval = {$interval}, limit = {$limit}\n"; if ($offset > 0) { echo "\nskipping to {$offset} limit {$limit}\n"; } $count = 0; $startTime = time(); foreach ($elements as $element) { echo "\nProcessing element: {$element}\n"; $nq = new NodeQuery(); $nq->setParameter('Elements.in', $element); $nq->setParameter('Count.only', true); $nq->setParameter('Status.all', true); $nq->setOrderBy('ActiveDate DESC'); $totalNodeCount = $this->NodeService->findAll($nq)->getTotalRecords(); echo "Nodes to process: {$totalNodeCount}\n"; $countForElement = 0; while (true) { $nq = new NodeQuery(); $nq->setParameter('NodeRefs.only', true); $nq->setParameter('Elements.in', $element); $nq->setOrderBy('ActiveDate DESC'); $nq->setParameter('Status.all', true); $nq->setLimit($interval); $nq->setOffset($offset); //$nodes = $this->NodeService->findAll($nq, true)->getResults(); $nodes = $this->NodeService->findAll($nq)->getResults(); if (empty($nodes)) { echo "\nElement '{$element}' done - {$countForElement} nodes processed\n"; break; } $this->Logger->debug('Processing ' . count($nodes)); $nodeRefs = array(); foreach ($nodes as $nodeRef) { $nodeRefs[] = '' . $nodeRef; $count++; $countForElement++; if ($count > $limit) { echo "Limit hit at '{$element}' node {$countForElement}\r"; break; } } echo "Nodes processed - element: {$countForElement}, total: {$count}\r"; $params = array('nodeRefs' => implode(',', $nodeRefs)); $this->Logger->debug('Initializing Gearman job: ' . ($count + $initialOffset - 1) . " [{$count}:{$limit}] " . $nodeRef); $this->GearmanService->doBackgroundJob('ImagesWorker', 'syncJsonThumbnails', $params, 'low'); usleep(10000); if ($count > $limit) { echo "\nLimit hit in '{$element}' - {$countForElement} nodes processed, {$count} jobs initialized\n"; $this->Logger->debug('Limit hit: ' . $count . ' jobs initialized'); break; } $offset = $offset + $interval; } $offset = 0; } echo "\n\nDone! {$count} nodes processed\n"; $endTime = time(); $t = $endTime - $startTime; $minutes = ceil($t / 60); echo "Process took {$minutes} minutes to complete\n\n"; }