1

i have one Problem that i cant understand. I have a Cronjob that should read in the Env.php via DeploymentConfig. When i add DeploymentConfig in the Job __construct, the Job wont start.

Doesnt start:

public function __construct(
    Context $context,
    MarketCollectionFactory $marketCollectionFactory,
    MarketFactory $marketFactory,
    SynchronizableFactory $modelFactory,
    ResourceConnection $resourceConnection,
    array $data = [],
    DeploymentConfig $deploymentConfig
) {
        ~~~
}

Does start:

public function __construct(
    Context $context,
    MarketCollectionFactory $marketCollectionFactory,
    MarketFactory $marketFactory,
    SynchronizableFactory $modelFactory,
    ResourceConnection $resourceConnection,
    array $data = []
) {
        ~~~
}

Could it be, that i cant inject it in Cron Jobs. If yes, is there an other way to access the env.php?

Greetings

Edit:

Here is the full file:

namespace Addons\Markets\cron;
use Addons\Markets\Block\Markets;
use \Magento\Framework\View\Element\Template\Context;
use \Addons\Markets\Model\ResourceModel\Markets\CollectionFactory as MarketCollectionFactory;
use \Addons\Markets\Model\MarketFactory as MarketFactory;
use \Addons\Synchronizer\Model\SynchronizableFactory;
use Magento\Framework\App\ResourceConnection;
use \Magento\Framework\App\DeploymentConfig;

use \Zend\Log\Writer\Stream;
use \Zend\Log\Logger;

class Job
{

private $market;

public function __construct(
    Context $context,
    MarketCollectionFactory $marketCollectionFactory,
    MarketFactory $marketFactory,
    SynchronizableFactory $modelFactory,
    ResourceConnection $resourceConnection,
    array $data = [],
    DeploymentConfig $deploymentConfig
) {
        $this->market = new Markets(
        $context,
        $marketCollectionFactory, 
        $marketFactory, 
        $modelFactory, 
        $resourceConnection,
        $data, 
        $deploymentConfig);
}

public function execute()
{
    $writer = new Stream(BP . '/var/log/cron.log');
    $logger = new Logger();
    $logger->addWriter($writer);
    $logger->info('Job Started');
    $this->market->synchronize();
}
}
  • What data do you want from env.php? – Kamlesh Solanki Apr 30 at 7:13
  • I have a part with information to access an extern backend. It looks like: ... 'sync' => [ 'url' => 'ZZZ', 'user' => 'YYY', 'pw' => 'XXX' ], ... – user3541236 Apr 30 at 7:22
  • Please post whole code for file – Kamlesh Solanki Apr 30 at 7:37
  • I edited the main post. I've tested the synchronize Code in a Controller and it works. Also i tested the Code without the DeploymentConfig. As soon the DeploymentConfig is in the Constructor, the Job wouldnt start. In the the exception.log is no entry about this. – user3541236 Apr 30 at 7:53
  • @user3541236 have you also check with the correct order of parameters mentioned in my answer? – Philipp Sander Apr 30 at 7:57
0

You need to correct the order of the parameters. $data is optional and has a default and must therefor be in the last place

public function __construct(
    Context $context,
    MarketCollectionFactory $marketCollectionFactory,
    MarketFactory $marketFactory,
    SynchronizableFactory $modelFactory,
    ResourceConnection $resourceConnection,
    DeploymentConfig $deploymentConfig,
    array $data = []
) {
        ~~~
}
  • I think that was the problem. I changed the order and it works. Thank you! – user3541236 Apr 30 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.