Skip to content

Commit

Permalink
Merge pull request #33 from WeareJH/lock-info
Browse files Browse the repository at this point in the history
Display lock info and create command to list locks
  • Loading branch information
AydinHassan authored Jun 24, 2020
2 parents 2881de6 + a40586c commit 80700ad
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 31 deletions.
15 changes: 14 additions & 1 deletion src/Block/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Jh\Import\Config;
use Jh\Import\Config\Data;
use Jh\Import\Locker\Locker;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;

Expand All @@ -29,22 +30,34 @@ class Info extends Template
*/
private $cronConfig;

/**
* @var Locker
*/
private $locker;

public function __construct(
Context $context,
Data $config,
\Magento\Cron\Model\Config $cronConfig
\Magento\Cron\Model\Config $cronConfig,
Locker $locker
) {
parent::__construct($context);

$this->config = $config;
$this->cronConfig = $cronConfig;
$this->locker = $locker;
}

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

public function getLockStatus(): string
{
return $this->locker->locked($this->getImport()->getImportName()) ? 'Locked' : 'Not locked';
}

protected function _prepareLayout() //@codingStandardsIgnoreLine
{
$importType = $this->getImport()->getType();
Expand Down
12 changes: 10 additions & 2 deletions src/Command/ListImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jh\Import\Command;

use Jh\Import\Config\Data;
use Jh\Import\Locker\Locker;
use Magento\Cron\Model\Config;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
Expand All @@ -24,10 +25,16 @@ class ListImportsCommand extends Command
*/
private $cronConfig;

public function __construct(Data $importConfig, Config $cronConfig)
/**
* @var Locker
*/
private $locker;

public function __construct(Data $importConfig, Config $cronConfig, Locker $locker)
{
$this->importConfig = $importConfig;
$this->cronConfig = $cronConfig;
$this->locker = $locker;
parent::__construct();
}

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

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

Expand All @@ -65,6 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$config->get('match_files'),
$config->get('incoming_directory'),
$cron,
$this->locker->locked($import) ? '<error>Yes</error>' : 'No'
];
}, $this->importConfig->getAllImportNames()))
->render();
Expand Down
67 changes: 67 additions & 0 deletions src/Command/ViewLocksCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Jh\Import\Command;

use Jh\Import\Config\Data;
use Jh\Import\Locker\Locker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ViewLocksCommand extends Command
{
/**
* @var Data
*/
private $importConfig;

/**
* @var Locker
*/
private $locker;

public function __construct(Data $importConfig, Locker $locker)
{
$this->importConfig = $importConfig;
$this->locker = $locker;
parent::__construct();
}

protected function configure()
{
$this->setName('jh-import:locks')
->setDescription('Show current locks');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$locks = array_map(
function (string $importName) {
return [$importName];
},
array_filter(
$this->importConfig->getAllImportNames(),
function (string $importName) {
return $this->locker->locked($importName);
}
)
);

if (empty($locks)) {
$output->writeln(['', '<comment>No import is locked</comment>', '']);
return;
}

$output->writeln('');
$output->writeln('<comment>All locked imports:</comment>');
$output->writeln('');

(new Table($output))
->setHeaders(['Locks'])
->setRows($locks)
->render();

$output->writeln('');
}
}
4 changes: 3 additions & 1 deletion src/Ui/Component/Listing/ImportSearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jh\Import\Ui\Component\Listing;

use Jh\Import\Config\Data;
use Jh\Import\Locker\Locker;
use Magento\Framework\Api;
use Magento\Framework\Api\Search\AggregationInterface;
use Magento\Framework\Api\Search\DocumentInterface;
Expand Down Expand Up @@ -31,7 +32,7 @@ class ImportSearchResult extends Collection implements SearchResultInterface
*/
private $totalCount;

public function __construct(Collection\EntityFactoryInterface $entityFactory, Data $config)
public function __construct(Collection\EntityFactoryInterface $entityFactory, Data $config, Locker $locker)
{
parent::__construct($entityFactory);
$this->setItemObjectClass(Document::class);
Expand All @@ -44,6 +45,7 @@ public function __construct(Collection\EntityFactoryInterface $entityFactory, Da
$item = $this->getNewEmptyItem();
$item->setData($importConfig->all());
$item->setData('name', $importName);
$item->setData('lock_status', $locker->locked($importName) ? 'Locked' : 'Not locked');

$this->addItem($item);
}
Expand Down
1 change: 1 addition & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<item name="import_run" xsi:type="object">Jh\Import\Command\RunImportCommand</item>
<item name="import_view_log" xsi:type="object">Jh\Import\Command\ViewLogsCommand</item>
<item name="import_unlock" xsi:type="object">Jh\Import\Command\UnlockImportCommand</item>
<item name="import_locks" xsi:type="object">Jh\Import\Command\ViewLocksCommand</item>
<item name="import_clear_last_log" xsi:type="object">Jh\Import\Command\ClearLastImportLogCommand</item>
</argument>
</arguments>
Expand Down
4 changes: 4 additions & 0 deletions src/view/adminhtml/templates/import_info.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<div class="dashboard-item-title"><?= __('Import Type') ?></div>
<?= $this->getImport()->get('type') ?>
</div>
<div class="dashboard-item">
<div class="dashboard-item-title"><?= __('Lock Status') ?></div>
<?= $this->getLockStatus() ?>
</div>
<div class="dashboard-item">
<div class="dashboard-item-title"><?= __('Indexers') ?></div>
<?= implode(",\n", $this->getImport()->get('indexers')) ?>
Expand Down
11 changes: 10 additions & 1 deletion src/view/adminhtml/ui_component/import_listing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
</argument>
</column>

<column name="lock_status">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Lock Status</item>
<item name="sortOrder" xsi:type="number">70</item>
</item>
</argument>
</column>

<actionsColumn name="actions" class="Jh\Import\Ui\Component\Listing\Column\ImportConfigActions">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
Expand All @@ -80,4 +89,4 @@
</argument>
</actionsColumn>
</columns>
</listing>
</listing>
Loading

0 comments on commit 80700ad

Please sign in to comment.