Skip to content

Commit 80700ad

Browse files
authored
Merge pull request #33 from WeareJH/lock-info
Display lock info and create command to list locks
2 parents 2881de6 + a40586c commit 80700ad

File tree

9 files changed

+241
-31
lines changed

9 files changed

+241
-31
lines changed

src/Block/Info.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Jh\Import\Config;
66
use Jh\Import\Config\Data;
7+
use Jh\Import\Locker\Locker;
78
use Magento\Backend\Block\Template;
89
use Magento\Backend\Block\Template\Context;
910

@@ -29,22 +30,34 @@ class Info extends Template
2930
*/
3031
private $cronConfig;
3132

33+
/**
34+
* @var Locker
35+
*/
36+
private $locker;
37+
3238
public function __construct(
3339
Context $context,
3440
Data $config,
35-
\Magento\Cron\Model\Config $cronConfig
41+
\Magento\Cron\Model\Config $cronConfig,
42+
Locker $locker
3643
) {
3744
parent::__construct($context);
3845

3946
$this->config = $config;
4047
$this->cronConfig = $cronConfig;
48+
$this->locker = $locker;
4149
}
4250

4351
public function getImport(): Config
4452
{
4553
return $this->config->getImportConfigByName($this->getRequest()->getParam('name'));
4654
}
4755

56+
public function getLockStatus(): string
57+
{
58+
return $this->locker->locked($this->getImport()->getImportName()) ? 'Locked' : 'Not locked';
59+
}
60+
4861
protected function _prepareLayout() //@codingStandardsIgnoreLine
4962
{
5063
$importType = $this->getImport()->getType();

src/Command/ListImportsCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jh\Import\Command;
44

55
use Jh\Import\Config\Data;
6+
use Jh\Import\Locker\Locker;
67
use Magento\Cron\Model\Config;
78
use Symfony\Component\Console\Command\Command;
89
use Symfony\Component\Console\Helper\Table;
@@ -24,10 +25,16 @@ class ListImportsCommand extends Command
2425
*/
2526
private $cronConfig;
2627

27-
public function __construct(Data $importConfig, Config $cronConfig)
28+
/**
29+
* @var Locker
30+
*/
31+
private $locker;
32+
33+
public function __construct(Data $importConfig, Config $cronConfig, Locker $locker)
2834
{
2935
$this->importConfig = $importConfig;
3036
$this->cronConfig = $cronConfig;
37+
$this->locker = $locker;
3138
parent::__construct();
3239
}
3340

@@ -49,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4956
$jobs = $this->cronConfig->getJobs();
5057

5158
(new Table($output))
52-
->setHeaders(['Name', 'Type', 'Match Files', 'Incoming Directory', 'Cron Expr'])
59+
->setHeaders(['Name', 'Type', 'Match Files', 'Incoming Directory', 'Cron Expr', 'Locked?'])
5360
->setRows(array_map(function ($import) use ($jobs) {
5461
$config = $this->importConfig->getImportConfigByName($import);
5562

@@ -65,6 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6572
$config->get('match_files'),
6673
$config->get('incoming_directory'),
6774
$cron,
75+
$this->locker->locked($import) ? '<error>Yes</error>' : 'No'
6876
];
6977
}, $this->importConfig->getAllImportNames()))
7078
->render();

src/Command/ViewLocksCommand.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Jh\Import\Command;
4+
5+
use Jh\Import\Config\Data;
6+
use Jh\Import\Locker\Locker;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Helper\Table;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class ViewLocksCommand extends Command
13+
{
14+
/**
15+
* @var Data
16+
*/
17+
private $importConfig;
18+
19+
/**
20+
* @var Locker
21+
*/
22+
private $locker;
23+
24+
public function __construct(Data $importConfig, Locker $locker)
25+
{
26+
$this->importConfig = $importConfig;
27+
$this->locker = $locker;
28+
parent::__construct();
29+
}
30+
31+
protected function configure()
32+
{
33+
$this->setName('jh-import:locks')
34+
->setDescription('Show current locks');
35+
}
36+
37+
protected function execute(InputInterface $input, OutputInterface $output)
38+
{
39+
$locks = array_map(
40+
function (string $importName) {
41+
return [$importName];
42+
},
43+
array_filter(
44+
$this->importConfig->getAllImportNames(),
45+
function (string $importName) {
46+
return $this->locker->locked($importName);
47+
}
48+
)
49+
);
50+
51+
if (empty($locks)) {
52+
$output->writeln(['', '<comment>No import is locked</comment>', '']);
53+
return;
54+
}
55+
56+
$output->writeln('');
57+
$output->writeln('<comment>All locked imports:</comment>');
58+
$output->writeln('');
59+
60+
(new Table($output))
61+
->setHeaders(['Locks'])
62+
->setRows($locks)
63+
->render();
64+
65+
$output->writeln('');
66+
}
67+
}

src/Ui/Component/Listing/ImportSearchResult.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jh\Import\Ui\Component\Listing;
44

55
use Jh\Import\Config\Data;
6+
use Jh\Import\Locker\Locker;
67
use Magento\Framework\Api;
78
use Magento\Framework\Api\Search\AggregationInterface;
89
use Magento\Framework\Api\Search\DocumentInterface;
@@ -31,7 +32,7 @@ class ImportSearchResult extends Collection implements SearchResultInterface
3132
*/
3233
private $totalCount;
3334

34-
public function __construct(Collection\EntityFactoryInterface $entityFactory, Data $config)
35+
public function __construct(Collection\EntityFactoryInterface $entityFactory, Data $config, Locker $locker)
3536
{
3637
parent::__construct($entityFactory);
3738
$this->setItemObjectClass(Document::class);
@@ -44,6 +45,7 @@ public function __construct(Collection\EntityFactoryInterface $entityFactory, Da
4445
$item = $this->getNewEmptyItem();
4546
$item->setData($importConfig->all());
4647
$item->setData('name', $importName);
48+
$item->setData('lock_status', $locker->locked($importName) ? 'Locked' : 'Not locked');
4749

4850
$this->addItem($item);
4951
}

src/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<item name="import_run" xsi:type="object">Jh\Import\Command\RunImportCommand</item>
2929
<item name="import_view_log" xsi:type="object">Jh\Import\Command\ViewLogsCommand</item>
3030
<item name="import_unlock" xsi:type="object">Jh\Import\Command\UnlockImportCommand</item>
31+
<item name="import_locks" xsi:type="object">Jh\Import\Command\ViewLocksCommand</item>
3132
<item name="import_clear_last_log" xsi:type="object">Jh\Import\Command\ClearLastImportLogCommand</item>
3233
</argument>
3334
</arguments>

src/view/adminhtml/templates/import_info.phtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<div class="dashboard-item-title"><?= __('Import Type') ?></div>
99
<?= $this->getImport()->get('type') ?>
1010
</div>
11+
<div class="dashboard-item">
12+
<div class="dashboard-item-title"><?= __('Lock Status') ?></div>
13+
<?= $this->getLockStatus() ?>
14+
</div>
1115
<div class="dashboard-item">
1216
<div class="dashboard-item-title"><?= __('Indexers') ?></div>
1317
<?= implode(",\n", $this->getImport()->get('indexers')) ?>

src/view/adminhtml/ui_component/import_listing.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@
7171
</argument>
7272
</column>
7373

74+
<column name="lock_status">
75+
<argument name="data" xsi:type="array">
76+
<item name="config" xsi:type="array">
77+
<item name="label" xsi:type="string" translate="true">Lock Status</item>
78+
<item name="sortOrder" xsi:type="number">70</item>
79+
</item>
80+
</argument>
81+
</column>
82+
7483
<actionsColumn name="actions" class="Jh\Import\Ui\Component\Listing\Column\ImportConfigActions">
7584
<argument name="data" xsi:type="array">
7685
<item name="config" xsi:type="array">
@@ -80,4 +89,4 @@
8089
</argument>
8190
</actionsColumn>
8291
</columns>
83-
</listing>
92+
</listing>

0 commit comments

Comments
 (0)