commands-1.x-dev/src/Plugin/AdvancedQueue/JobType/CommandsJobType.php

src/Plugin/AdvancedQueue/JobType/CommandsJobType.php
<?php

namespace Drupal\commands\Plugin\AdvancedQueue\JobType;

use Drupal\advancedqueue\Exception\DuplicateJobException;
use Drupal\advancedqueue\Job;
use Drupal\advancedqueue\JobResult;
use Drupal\advancedqueue\Plugin\AdvancedQueue\Backend\BackendInterface;
use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase;
use Drupal\commands\Entity\Command;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Plugin\PluginBase;
use Drush\Drush;
use Drush\Exceptions\CommandFailedException;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

/**
 * Provides the job type for renewing recurring orders.
 *
 * @AdvancedQueueJobType(
 *   id = "command_run",
 *   label = @Translation("Runs a command."),
 * )
 */
class CommandsJobType extends JobTypeBase {
  use LoggerAwareTrait;
  /**
   * {@inheritdoc}
   */
  public function process(Job $job) {
    $command_id = $job->getPayload()['command_id'];
    $command = Command::load($command_id);
    if (empty($command)) {
      return JobResult::failure(t('Command ID :id not found.', [':id' => $command_id]));
    }

    $args = explode(' ', $command->command->value);
    $process = Drush::process($args);

    $process->setWorkingDirectory($command->working_directory->value ?? getcwd());

    try {
      $process->mustRun(function ($type, $buffer) use ($command){
        // @TODO: Use insert queries directly? Test performance.
        $command->get('output')->appendItem([
          'output' => $buffer,
          'stream' => $type == Process::OUT ? Process::STDOUT: Process::STDERR,
        ]);
        $command->save();
      });
//      $command->set('state', Command::COMMAND_OK);
//      $command->setNewRevision();
//      $command->set('finished', \Drupal::time()->getCurrentTime());
//      $command->save();
      return JobResult::success('Command ended in Success.');
    }
    catch (ProcessFailedException $exception) {
//      $command->set('state', Command::COMMAND_ERROR);
//      $command->setNewRevision();
//      $command->set('finished', \Drupal::time()->getCurrentTime());
      $command->save();
      return JobResult::failure(dt('Command ended in Failure.'));
    }
  }
//    $order_id = $job->getPayload()['order_id'];
//    $order_storage = $this->entityTypeManager->getStorage('commerce_order');
//    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
//    $order = $order_storage->load($order_id);
//    if (!$order) {
//      return JobResult::failure('Order not found.');
//    }
//    $this->recurringOrderManager->renewOrder($order);
//
//    return JobResult::success();
//  }

  public function logger() {
    return \Drupal::logger('commands');
  }
}


Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc