This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle files index via async job (#9)
* #54 feat: handle files index via async job * chore: add annotations
- Loading branch information
1 parent
22aa221
commit c7f4b02
Showing
5 changed files
with
204 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Chatlas BEdita plugin | ||
* | ||
* Copyright 2023 Atlas Srl | ||
*/ | ||
namespace BEdita\Chatlas\Job\Service; | ||
|
||
use BEdita\Chatlas\Index\CollectionHandler; | ||
use BEdita\Core\Job\JobService; | ||
use Cake\Log\LogTrait; | ||
use Cake\ORM\Locator\LocatorAwareTrait; | ||
use Throwable; | ||
|
||
class IndexFileService implements JobService | ||
{ | ||
use LocatorAwareTrait; | ||
use LogTrait; | ||
|
||
/** | ||
* Run an async job using $payload input data and optional $options. | ||
* | ||
* It can return: | ||
* - a boolean i.e. `true` on success, `false` on failure | ||
* - an array with keys: | ||
* - 'success' (required) => `true` on success, `false` on failure | ||
* - 'messages' (optional) => array of messages | ||
* | ||
* @param array $payload Input data for running this job. | ||
* @param array $options Options for running this job. | ||
* @return bool | ||
*/ | ||
public function run(array $payload, array $options = []): bool | ||
{ | ||
$handler = new CollectionHandler(); | ||
try { | ||
/** @var \BEdita\Core\Model\Entity\ObjectEntity $collection */ | ||
$collection = $this->fetchTable('Collections')->get($payload['collection_id']); | ||
/** @var \BEdita\Core\Model\Entity\ObjectEntity $file */ | ||
$file = $this->fetchTable('Files')->get($payload['file_id']); | ||
$handler->uploadDocument($collection, $file); | ||
|
||
return true; | ||
} catch (Throwable $th) { | ||
$this->log(sprintf('IndexFile async job error - %s', $th->getMessage()), 'error'); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace BEdita\Chatlas\Test\TestCase\Job\Service; | ||
|
||
use BEdita\Chatlas\Job\Service\IndexFileService; | ||
use BEdita\Chatlas\Test\TestMockTrait; | ||
use BEdita\Core\Filesystem\FilesystemRegistry; | ||
use BEdita\Core\Model\Entity\ObjectEntity; | ||
use BEdita\Core\Model\Entity\Stream; | ||
use Cake\TestSuite\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \BEdita\Chatlas\Job\Service\IndexFileServiceTest | ||
*/ | ||
class IndexFileServiceTest extends TestCase | ||
{ | ||
use TestMockTrait; | ||
|
||
/** | ||
* Test `run()` method. | ||
* | ||
* @return void | ||
* @covers ::run() | ||
*/ | ||
public function testRun(): void | ||
{ | ||
FilesystemRegistry::dropAll(); | ||
FilesystemRegistry::setConfig([ | ||
'default' => [ | ||
'className' => 'BEdita/Core.Local', | ||
'path' => './tests' . DS . 'uploads', | ||
], | ||
]); | ||
|
||
$stream = new Stream(); | ||
$stream->set('uri', 'default://test.txt', ['guard' => false]); | ||
$stream->set('file_size', 1, ['guard' => false]); | ||
$file = $this->mockEntity('files', [ | ||
'index_updated' => null, | ||
'status' => 'on', | ||
'streams' => [$stream], | ||
]); | ||
$this->mockClientResponse(); | ||
$this->mockTable('Collections', new ObjectEntity()); | ||
$this->mockTable('Files', $file); | ||
|
||
$service = new IndexFileService(); | ||
$success = $service->run([ | ||
'collection_id' => 1, | ||
'file_id' => 2, | ||
]); | ||
static::assertTrue($success); | ||
static::assertNotEmpty($file->get('index_updated')); | ||
static::assertEquals('done', $file->get('index_status')); | ||
} | ||
|
||
/** | ||
* Test `run()` method with failure. | ||
* | ||
* @return void | ||
* @covers ::run() | ||
*/ | ||
public function testFailure(): void | ||
{ | ||
$service = new IndexFileService(); | ||
$success = $service->run([ | ||
'collection_id' => 1, | ||
'file_id' => 2, | ||
]); | ||
static::assertFalse($success); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters