Skip to content

Commit

Permalink
add working daily cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
michb committed Nov 25, 2021
1 parent 747e293 commit 75817a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/Cron/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
namespace numero2\MobileDeAdsBundle\Cron;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\System;
use numero2\MobileDeAdsBundle\EventListener\Importer\AdListener;
use Contao\CoreBundle\ServiceAnnotation\CronJob;


/**
Expand All @@ -21,11 +24,21 @@
class Cron {


/**
* @var ContaoFramework
*/
private $framework;

/**
* @var AdListener
*/
private $adListener;


public function __construct( ContaoFramework $framework, AdListener $adListener ) {

public function __construct( ContaoFramework $framework ) {
$this->framework = $framework;
$this->adListener = $adListener;
}


Expand All @@ -35,5 +48,16 @@ public function __construct( ContaoFramework $framework ) {
public function __invoke( string $scope ): void {

$this->framework->initialize();

/** @var System $system */
$system = $this->framework->getAdapter(System::class);

$system->log('Daily cron for importing ads from mobile.de started', __METHOD__, TL_CRON);

if( $this->adListener->importAds() ) {
$system->log('Import successfully finished.', __METHOD__, TL_CRON);
} else {
$system->log('No ads found to import.', __METHOD__, TL_CRON);
}
}
}
4 changes: 2 additions & 2 deletions src/EventListener/Importer/AdListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ public function importAds(): bool {
$this->importAd($aAd);
}

System::log(sprintf('Imported %d ads from mobile.de',count($aAds)), __METHOD__, TL_CRON);
System::log(sprintf('Imported %d ads from mobile.de', count($aAds)), __METHOD__, TL_CRON);

return true;
}

System::log(sprintf('Could not find any ads to import from mobile.de',count($aAds)), __METHOD__, TL_ERROR);
System::log('Could not find any ads to import from mobile.de', __METHOD__, TL_ERROR);

return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ services:
numero2\MobileDeAdsBundle\Cron\Cron:
arguments:
- '@contao.framework'
- '@numero2_mobile.listener.import.ad'

0 comments on commit 75817a4

Please sign in to comment.