You are here

function tmgmt_smartling_cron in TMGMT Translator Smartling 8.4

Implements hook_cron().

File

./tmgmt_smartling.module, line 43
Contains

Code

function tmgmt_smartling_cron() {
  $smartling_provider_configs = \Drupal::getContainer()
    ->get("tmgmt_smartling.smartling_config_manager")
    ->getAvailableConfigs();
  foreach ($smartling_provider_configs as $smartling_provider_config) {
    $api_wrapper = \Drupal::getContainer()
      ->get("tmgmt_smartling.smartling_api_wrapper");
    $settings = $smartling_provider_config
      ->get("settings");
    $api_wrapper
      ->setSettings($settings);
    $search_params = new SearchTranslationRequestParams();
    $search_params
      ->setState(TranslationSubmissionStates::STATE_TRANSLATED);
    $search_params
      ->setLimit(100);
    $bucket_name = Drupal::state()
      ->get('tmgmt_smartling.bucket_name', 'tmgmt_smartling_default_bucket_name');
    $result = $api_wrapper
      ->searchOnlyTranslationRequest($bucket_name, $search_params);
    foreach ($result as $item) {
      if (!isset($item['originalAssetKey']['tmgmt_job_id'])) {
        continue;
      }
      $tmgmt_job_id = (int) $item['originalAssetKey']['tmgmt_job_id'];
      $job = Job::load($tmgmt_job_id);

      // Self healing for submissions which doesn't have corresponding
      // TMGMT Job anymore.
      if (empty($job)) {
        try {
          $translation_request_with_submission = $api_wrapper
            ->getTranslationRequestByUid($bucket_name, $item['translationRequestUid']);
          if (!isset($translation_request_with_submission['translationSubmissions'][0]['translationSubmissionUid'])) {
            continue;
          }
          $update_submission_params = new UpdateTranslationSubmissionParams();
          $update_submission_params
            ->setState(TranslationSubmissionStates::STATE_FAILED)
            ->setLastErrorMessage("TMGMT Job {$tmgmt_job_id} doesn't exist in Drupal and will not be scheduled for download")
            ->setTranslationSubmissionUid($translation_request_with_submission['translationSubmissions'][0]['translationSubmissionUid']);
          $update_request_params = new UpdateTranslationRequestParams();
          $update_request_params
            ->addTranslationSubmission($update_submission_params);
          $api_wrapper
            ->updateTranslationRequest($bucket_name, $translation_request_with_submission['translationRequestUid'], $update_request_params);
        } catch (Exception $e) {

          // Marking submission as "failed" failed for some reason.
          // Next cron run will try to mark it as failed again.
          // Do nothing.
        }

        // Just don't add this submission to download queue.
        continue;
      }
      if ($settings['download_by_job_items']) {
        foreach ($job
          ->getItems() as $item) {
          Drupal::service('tmgmt_extension_suit.utils.queue_unique_item')
            ->addItem('tmgmt_extension_suit_download', [
            'tjid' => $tmgmt_job_id,
            'tjiid' => $item
              ->id(),
          ]);
        }
      }
      else {
        Drupal::service('tmgmt_extension_suit.utils.queue_unique_item')
          ->addItem('tmgmt_extension_suit_download', [
          'id' => $tmgmt_job_id,
        ]);
      }
    }
  }
}