2
2
3
3
namespace EthanYehuda \CronjobManager \Console \Command ;
4
4
5
- use EthanYehuda \CronjobManager \Model \ManagerFactory ;
5
+ use Magento \Framework \App \ObjectManagerFactory ;
6
+ use Magento \Store \Model \Store ;
7
+ use Magento \Store \Model \StoreManager ;
6
8
use Symfony \Component \Console \Command \Command ;
9
+ use Symfony \Component \Console \Input \InputArgument ;
7
10
use Symfony \Component \Console \Input \InputInterface ;
8
11
use Symfony \Component \Console \Output \OutputInterface ;
9
- use Symfony \Component \Console \Input \InputArgument ;
10
- use Magento \Framework \App \Area ;
11
- use Magento \Framework \App \State ;
12
- use Magento \Framework \Console \Cli ;
13
- use Magento \Framework \Stdlib \DateTime \DateTimeFactory ;
14
12
15
13
class Runjob extends Command
16
14
{
17
15
const INPUT_KEY_JOB_CODE = 'job_code ' ;
18
16
19
17
/**
20
- * @var ManagerFactory $managerFactory
21
- */
22
- private $ managerFactory ;
23
-
24
- /**
25
- * @var \Magento\Framework\App\State $state
18
+ * @var ObjectManagerFactory
26
19
*/
27
- private $ state ;
28
-
29
- /**
30
- * @var DateTimeFactory $dateTimeFactory
31
- */
32
- private $ dateTimeFactory ;
20
+ private $ objectManagerFactory ;
33
21
34
22
public function __construct (
35
- State $ state ,
36
- ManagerFactory $ managerFactory ,
37
- DateTimeFactory $ dateTimeFactory
23
+ ObjectManagerFactory $ objectManagerFactory
38
24
) {
39
- $ this ->managerFactory = $ managerFactory ;
40
- $ this ->state = $ state ;
41
- $ this ->dateTimeFactory = $ dateTimeFactory ;
25
+ $ this ->objectManagerFactory = $ objectManagerFactory ;
42
26
parent ::__construct ();
43
27
}
44
28
@@ -60,27 +44,18 @@ protected function configure()
60
44
61
45
protected function execute (InputInterface $ input , OutputInterface $ output )
62
46
{
63
- $ manager = $ this ->managerFactory ->create ();
64
- $ dateTime = $ this ->dateTimeFactory ->create ();
65
-
66
- try {
67
- $ this ->state ->setAreaCode (Area::AREA_ADMINHTML );
68
- } catch (\Magento \Framework \Exception \LocalizedException $ exception ) {
69
- // Area code is already set
70
- }
71
-
72
- try {
73
- // lets create a new cron job and dispatch it
74
- $ jobCode = $ input ->getArgument (self ::INPUT_KEY_JOB_CODE );
75
- $ now = strftime ('%Y-%m-%dT%H:%M:%S ' , $ dateTime ->gmtTimestamp ());
76
-
77
- $ schedule = $ manager ->createCronJob ($ jobCode , $ now );
78
- $ manager ->dispatchCron (null , $ jobCode , $ schedule );
79
- $ output ->writeln ("$ jobCode successfully ran " );
80
- return Cli::RETURN_SUCCESS ;
81
- } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
82
- $ output ->writeln ($ e ->getMessage ());
83
- return Cli::RETURN_FAILURE ;
84
- }
47
+ /**
48
+ * @todo Find a way to avoid using `ObjectManager`
49
+ */
50
+ $ omParams = $ _SERVER ;
51
+ $ omParams [StoreManager::PARAM_RUN_CODE ] = Store::ADMIN_CODE ;
52
+ $ omParams [Store::CUSTOM_ENTRY_POINT_PARAM ] = true ;
53
+ $ objectManager = $ this ->objectManagerFactory ->create ($ omParams );
54
+
55
+ $ jobCode = $ input ->getArgument (self ::INPUT_KEY_JOB_CODE );
56
+ $ cron = $ objectManager ->create (\EthanYehuda \CronjobManager \Model \Cron \Runner::class);
57
+ list ($ resultCode , $ resultMessage ) = $ cron ->runCron ($ jobCode );
58
+ $ output ->writeln ($ resultMessage );
59
+ return $ resultCode ;
85
60
}
86
61
}
0 commit comments