Skip to content

Commit f38e35f

Browse files
dhufjrenggli
authored andcommitted
[BUGFIX] Ignore errors in CloudinaryScanCommand
1 parent a97d7d1 commit f38e35f

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

Classes/Command/CloudinaryScanCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8888
);
8989
}
9090

91-
$message = "Statistics for files: \n\n- created: %s\n- updated: %s\n- total: %s\n- deleted: %s";
91+
$message = "Statistics for files: \n\n- created: %s\n- updated: %s\n- total: %s\n- deleted: %s\n- failed: %s";
9292
$message .= "\n\nStatistics for folders: \n\n- deleted: %s";
9393
$this->success($message, [
9494
$result['created'],
9595
$result['updated'],
9696
$result['total'],
9797
$result['deleted'],
98+
$result['failed'],
9899
$result['folder_deleted'],
99100
]);
100101

Classes/Services/CloudinaryScanService.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CloudinaryScanService
3030
private const UPDATED = 'updated';
3131
private const DELETED = 'deleted';
3232
private const TOTAL = 'total';
33+
private const FAILED = 'failed';
3334
private const FOLDER_DELETED = 'folder_deleted';
3435

3536
/**
@@ -55,6 +56,7 @@ class CloudinaryScanService
5556
self::UPDATED => 0,
5657
self::DELETED => 0,
5758
self::TOTAL => 0,
59+
self::FAILED => 0,
5860

5961
self::FOLDER_DELETED => 0,
6062
];
@@ -143,34 +145,42 @@ public function scan(): array
143145

144146
if (is_array($response['resources'])) {
145147
foreach ($response['resources'] as $resource) {
146-
147148
$fileIdentifier = $this->getCloudinaryPathService()->computeFileIdentifier($resource);
148-
if ($this->io) {
149-
$this->io->writeln($fileIdentifier);
150-
}
149+
try {
150+
if ($this->io) {
151+
$this->io->writeln($fileIdentifier);
152+
}
151153

152-
// Save mirrored file
153-
$result = $this->getCloudinaryResourceService()->save($resource);
154+
// Save mirrored file
155+
$result = $this->getCloudinaryResourceService()->save($resource);
154156

155-
// Find if the file exists in sys_file already
156-
if (!$this->fileExistsInStorage($fileIdentifier)) {
157+
// Find if the file exists in sys_file already
158+
if (!$this->fileExistsInStorage($fileIdentifier)) {
157159

158-
if ($this->io) {
159-
$this->io->writeln('Indexing new file: ' . $fileIdentifier);
160-
$this->io->writeln('');
161-
}
160+
if ($this->io) {
161+
$this->io->writeln('Indexing new file: ' . $fileIdentifier);
162+
$this->io->writeln('');
163+
}
162164

163-
// This will trigger a file indexation
164-
$this->storage->getFile($fileIdentifier);
165-
}
165+
// This will trigger a file indexation
166+
$this->storage->getFile($fileIdentifier);
167+
}
166168

167-
// For the stats, we collect the number of files touched
168-
$key = key($result);
169-
$this->statistics[$key] += $result[$key];
169+
// For the stats, we collect the number of files touched
170+
$key = key($result);
171+
$this->statistics[$key] += $result[$key];
170172

171-
// In any case we can add a file to the counter.
172-
// Later we can verify the total corresponds to the "created" + "updated" + "deleted" files
173-
$this->statistics[self::TOTAL]++;
173+
// In any case we can add a file to the counter.
174+
// Later we can verify the total corresponds to the "created" + "updated" + "deleted" files
175+
$this->statistics[self::TOTAL]++;
176+
}
177+
catch (\Exception $e) {
178+
$this->statistics[self::FAILED]++;
179+
if ($this->io) {
180+
$this->io->warning(sprintf('Error could not process "%s"', $fileIdentifier));
181+
}
182+
// ignore
183+
}
174184
}
175185
}
176186
} while (!empty($response) && isset($response['next_cursor']));

0 commit comments

Comments
 (0)