Skip to content

Commit 533cf05

Browse files
author
Ethan Yehuda
authored
Merge pull request #14 from Ethan3600/1.x-develop
Merge Command Line Interface: Cronmanager into 1.x
2 parents c3d6b6f + c301c68 commit 533cf05

File tree

4 files changed

+175
-4
lines changed

4 files changed

+175
-4
lines changed

Command/Runjob.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace EthanYehuda\CronjobManager\Command;
3+
4+
use Symfony\Component\Console\Command\Command;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use EthanYehuda\CronjobManager\Model\Manager;
9+
use \Magento\Framework\App\State;
10+
use \Magento\Framework\Console\Cli;
11+
use \Magento\Framework\Stdlib\DateTime\DateTime;
12+
13+
class Runjob extends Command
14+
{
15+
const INPUT_KEY_JOB_CODE = 'job_code';
16+
17+
/**
18+
* @var EthanYehuda\CronjobManager\Model\Manager $manager
19+
*/
20+
private $manager;
21+
22+
/**
23+
* @var \Magento\Framework\App\State $state
24+
*/
25+
private $state;
26+
27+
/**
28+
* @var \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
29+
*/
30+
private $dateTime;
31+
32+
public function __construct(
33+
State $state,
34+
Manager $manager,
35+
DateTime $dateTime
36+
) {
37+
$this->manager = $manager;
38+
$this->state = $state;
39+
$this->dateTime = $dateTime;
40+
parent::__construct();
41+
}
42+
protected function configure()
43+
{
44+
$arguments = [
45+
new InputArgument(
46+
self::INPUT_KEY_JOB_CODE,
47+
InputArgument::REQUIRED,
48+
'Job code input (ex. \'sitemap_generate\')'
49+
)
50+
];
51+
52+
$this->setName("cronmanager:runjob");
53+
$this->setDescription("Run a specific cron job by its job_code ");
54+
$this->setDefinition($arguments);
55+
parent::configure();
56+
}
57+
58+
protected function execute(InputInterface $input, OutputInterface $output)
59+
{
60+
try {
61+
$this->state->setAreaCode('adminhtml');
62+
63+
// lets create a new cron job and dispatch it
64+
$jobCode = $input->getArgument(self::INPUT_KEY_JOB_CODE);
65+
$now = strftime('%Y-%m-%dT%H:%M:%S', $this->dateTime->gmtTimestamp());
66+
67+
$schedule = $this->manager->createCronJob($jobCode, $now);
68+
$this->manager->dispatchCron(null, $jobCode, $schedule);
69+
$output->writeln("$jobCode successfully ran");
70+
return Cli::RETURN_SUCCESS;
71+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
72+
$output->writeln($e->getMessage());
73+
return Cli::RETURN_FAILURE;
74+
}
75+
}
76+
}

Command/Showjobs.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
namespace EthanYehuda\CronjobManager\Command;
3+
4+
use Symfony\Component\Console\Command\Command;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use EthanYehuda\CronjobManager\Model\Manager;
8+
use \Magento\Framework\App\State;
9+
use \Magento\Framework\Console\Cli;
10+
11+
class Showjobs extends Command
12+
{
13+
/**
14+
* @var EthanYehuda\CronjobManager\Model\Manager $manager
15+
*/
16+
private $manager;
17+
18+
/**
19+
* @var \Magento\Framework\App\State $state
20+
*/
21+
private $state;
22+
23+
/**
24+
* @var Array $headers
25+
*/
26+
private $headers = ['Job Code', 'Group', 'Frequency', 'Class'];
27+
28+
public function __construct(
29+
State $state,
30+
Manager $manager
31+
) {
32+
$this->manager = $manager;
33+
$this->state = $state;
34+
parent::__construct();
35+
}
36+
protected function configure()
37+
{
38+
$this->setName("cronmanager:showjobs");
39+
$this->setDescription("Show all cron job codes in Magento");
40+
parent::configure();
41+
}
42+
43+
protected function execute(InputInterface $input, OutputInterface $output)
44+
{
45+
try {
46+
$this->state->setAreaCode('adminhtml');
47+
48+
$jobs = $this->manager->getCronJobs();
49+
$table = $this->getHelperSet()->get('table')->setHeaders($this->headers);
50+
51+
foreach ($jobs as $group => $crons) {
52+
foreach ($crons as $code => $job) {
53+
$instance = $job['instance'];
54+
$method = $job['method'];
55+
$schedule = (isset($job['schedule']) ? $job['schedule'] : "");
56+
$jobData = [
57+
$code,
58+
$group,
59+
$schedule,
60+
"$instance::$method"
61+
];
62+
$table->addRow($jobData);
63+
}
64+
}
65+
66+
$table->render($output);
67+
return Cli::RETURN_SUCCESS;
68+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
69+
$output->writeln($e->getMessage());
70+
return Cli::RETURN_FAILURE;
71+
}
72+
}
73+
}

Model/Manager.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function createCronJob($jobCode, $time)
2222
->setScheduledAt($filteredTime);
2323

2424
$schedule->getResource()->save($schedule);
25+
return $schedule;
2526
}
2627

2728
public function saveCronJob($jobId, $jobCode = null, $status = null, $time = null)
@@ -57,12 +58,14 @@ public function flushCrons()
5758
}
5859
}
5960

60-
public function dispatchCron($jobId, $jobCode)
61+
public function dispatchCron($jobId = null, $jobCode, $schedule = null)
6162
{
6263
$groups = $this->_config->getJobs();
6364
$groupId = $this->getGroupId($jobCode, $groups);
6465
$jobConfig = $groups[$groupId][$jobCode];
65-
$schedule = $this->loadSchedule($jobId);
66+
if(is_null($schedule)) {
67+
$schedule = $this->loadSchedule($jobId);
68+
}
6669
$scheduledTime = $this->dateTime->gmtTimestamp();
6770

6871
/* We need to trick the method into thinking it should run now so we
@@ -71,6 +74,11 @@ public function dispatchCron($jobId, $jobCode)
7174
$this->_runJob($scheduledTime, $scheduledTime, $jobConfig, $schedule, $groupId);
7275
$schedule->getResource()->save($schedule);
7376
}
77+
78+
public function getCronJobs()
79+
{
80+
return $this->_config->getJobs();
81+
}
7482

7583
// ========================= UTILITIES ========================= //
7684

@@ -115,4 +123,4 @@ protected function loadSchedule($jobId)
115123
$scheduleResource->load($schedule, $jobId);
116124
return $schedule;
117125
}
118-
}
126+
}

etc/di.xml

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,18 @@
1313
</argument>
1414
</arguments>
1515
</type>
16-
</config>
16+
<type name="Magento\Framework\Console\CommandList">
17+
<arguments>
18+
<argument name="commands" xsi:type="array">
19+
<item name="ethanyehuda_cronjobmanager_command_showjobs" xsi:type="object">EthanYehuda\CronjobManager\Command\Showjobs</item>
20+
</argument>
21+
</arguments>
22+
</type>
23+
<type name="Magento\Framework\Console\CommandList">
24+
<arguments>
25+
<argument name="commands" xsi:type="array">
26+
<item name="ethanyehuda_cronjobmanager_command_runjob" xsi:type="object">EthanYehuda\CronjobManager\Command\Runjob</item>
27+
</argument>
28+
</arguments>
29+
</type>
30+
</config>

0 commit comments

Comments
 (0)