1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Bridge\WorkerBundle\Executor; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException; |
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
use TreeHouse\IoBundle\Entity\Feed; |
10
|
|
|
use TreeHouse\IoBundle\Import\ImportFactory; |
11
|
|
|
use TreeHouse\IoBundle\Import\ImportScheduler; |
12
|
|
|
use TreeHouse\WorkerBundle\Executor\AbstractExecutor; |
13
|
|
|
use TreeHouse\WorkerBundle\Executor\ObjectPayloadInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Schedules an import. |
17
|
|
|
*/ |
18
|
|
|
class ImportScheduleExecutor extends AbstractExecutor implements ObjectPayloadInterface |
19
|
|
|
{ |
20
|
|
|
const NAME = 'import.schedule'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ImportScheduler |
24
|
|
|
*/ |
25
|
|
|
protected $scheduler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ImportFactory |
29
|
|
|
*/ |
30
|
|
|
protected $importFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LoggerInterface |
34
|
|
|
*/ |
35
|
|
|
protected $logger; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ImportScheduler $scheduler |
39
|
|
|
* @param ImportFactory $importFactory |
40
|
|
|
* @param LoggerInterface $logger |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ImportScheduler $scheduler, ImportFactory $importFactory, LoggerInterface $logger) |
43
|
|
|
{ |
44
|
|
|
$this->scheduler = $scheduler; |
45
|
|
|
$this->importFactory = $importFactory; |
46
|
|
|
$this->logger = $logger; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getName() |
53
|
|
|
{ |
54
|
|
|
return self::NAME; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function getObjectPayload($object) |
61
|
|
|
{ |
62
|
|
|
return [$object->getId()]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function supportsObject($object) |
69
|
|
|
{ |
70
|
|
|
return $object instanceof Feed; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function configurePayload(OptionsResolver $resolver) |
77
|
|
|
{ |
78
|
|
|
$resolver->setRequired(0); |
79
|
|
|
$resolver->setAllowedTypes(0, 'numeric'); |
80
|
|
|
$resolver->setNormalizer(0, function (Options $options, $value) { |
81
|
|
|
if (null === $listing = $this->scheduler->findFeed($value)) { |
82
|
|
|
throw new InvalidArgumentException(sprintf('Feed with id "%d" does not exist', $value)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $listing; |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
$resolver->setDefaults([1 => false]); |
89
|
|
|
$resolver->setNormalizer(1, function (Options $options, $value) { |
90
|
|
|
return (boolean) $value; |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function execute(array $payload) |
98
|
|
|
{ |
99
|
|
|
/** @var Feed $feed */ |
100
|
|
|
/** @var bool $force */ |
101
|
|
|
list($feed, $force) = $payload; |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
$import = $this->importFactory->createImport($feed, new \DateTime(), $force); |
105
|
|
|
|
106
|
|
|
$this->logger->info(sprintf('Created import %d', $import->getId())); |
107
|
|
|
|
108
|
|
|
$this->logger->debug('Scheduling parts'); |
109
|
|
|
foreach ($import->getParts() as $part) { |
110
|
|
|
$this->scheduler->schedulePart($part); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} catch (\Exception $e) { |
115
|
|
|
// we could get an exception when a new import cannot be created, for example when an existing import |
116
|
|
|
// for this feed is still running. |
117
|
|
|
$this->logger->error(sprintf('<error>%s</error>', $e->getMessage())); |
118
|
|
|
|
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|