Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5. *
  6. * Created By : Prasenjit CHowdhury
  7. * Description : Creating a single interface for acting with the redis no need of third party code single wntry for redis monitories and data management
  8. */
  9.  
  10. namespace App\Console\Commands;
  11. date_default_timezone_set('UTC');
  12.  
  13. use Illuminate\Console\Command;
  14. use Resque;
  15. use Resque_Job_Status;
  16. use Resque_Worker;
  17. use Resque_Stat;
  18.  
  19. class ResqueConsole extends Command
  20. {
  21. /**
  22. * The name and signature of the console command.
  23. *
  24. * @var string
  25. */
  26.  
  27. private $resquePath;
  28. private $laravelPath;
  29. private $logPath;
  30. private $error = array();
  31. private $errorMessage;
  32. private $user;
  33. private $redisHost;
  34. private $redisPort;
  35.  
  36. protected $signature = 'resque {action}
  37. {-queue=default}
  38. {-n=5}
  39. {-interval=5}
  40. ';
  41.  
  42. /**
  43. * The console command description.
  44. *
  45. * @var string
  46. */
  47. protected $description = 'handle all resque related request from this single console';
  48.  
  49. /**
  50. * Create a new command instance.
  51. *
  52. * @return void
  53. */
  54. public function __construct()
  55. {
  56. parent::__construct();
  57. $path = app_path();
  58. $ds = DIRECTORY_SEPARATOR;
  59. if($path != ''){
  60.  
  61. $pathArray = explode($ds,$path);
  62. array_pop($pathArray);
  63. $pathArray = implode($ds,$pathArray);
  64. $this->laravelPath = $pathArray.$ds.'app'.$ds.'Lib'.$ds.'ResqueJobRiver.php';
  65. //$pathArray.$ds.'app'.$ds.'Console'.$ds.'Commands'.$ds.'ResqueJob.php';//$pathArray.$ds.'bootstrap'.$ds.'app.php';
  66. //$this->resquePath = $pathArray.$ds.'vendor'.$ds.'chrisboulton'.$ds.'php-resque'.$ds.'resque.php';
  67. $this->resquePath = $pathArray.$ds.'app'.$ds.'Lib'.$ds.'resqueinit.php';
  68. //var_dump($this->resquePath);
  69. $this->logPath = $pathArray.$ds.'storage'.$ds.'logs'.$ds.'resque.log';
  70.  
  71. }
  72.  
  73. $this->redisHost = env('REDIS_HOST','localhost');
  74. $this->redisPort = env('REDIS_PORT', 6379);
  75. $this->redisDb= env('REDIS_DB', 2);
  76. $this->redisPassword = env('REDIS_PASSWORD',null);
  77.  
  78. if(is_null($this->redisPort) || is_null($this->redisHost)){
  79.  
  80. $this->error[] = "Redis config missing";
  81. }
  82.  
  83. if(!file_exists($this->resquePath)){
  84.  
  85. $this->error[] = "Resque folder chrisboulton".$ds."php-resque".$ds."resque.php missing please update the composer to add resque part";
  86. }
  87.  
  88.  
  89. if(!file_exists($this->logPath)){
  90.  
  91. $fh = fopen($this->logPath,'w');
  92. fclose($fh);
  93. chmod($this->logPath, 0777);
  94.  
  95. }
  96. $this->user = get_current_user();
  97.  
  98. }
  99.  
  100. /**
  101. * Execute the console command.
  102. *
  103. * @return mixed
  104. */
  105. public function handle()
  106. {
  107.  
  108. /*
  109. * Check for any errors of directory structure if present
  110. * move forward else throw error
  111. */
  112. if(count($this->error) > 0){
  113.  
  114. $error = '';
  115. foreach ($this->error as $key => $value) {
  116.  
  117. $error .= $key." $value";
  118. //var_dump($value);
  119. }
  120.  
  121. $this->error($error);
  122.  
  123. }else{
  124.  
  125.  
  126. $arguments = $this->argument();
  127. $action = $arguments['action'];
  128. $queue = $arguments['-queue'];
  129. $noOfworkers = $arguments['-n'];
  130. $interval = $arguments['-interval'];
  131.  
  132.  
  133. if(!in_array($action,array('start','tail','stop','stats','restart'))){//'stop','restart',
  134.  
  135. $this->error("Action Allowed are start / stop / restart");
  136. return;
  137.  
  138. }
  139.  
  140. //var_dump($this->redisPassword);
  141. if(empty($this->redisPassword)){
  142. echo "setting no password1 resconsole";
  143. Resque::setBackend($this->redisHost.':'.$this->redisPort,$this->redisDb,'resque');
  144. }else{
  145.  
  146. if($this->redisPassword == '' || is_null($this->redisPassword)){
  147. echo "setting no password2 resconsole";
  148. Resque::setBackend($this->redisHost.':'.$this->redisPort,$this->redisDb,'resque');
  149. }else{
  150. echo "setting password resconsole";
  151. Resque::setBackend($this->redisHost.':'.$this->redisPort,$this->redisDb,'resque',$this->redisPassword);
  152. }
  153. }
  154.  
  155. //Resque::setBackend($this->redisHost.':'.$this->redisPort,$this->redisDb,'resque');
  156. //land on the required action fields
  157. //@params
  158. // queue = queuename
  159. // number = numberofworkers
  160. // interval = intervaltime
  161.  
  162. $this->$action(escapeshellarg($queue),escapeshellarg($noOfworkers),escapeshellarg($interval));
  163.  
  164. }
  165. }
  166.  
  167.  
  168. public function start($queue,$workersNumber,$interval){
  169.  
  170. $this->info(" Forking new PHP Resque worker service ( queue: {$queue} no of workers {$workersNumber} interval time {$interval})" );
  171. //$cmd = 'nohup sudo -u '.$this->user.' bash -c "cd ' .' php ./resque.php';
  172. $cmd = "nohup sudo -u $this->user QUEUE=$queue COUNT=$workersNumber REDIS_BACKEND=$this->redisHost:$this->redisPort REDIS_DB=$this->redisDb ";
  173. $cmd .= "REDIS_PASSWORD=$this->redisPassword ";
  174. $cmd .= "VVERBOSE=true APP_INCLUDE=$this->laravelPath INTERVAL=$interval php $this->resquePath";
  175. $cmd .= ' >> '.$this->logPath;
  176. $this->info($cmd);
  177. //$cmd.= ' > /dev/null 2>&1 &';
  178. passthru($cmd);
  179.  
  180. }
  181.  
  182. /**
  183. * Convenience functions.
  184. */
  185. public function tail($queue,$workersNumber,$interval){
  186. //$log_path = $this->logPath;
  187. if (file_exists($this->logPath))
  188. {
  189. passthru('sudo tail -f ' . escapeshellarg($this->logPath));
  190. }
  191. else
  192. {
  193. $this->out('Log file does not exist. Is the service running?');
  194. }
  195. }
  196.  
  197.  
  198. /**
  199. * Kill all php resque worker services.
  200. */
  201. public function stop($queue,$workersNumber=NULL,$interval=NULL){
  202.  
  203. $queue = str_replace("'", "", $queue);
  204. $forced = false;
  205. $this->info('Shutting down Resque Worker complete');
  206. $workers = Resque_Worker::all();
  207. if (empty($workers))
  208. {
  209. $this->error(' There were no active workers to kill ...');
  210. }
  211. else{
  212.  
  213. $count = 0;
  214. foreach($workers as $w)
  215. {
  216. if($forced){
  217. $w->shutDownNow();
  218. }else{
  219. $w->shutDown(); // Send signal to stop processing jobs
  220. }
  221.  
  222. // Remove jobs from resque environment
  223. //list($hostname, $pid, $queue) = explode(':', (string)$w);
  224.  
  225. $dumps = explode(':', (string)$w);
  226. if($dumps[2] == $queue){
  227. $w->unregisterWorker();
  228. $this->error('Killing ' . $dumps[1]);
  229. exec('kill -9 '.$dumps[1]);
  230. $count++;
  231. } // Kill all remaining system process
  232. }
  233. $this->info('Killed '.$count.' workers ...');
  234. }
  235. }
  236.  
  237.  
  238. public function stats($queue,$workersNumber,$interval)
  239. {
  240. $this->info("\n");
  241. $this->info('<info>PHPResque Statistics</info>');
  242. $this->info("\n");
  243. $this->info('<info>Jobs Stats</info>');
  244. $this->info(" Processed Jobs : " . Resque_Stat::get('processed'));
  245. $this->info(" <warning>Failed Jobs : " . Resque_Stat::get('failed') . "</warning>");
  246. $this->info("\n");
  247. $this->info('<info>Workers Stats</info>');
  248. $workers = Resque_Worker::all();
  249. $this->info(" Active Workers : " . count($workers));
  250.  
  251. if (!empty($workers))
  252. {
  253. foreach($workers as $worker)
  254. {
  255. $this->info("\tWorker : " . $worker);
  256. $this->info("\t - Started on : " . Resque::Redis()->get('worker:' . $worker . ':started'));
  257. $this->info("\t - Processed Jobs : " . $worker->getStat('processed'));
  258. $worker->getStat('failed') == 0
  259. ? $this->info("\t - Failed Jobs : " . $worker->getStat('failed'))
  260. : $this->info("\t - <warning>Failed Jobs : " . $worker->getStat('failed') . "</warning>");
  261. }
  262. }
  263.  
  264. $this->info("\n");
  265. }
  266.  
  267.  
  268. /**
  269. * Restart all workers
  270. */
  271. public function restart()
  272. {
  273. $this->stop(false);
  274.  
  275. if (false !== $workers = $this->__getWorkers())
  276. {
  277. foreach($workers as $worker)
  278. $this->start($worker);
  279. }
  280. else
  281. $this->start();
  282. }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement