Skip to content

Commit

Permalink
Optimze Jobs CLI: Expire jobs
Browse files Browse the repository at this point in the history
* Simplifies filter options.
* Add --info flag
  • Loading branch information
TiSiE committed Sep 15, 2016
1 parent 133a1c9 commit dee413c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
14 changes: 7 additions & 7 deletions module/Jobs/Module.php
Expand Up @@ -22,13 +22,13 @@ class Module implements ConsoleUsageProviderInterface

public function getConsoleUsage(Console $console)
{
return array(
'Manipulation of jobs database',
'jobs expire [--filter=]' => 'expire jobs.',
array('--filter=JSON', "available keys:\n"
. "- 'days:INT' -> expire jobs after <days> days. Default 30\n"
. "- 'limit':INT -> Limit jobs to expire per run. Default 10."),
);
return [
'Expire jobs',
'jobs expire [--days] [--limit] [--info]' => 'Expire jobs',
['--days=INT', 'expire jobs after <days> days. Default 30'],
['--limit=INT[,<offset>]', 'Limit jobs to expire per run starting from <offset>. Default 10. 0 means no limit'],
['--info', 'Does not manipulate the database, but prints a list of all matched jobs.']
];
}

/**
Expand Down
4 changes: 3 additions & 1 deletion module/Jobs/config/console.config.php
Expand Up @@ -13,10 +13,12 @@
'routes' => [
'jobs-expire' => [
'options' => [
'route' => 'jobs expire [--filter=]',
'route' => 'jobs expire [--days=] [--limit=] [--info]',
'defaults' => [
'controller' => 'Jobs/Console',
'action' => 'expirejobs',
'days' => 30,
'limit' => '10,0',
],
],
],
Expand Down
80 changes: 51 additions & 29 deletions module/Jobs/src/Jobs/Controller/ConsoleController.php
Expand Up @@ -11,6 +11,7 @@
/** ConsoleController of Jobs */
namespace Jobs\Controller;

use Jobs\Entity\StatusInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\ProgressBar\ProgressBar;
use Core\Console\ProgressBar as CoreProgressBar;
Expand All @@ -26,42 +27,41 @@ public function expireJobsAction()
$repositories = $services->get('repositories');
/* @var \Jobs\Repository\Job $jobsRepo */
$jobsRepo = $repositories->get('Jobs/Job');
$filter = \Zend\Json\Json::decode($this->params('filter', '{}'));
$query = array();
$limit = 10;
foreach ($filter as $key => $value) {
switch ($key) {
case "limit":
$limit = $value;
break;

case "days":
$date = new \DateTime();
$date->modify('-' . (int) $value. ' day');
$q = array('$lt' => $date);
if (isset($query['datePublishStart.date'])) {
$query['datePublishStart.date']= array_merge(
$query['datePublishStart.date'],
$q
);
} else {
$query['datePublishStart.date'] = $q;
}
break;

default:
$query[$key] = $value;
break;
}
$days = (int) $this->params('days');
$limit = (string) $this->params('limit');
$info = $this->params('info');

if (!$days) {
return 'Invalid value for --days. Must be integer.';
}
$query['status.name'] = 'active';

$jobs = $jobsRepo->findBy($query, null, $limit);
$date = new \DateTime('today');
$date->sub(new \DateInterval('P' . $days . 'D'));

$query = [
'status.name' => StatusInterface::ACTIVE,
'datePublishStart.date' => ['$lt' => $date]
];

$offset = 0;
if ($limit && false !== strpos($limit, ',')) {
list($limit,$offset) = explode(',', $limit);
}

$jobs = $jobsRepo->findBy($query, null, (int) $limit, (int) $offset);
$count = count($jobs);

if (0 === $count) {
return 'No jobs found.';
}

if ($info) {
echo count($jobs) , ' Jobs';
if ($offset) { echo ' starting from ' . $offset; }
echo PHP_EOL . PHP_EOL;
$this->listExpiredJobs($jobs);
return;
}

foreach ($repositories->getEventManager()->getListeners('preUpdate') as $listener) {
$repositories->getEventManager()->removeEventListener('preUpdate', $listener);
Expand Down Expand Up @@ -156,4 +156,26 @@ public function setpermissionsAction()
$progress->finish();
return PHP_EOL;
}

private function listExpiredJobs($jobs)
{
/* @var \Jobs\Entity\JobInterface $job */
foreach ($jobs as $job) {
$id = $job->getId();
$org = $job->getOrganization();
if ($org) {
$org = $org->getName();
} else {
$org = $job->getCompany();
}
printf(
'%s %s %-30s %-20s' . PHP_EOL,
$job->getDatePublishStart()->format('Y-m-d'),
$id,
substr($job->getTitle(), 0, 30),
substr($org, 0, 20)
);
}
return count($jobs) . ' Jobs.';
}
}

0 comments on commit dee413c

Please sign in to comment.