Skip to content

Commit

Permalink
[CSMS-2443] Full cron based (#34)
Browse files Browse the repository at this point in the history
[CSMS-2443] Disable queues. Full cron based
  • Loading branch information
mediclab authored Aug 28, 2019
1 parent 0f5dfb0 commit 765d2aa
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 760 deletions.
2 changes: 2 additions & 0 deletions inc/configs/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ parameters:
- "smartcat_debug_mode"
- "smartcat_events_enabled"
- "smartcat_regform_showed"
- "last_cron_send"
- "last_cron_check"
#Manual update statistics switch
- "statistic_queue_active"
#Сallback Authorize code
Expand Down
16 changes: 0 additions & 16 deletions inc/configs/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ services:
class: SmartCAT\WP\Cron\ClearDeletedProject
tags: [installable, cron]

core.queue.callback:
class: SmartCAT\WP\Queue\Callback
tags: [queue]

core.queue.statistic:
class: SmartCAT\WP\Queue\Statistic
tags: [queue]

core.queue.publication:
class: SmartCAT\WP\Queue\Publication
tags: [queue]

core.queue.post:
class: SmartCAT\WP\Queue\CreatePost
tags: [queue]

core.admin.columns:
class: SmartCAT\WP\Admin\AdditionalActions
tags: [hook]
Expand Down
20 changes: 0 additions & 20 deletions inc/smartcat/Admin/StatisticsAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Psr\Container\ContainerInterface;
use SmartCAT\WP\Connector;
use SmartCAT\WP\DB\Repository\StatisticRepository;
use SmartCAT\WP\Queue\Statistic;
use SmartCAT\WP\WP\HookInterface;
use SmartCAT\WP\WP\Options;

Expand Down Expand Up @@ -56,25 +55,6 @@ static public function start_refresh_statistic() {

if ( ! $options->get( 'statistic_queue_active' ) ) {

/** @var StatisticRepository $statistic_repository */
$statistic_repository = $container->get( 'entity.repository.statistic' );
$statistics = $statistic_repository->get_sended();

if ( count( $statistics ) > 0 ) {
$options->set( 'statistic_queue_active', true );
/** @var Statistic $queue */
$queue = $container->get( 'core.queue.statistic' );
foreach ( $statistics as $statistic ) {
if ( $statistic->get_error_count() > 0 ) {
$statistic->set_error_count( 0 );
$statistic_repository->persist( $statistic );
}

$queue->push_to_queue( $statistic->get_document_id() );
}
$statistic_repository->flush();
$queue->save()->dispatch();
}
}

$ajax_response->send_success( 'ok' );
Expand Down
30 changes: 10 additions & 20 deletions inc/smartcat/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace SmartCAT\WP;

use Psr\Container\ContainerInterface;
use SmartCAT\WP\Cron\CronInterface;
use SmartCAT\WP\DB\Setup\SetupInterface;
use SmartCAT\WP\Helpers\SmartCAT;
use SmartCAT\WP\Helpers\Utils;
use SmartCAT\WP\Queue\QueueAbstract;
use SmartCAT\WP\WP\HookInterface;
use SmartCAT\WP\WP\InitInterface;
use SmartCAT\WP\WP\Notice;
Expand Down Expand Up @@ -81,16 +81,6 @@ private function register_hooks() {
}
}

/**
* @throws \Exception
*/
private function init_queue() {
$services = self::get_container()->findTaggedServiceIds( 'queue' );
foreach ( $services as $service => $tags ) {
$this->from_container( $service );
}
}

/**
* @throws \Exception
*/
Expand Down Expand Up @@ -138,14 +128,6 @@ public function plugin_deactivate() {
$object->plugin_deactivate();
}
}
// Stopping queues.
$hooks = self::get_container()->findTaggedServiceIds( 'queue' );
foreach ( $hooks as $hook => $tags ) {
$object = $this->from_container( $hook );
if ( $object instanceof QueueAbstract ) {
$object->cancel_process();
}
}
}

/**
Expand Down Expand Up @@ -190,7 +172,6 @@ static public function plugin_uninstall() {
* @throws \Exception
*/
public function plugin_init( $query ) {
$this->init_queue();
$hooks = self::get_container()->findTaggedServiceIds( 'initable' );
foreach ( $hooks as $hook => $tags ) {
$object = self::get_container()->get( $hook );
Expand Down Expand Up @@ -221,6 +202,15 @@ public function plugin_admin_notice( $query ) {
$notice->add_error( __( 'Smartcat credentials are incorrect. Login failed.', 'translation-connectors' ), false );
}
}

/** @var ContainerInterface */
$container = self::get_container();
/** @var Options $options */
$options = $container->get( 'core.options' );

if ( abs( time() - intval( $options->get( 'last_cron_send' ) ) ) > 600 || abs( time() - intval( $options->get( 'last_cron_check' ) ) ) > 600 ) {
$notice->add_warning( __( 'It looks like cron service is not working properly. Lag is more than 10 minutes. Please check it.', 'translation-connectors' ), false );
}
}
}

Expand Down
177 changes: 145 additions & 32 deletions inc/smartcat/Cron/CheckProjectsStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,34 @@

namespace SmartCAT\WP\Cron;

use Http\Client\Common\Exception\ClientErrorException;
use Psr\Container\ContainerInterface;
use SmartCat\Client\Model\ProjectModel;
use SmartCAT\WP\Connector;
use SmartCAT\WP\DB\Entity\Statistics;
use SmartCAT\WP\DB\Repository\StatisticRepository;
use SmartCAT\WP\DB\Repository\TaskRepository;
use SmartCAT\WP\Helpers\Logger;
use SmartCAT\WP\Helpers\ProjectManager;
use SmartCAT\WP\Helpers\SmartCAT;
use SmartCAT\WP\WP\Options;

/**
* Class CheckProjectsStatus
*
* @package SmartCAT\WP\Cron
*/
class CheckProjectsStatus extends CronAbstract {

/** @var SmartCAT $smartcat */
private $smartcat;
/** @var StatisticRepository $statistic_repository */
private $statistic_repository;
/** @var TaskRepository $task_repository */
private $task_repository;
/** @var Options */
private $options;

/**
* @return mixed
*/
Expand All @@ -45,61 +59,160 @@ public function run() {
return;
}

Logger::event( 'cron', 'Checking documents started' );

/** @var ContainerInterface $container */
$container = Connector::get_container();

/** @var StatisticRepository $statistic_repository */
$statistic_repository = $container->get( 'entity.repository.statistic' );
$this->smartcat = $container->get( 'smartcat' );
$this->options = $container->get( 'core.options' );
$this->statistic_repository = $container->get( 'entity.repository.statistic' );
$this->task_repository = $container->get( 'entity.repository.task' );

/** @var TaskRepository $task_repository */
$task_repository = $container->get( 'entity.repository.task' );
$this->options->set( 'last_cron_check', time() );

/** @var SmartCAT $smartcat */
$smartcat = $container->get( 'smartcat' );
Logger::event( 'cron', 'Checking documents started' );

$statistics = $statistic_repository->get_sended();
$statistics = $this->statistic_repository->get_by_status( [ Statistics::STATUS_SENDED, Statistics::STATUS_EXPORT ] );
$count = count( $statistics );

Logger::event( 'cron', "Find $count documents to check" );

foreach ( $statistics as $statistic ) {
if ( $statistic->get_status() !== Statistics::STATUS_SENDED ) {
continue;
}
try {
foreach ( $statistics as $statistic ) {
$task = $this->task_repository->get_one_by_id( $statistic->get_task_id() );
$project = $this->smartcat->getProjectManager()->projectGet( $task->get_project_id() );

if ( $this->canceled_check( $statistic, $project ) ) {
continue;
}

if ( $statistic->get_status() === Statistics::STATUS_EXPORT ) {
$this->create_post( $statistic );
continue;
}

$task = $task_repository->get_one_by_id( $statistic->get_task_id() );
$project = $smartcat->getProjectManager()->projectGet( $task->get_project_id() );
if ( $statistic->get_status() === Statistics::STATUS_SENDED ) {
$document = $this->smartcat->getDocumentManager()->documentGet(
[ 'documentId' => $statistic->get_document_id() ]
);

if ( $project->getStatus() === 'canceled' ) {
$stat_update = $statistic_repository->get_all_by( [ 'taskId' => $statistic->get_task_id() ] );
$stages = $document->getWorkflowStages();
$progress = 0;

foreach ( $stat_update as $stat ) {
$stat->set_status( Statistics::STATUS_CANCELED );
$statistic_repository->save( $stat );
foreach ( $stages as $stage ) {
$progress += $stage->getProgress();
}

$progress = round( $progress / count( $stages ), 2 );
$statistic->set_progress( $progress );
$this->statistic_repository->save( $statistic );

if ( $document->getStatus() === 'completed' ) {
$this->export_request( $statistic );
}
}
}
} catch (\Exception $e) {
Logger::error(
"Document {$statistic->get_document_id()}, main error",
"Message: {$e->getMessage()}"
);
}

continue;
Logger::event( 'cron', 'Checking documents ended' );
}

/**
* @param Statistics $statistic
* @param ProjectModel $project
*
* @return bool
*/
private function canceled_check( $statistic, $project ) {
if ( $project->getStatus() === 'canceled' ) {
$stat_update = $this->statistic_repository->get_all_by( [ 'taskId' => $statistic->get_task_id() ] );

foreach ( $stat_update as $stat ) {
$stat->set_status( Statistics::STATUS_CANCELED );
$this->statistic_repository->save( $stat );
}

$document = $smartcat->getDocumentManager()->documentGet(
[ 'documentId' => $statistic->get_document_id() ]
);
return true;
}

$stages = $document->getWorkflowStages();
$progress = 0;
return false;
}

foreach ( $stages as $stage ) {
$progress += $stage->getProgress();
/**
* @param Statistics $statistic
*
* @return void
*/
private function export_request( $statistic ) {
try {
Logger::event( 'exporting', "Export request '{$statistic->get_document_id()}'" );

$task = $this->smartcat->getDocumentExportManager()
->documentExportRequestExport( [ 'documentIds' => [ $statistic->get_document_id() ] ] );
if ( $task->getId() ) {
$statistic->set_export_id( $task->getId() );
$statistic->set_status( Statistics::STATUS_EXPORT );
$this->statistic_repository->update( $statistic );
}

$progress = round( $progress / count( $stages ), 2 );
$statistic->set_progress( $progress );
Logger::event( 'exporting', "Export request '{$statistic->get_document_id()}' done" );
} catch ( ClientErrorException $e ) {
if ( $e->getResponse()->getStatusCode() === 404 ) {
$this->statistic_repository->delete( $statistic );
Logger::event( 'exporting', "Deleted '{$statistic->get_document_id()}'" );
} else {
Logger::error(
"Document {$statistic->get_document_id()}, request download",
"API error code: {$e->getResponse()->getStatusCode()}. API error message: {$e->getResponse()->getBody()->getContents()}"
);
}
} catch ( \Throwable $e ) {
Logger::error(
"Document {$statistic->get_document_id()}, request download",
"Message: {$e->getMessage()}"
);
}
}

$statistic_repository->save( $statistic );
/**
* @param Statistics $statistic
*
* @return void
*/
private function create_post( $statistic ) {
Logger::event( 'createPost', "Start create post '{$statistic->get_document_id()}'" );

try {
$result = $this->smartcat->getDocumentExportManager()->documentExportDownloadExportResult( $statistic->get_export_id() );
if ( 204 === $result->getStatusCode() ) {
Logger::event( 'createPost', "Export not done yet '{$statistic->get_document_id()}'" );
} elseif ( 200 === $result->getStatusCode() ) {
Logger::event( 'createPost', "Download document '{$statistic->get_document_id()}'" );
ProjectManager::publish( $statistic, $result->getBody()->getContents() );
$statistic->set_status( Statistics::STATUS_COMPLETED )->set_export_id( null );
Logger::event( 'createPost', "Generated post for '{$statistic->get_document_id()}' and status = {$statistic->get_status()}" );
}
} catch ( ClientErrorException $e ) {
if ( 404 === $e->getResponse()->getStatusCode() ) {
$statistic->set_status( Statistics::STATUS_SENDED );
} else {
$statistic->set_status( Statistics::STATUS_FAILED );
}
Logger::error(
"Document {$statistic->get_document_id()}, download translate error",
"API error code: {$e->getResponse()->getStatusCode()}. API error message: {$e->getResponse()->getBody()->getContents()}"
);
} catch ( \Throwable $e ) {
$statistic->set_status( Statistics::STATUS_SENDED );
Logger::error( "Document {$statistic->get_document_id()}, download translate error", "Message: {$e->getMessage()}" );
} finally {
$this->statistic_repository->save( $statistic );
}

Logger::event( 'cron', 'Checking documents ended' );
Logger::event( 'createPost', "End create post '{$statistic->get_document_id()}'" );
}
}
10 changes: 8 additions & 2 deletions inc/smartcat/Cron/SendToSmartCAT.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use SmartCAT\WP\Helpers\Logger;
use SmartCAT\WP\Helpers\SmartCAT;
use SmartCAT\WP\Helpers\Utils;
use SmartCAT\WP\WP\Options;

/**
* Class SendToSmartCAT
Expand Down Expand Up @@ -47,11 +48,16 @@ public function run() {
return;
}

Logger::event( 'cron', 'Sending to Smartсat started' );

/** @var ContainerInterface $container */
$container = Connector::get_container();

/** @var Options $options */
$options = $container->get( 'core.options' );

$options->set( 'last_cron_send', time() );

Logger::event( 'cron', 'Sending to Smartсat started' );

/** @var TaskRepository $task_repository */
$task_repository = $container->get( 'entity.repository.task' );

Expand Down
Loading

0 comments on commit 765d2aa

Please sign in to comment.