Skip to content

Commit

Permalink
raise SQL default timeout to 70s avoiding MySQL server has gone away
Browse files Browse the repository at this point in the history
  • Loading branch information
WengerK committed Apr 26, 2024
1 parent 7a54542 commit f5a8864
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/entity_to_text_tika/drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ services:
entity_to_text_tika.ocr_warmup_command:
class: \Drupal\entity_to_text_tika\Commands\OcrWarmupCommand
arguments:
- '@database'
- '@entity_type.manager'
- '@entity_to_text_tika.extractor.file_to_text'
- '@entity_to_text_tika.storage.local_file'
Expand Down
17 changes: 16 additions & 1 deletion modules/entity_to_text_tika/src/Commands/OcrWarmupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\entity_to_text_tika\Commands;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\entity_to_text_tika\Extractor\FileToText;
use Drupal\entity_to_text_tika\Storage\StorageInterface;
Expand All @@ -21,6 +22,13 @@ class OcrWarmupCommand extends DrushCommands {
*/
public const LIMIT_PAGER = 100;

/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* The file storage service.
*
Expand All @@ -45,7 +53,8 @@ class OcrWarmupCommand extends DrushCommands {
/**
* Warmup OCR caches for Tika constructor.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, FileToText $file_to_text, StorageInterface $local_storage) {
public function __construct(Connection $connection, EntityTypeManagerInterface $entity_type_manager, FileToText $file_to_text, StorageInterface $local_storage) {
$this->connection = $connection;
$this->fileStorage = $entity_type_manager->getStorage('file');
$this->fileToText = $file_to_text;
$this->localFileStorage = $local_storage;
Expand Down Expand Up @@ -121,6 +130,12 @@ public function warmup(
$force = (bool) $options['force'];
$dry_run = (bool) $options['dry-run'];

// Raise the database connection timeout to 70 seconds.
// Tika default timeout is 60 seconds therefore having a lower value
// for database timeout may lead to MySQL server has gone away.
// @see \Drupal\entity_to_text_tika\Extractor\FileToText::getClient().
$this->connection->query('SET wait_timeout = 70');

$query = $this->fileStorage->getQuery();
$query->accessCheck(FALSE);
if ($fid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\Tests\entity_to_text_tika\Unit;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
Expand Down Expand Up @@ -58,6 +59,11 @@ final class OcrWarmupCommandTest extends UnitTestCase {
protected function setUp(): void {
parent::setUp();

$database = $this->createMock(Connection::class);
$database->expects(self::once())
->method('query')
->with('SET wait_timeout = 70');

$this->fileStorage = $this->createMock(EntityStorageInterface::class);
$entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
$entity_type_manager->expects(self::once())
Expand All @@ -69,6 +75,7 @@ protected function setUp(): void {
$this->fileToText = $this->createMock(FileToText::class);

$this->warmupCommand = new OcrWarmupCommand(
$database,
$entity_type_manager,
$this->fileToText,
$this->localFileStorage,
Expand Down

0 comments on commit f5a8864

Please sign in to comment.