Skip to content

Commit

Permalink
added ability to schedule feeds outside specific time
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Nov 15, 2023
1 parent 7841df4 commit e922767
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/model/product-feeds.md
Expand Up @@ -40,6 +40,10 @@ In such a case, this feed will be generated every four hours.
Feeds have their default times set in their own `services.yaml` files, but can be easily changed in your project's `feed.yaml` file.
You only need to copy service definition from feed `services.yaml` file and change the hours and minutes to expected ones.

## How to run feed outside scheduled time?

If you want to run feed outside scheduled time, you can use the `php bin/console shopsys:feed-schedule` command with `--feed-name` argument to schedule one feed or `--all` argument to schedule all feeds and then run `php bin/console shopsys:cron --module="Shopsys\FrameworkBundle\Model\Feed\FeedCronModule" --instance-name=export` to generate the feeds.

## How to limit a product feed to a specific domain?

If you want to limit a product feed to a specific domain, you can use the `domain_ids` parameter in the feed's service definition.
Expand Down
67 changes: 67 additions & 0 deletions packages/framework/src/Command/ScheduleFeedsCommand.php
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Command;

use Shopsys\FrameworkBundle\Model\Feed\FeedFacade;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'shopsys:feed-schedule')]
class ScheduleFeedsCommand extends Command
{
private const OPTION_FEED_NAME = 'feed-name';
private const OPTION_ALL = 'all';

/**
* @param \Shopsys\FrameworkBundle\Model\Feed\FeedFacade $feedFacade
*/
public function __construct(
protected readonly FeedFacade $feedFacade,
) {
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDescription('Schedule feeds to be generated in the next cron run.')
->addOption(self::OPTION_FEED_NAME, null, InputOption::VALUE_OPTIONAL, 'name of feed to be scheduled')
->addOption(self::OPTION_ALL, null, InputOption::VALUE_NONE, 'schedule all feeds');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$optionAll = $input->getOption(self::OPTION_ALL);
$optionFeedName = $input->getOption(self::OPTION_FEED_NAME);

$symfonyStyle = new SymfonyStyle($input, $output);

if ($optionAll === true) {
$symfonyStyle->info('Marking all feeds for scheduling...');
$this->feedFacade->scheduleAllFeeds();
} elseif ($optionFeedName !== null) {
$symfonyStyle->info('Marking feed for scheduling...');
$this->feedFacade->scheduleFeedByName($optionFeedName);
} else {
$symfonyStyle->error('You have to specify either --all or --feed-name option.');

return Command::FAILURE;
}

$symfonyStyle->success('Done!');

return Command::SUCCESS;
}
}
39 changes: 32 additions & 7 deletions packages/framework/src/Model/Feed/FeedFacade.php
Expand Up @@ -137,15 +137,24 @@ public function scheduleFeedsForCurrentTime(): void
{
$feedConfigsToSchedule = $this->feedRegistry->getFeedConfigsForCurrentTime();

foreach ($feedConfigsToSchedule as $feedConfig) {
$feedModules = $this->feedModuleRepository->getFeedModulesByConfig($feedConfig);
$this->markFeedConfigsForScheduling($feedConfigsToSchedule);
}

foreach ($feedModules as $feedModule) {
$feedModule->schedule();
}
}
public function scheduleAllFeeds(): void
{
$feedConfigsToSchedule = $this->feedRegistry->getAllFeedConfigs();

$this->em->flush();
$this->markFeedConfigsForScheduling($feedConfigsToSchedule);
}

/**
* @param string $name
*/
public function scheduleFeedByName(string $name): void
{
$feedConfigsToSchedule = [$this->feedRegistry->getFeedConfigByName($name)];

$this->markFeedConfigsForScheduling($feedConfigsToSchedule);
}

/**
Expand All @@ -156,4 +165,20 @@ public function markFeedModuleAsUnscheduled(FeedModule $feedModule): void
$feedModule->unschedule();
$this->em->flush();
}

/**
* @param array $feedConfigsToSchedule
*/
protected function markFeedConfigsForScheduling(array $feedConfigsToSchedule): void
{
foreach ($feedConfigsToSchedule as $feedConfig) {
$feedModules = $this->feedModuleRepository->getFeedModulesByConfig($feedConfig);

foreach ($feedModules as $feedModule) {
$feedModule->schedule();
}
}

$this->em->flush();
}
}

0 comments on commit e922767

Please sign in to comment.