Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AjaxProcessCommand implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Trackable;
- /**
- * @var array $cmds
- */
- protected $cmds;
- /**
- * @var string $cwd
- */
- protected $cwd;
- /**
- * @var int $shopID
- */
- protected $shopID;
- /**
- * Create a new job instance.
- *
- * @param array $cmd
- * @param string $cwd
- * @param int $shopID
- *
- * @return void
- */
- public function __construct(array $cmd, string $cwd, int $shopID)
- {
- $this->cmds = $this->arraySplitByFilter($cmd, '&&');
- $this->cwd = $cwd;
- $this->shopID = $shopID;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $shopID = $this->shopID;
- $processExitCodes = [];
- try
- {
- foreach ($this->cmds as $cmd) {
- $processBuilder = new ProcessBuilder();
- $processBuilder
- ->setArguments($cmd)
- ->setWorkingDirectory($this->cwd);
- $process = $processBuilder->getProcess();
- event(new AjaxProcessExecuting($shopID, $process->getCommandLine() . "\r\n"));
- $process->run(function($type, $response) use (&$shopID) {
- if ($type == Process::ERR) {
- // $response
- // Resolving deltas: 86% (21274/24713)\rResolving deltas: 87% (21502/24713)\rResolving deltas: 88% (21750/24713)
- // Set property that this is an error output?!
- event(new AjaxProcessExecuting($shopID, $response));
- }
- else {
- event(new AjaxProcessExecuting($shopID, $response));
- }
- });
- $processExitCodes[] = $process->getExitCode() === 0;
- }
- if (in_array(false, $processExitCodes, true)) {
- event(new AjaxProcessExecuted($shopID, false));
- } else {
- event(new AjaxProcessExecuted($shopID, true));
- }
- }
- catch (\Exception $e)
- {
- event(new AjaxProcessExecuting($shopID, $e->getMessage()));
- //$this->error($e->getMessage());
- }
- }
- public function arraySplitByFilter(array $cmdArray, string $filter)
- {
- $i = 0;
- $multiCmdArray = [];
- foreach ($cmdArray as $cmd) {
- if ($cmd === $filter) {
- $i++;
- } else {
- $multiCmdArray[$i][] = $cmd;
- }
- }
- return $multiCmdArray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement