Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. class AjaxProcessCommand implements ShouldQueue
  2. {
  3. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Trackable;
  4.  
  5. /**
  6. * @var array $cmds
  7. */
  8. protected $cmds;
  9.  
  10. /**
  11. * @var string $cwd
  12. */
  13. protected $cwd;
  14.  
  15. /**
  16. * @var int $shopID
  17. */
  18. protected $shopID;
  19.  
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @param array $cmd
  24. * @param string $cwd
  25. * @param int $shopID
  26. *
  27. * @return void
  28. */
  29. public function __construct(array $cmd, string $cwd, int $shopID)
  30. {
  31. $this->cmds = $this->arraySplitByFilter($cmd, '&&');
  32. $this->cwd = $cwd;
  33. $this->shopID = $shopID;
  34. }
  35.  
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. $shopID = $this->shopID;
  44. $processExitCodes = [];
  45.  
  46. try
  47. {
  48. foreach ($this->cmds as $cmd) {
  49. $processBuilder = new ProcessBuilder();
  50. $processBuilder
  51. ->setArguments($cmd)
  52. ->setWorkingDirectory($this->cwd);
  53.  
  54. $process = $processBuilder->getProcess();
  55. event(new AjaxProcessExecuting($shopID, $process->getCommandLine() . "\r\n"));
  56. $process->run(function($type, $response) use (&$shopID) {
  57. if ($type == Process::ERR) {
  58. // $response
  59. // Resolving deltas: 86% (21274/24713)\rResolving deltas: 87% (21502/24713)\rResolving deltas: 88% (21750/24713)
  60. // Set property that this is an error output?!
  61. event(new AjaxProcessExecuting($shopID, $response));
  62. }
  63. else {
  64. event(new AjaxProcessExecuting($shopID, $response));
  65. }
  66. });
  67.  
  68. $processExitCodes[] = $process->getExitCode() === 0;
  69. }
  70. if (in_array(false, $processExitCodes, true)) {
  71. event(new AjaxProcessExecuted($shopID, false));
  72. } else {
  73. event(new AjaxProcessExecuted($shopID, true));
  74. }
  75. }
  76. catch (\Exception $e)
  77. {
  78. event(new AjaxProcessExecuting($shopID, $e->getMessage()));
  79. //$this->error($e->getMessage());
  80. }
  81. }
  82.  
  83. public function arraySplitByFilter(array $cmdArray, string $filter)
  84. {
  85. $i = 0;
  86. $multiCmdArray = [];
  87.  
  88. foreach ($cmdArray as $cmd) {
  89. if ($cmd === $filter) {
  90. $i++;
  91. } else {
  92. $multiCmdArray[$i][] = $cmd;
  93. }
  94. }
  95.  
  96. return $multiCmdArray;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement