php – Laravel CRON或事件进程通过长时间轮询响应api请求 – 如何重新激活会话

前端之家收集整理的这篇文章主要介绍了php – Laravel CRON或事件进程通过长时间轮询响应api请求 – 如何重新激活会话前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Laravel 5.7服务器上的API上有一个轮询路由,其中​​api用户可以在上次轮询后请求任何信息.

如果有新信息返回$this-> prepareResult($newData),那么简单的部分是立即响应有效请求;

如果没有新数据,我将在数据库中存储轮询请求,然后cron实用程序可以每分钟检查一次所有轮询请求,并响应任何已更新数据的轮询.或者,我可以为数据更新创建一个事件监听器,并在更新数据时触发对轮询的响应.

我坚持如何恢复每个会话以匹配等待更新的设备.我可以存储或传递会话ID,但是如何确保CRON任务/事件处理器能够响应正确的IP地址,就像它对原始请求一样. PHP甚至可以这样做吗?

我试图避免websockets,因为会有很多设备,但有限的更新/交互.

长轮询是一种有效的技术.我认为用会话运行民意调查是一个坏主意.因为会话仅适用于原始用户.你可以使用PHP cli运行你的长轮询.您可以检查您的中间件以仅允许cli进行路由轮询.你可以使用 pthreads
通过cli运行你的长轮询使用pthreads.而现在pthreads v3在CLI以外的任何地方都安全且合理地设计.你可以使用你的cron每一小时触发你的线程.然后在你的控制器中你需要存储$time = time();标记您的执行开始时间.然后创建dowhile循环以循环您的轮询过程.条件可以是($time> time()3600)或其他条件.内部循环你需要检查是否存在民意调查?如果是,则运行它.然后在内部循环线的底部你需要睡一段时间,例如2秒.

在你的background.PHP上(这个文件由cron执行)

  1. <?PHP
  2. error_reporting(-1);
  3. ini_set('display_errors',1);
  4.  
  5. class Atomic extends Threaded {
  6. public function __construct($data = NULL) {
  7. $this->data = $data;
  8. }
  9.  
  10. private $data;
  11. private $method;
  12. private $class;
  13. private $config;
  14. }
  15.  
  16. class Task extends Thread {
  17.  
  18. public function __construct(Atomic $atomic) {
  19. $this->atomic = $atomic;
  20. }
  21.  
  22. public function run() {
  23. $this->atomic->synchronized(function($atomic)
  24. {
  25. chdir($atomic->config['root']);
  26.  
  27. $exec_statement = array(
  28. "PHP7.2.7",$atomic->config['index'],$atomic->class,$atomic->method
  29. );
  30.  
  31. echo "Running Command".PHP_EOL. implode(" ",$exec_statement)." at: ".date("Y-m-d H:i:s").PHP_EOL;
  32. $data = shell_exec(implode(" ",$exec_statement));
  33.  
  34. echo $data.PHP_EOL;
  35.  
  36. },$this->atomic);
  37. }
  38.  
  39. private $atomic;
  40. }
  41.  
  42. $config = array(
  43. "root" => "/var/www/api.example.com/api/v1.1","index" => "index.PHP","interval_execution_time" => 200
  44. );
  45.  
  46. chdir($config['root']);
  47.  
  48. $threads = array();
  49.  
  50.  
  51. $list_threads = array(
  52. array(
  53. "class" => "Background_workers","method" => "send_email","total_thread" => 2
  54. ),array(
  55. "class" => "Background_workers","method" => "updating_data_user","method" => "sending_fcm_broadcast","total_thread" => 2
  56. )
  57. );
  58.  
  59. for ($i=0; $i < count($list_threads); $i++)
  60. {
  61. $total_thread = $list_threads[$i]['total_thread'];
  62.  
  63. for ($j=0; $j < $total_thread; $j++)
  64. {
  65. $atomic = new Atomic();
  66. $atomic->class = $list_threads[$i]['class'];
  67. $atomic->method = $list_threads[$i]['method'];
  68. $atomic->thread_number = $j;
  69. $atomic->config = $config;
  70.  
  71. $threads[] = new Task($atomic);
  72. }
  73. }
  74.  
  75. foreach ($threads as $thread) {
  76. $thread->start();
  77. usleep(200);
  78. }
  79.  
  80. foreach ($threads as $thread)
  81. $thread->join();
  82. ?>

这在你的控制器上

  1. <?PHP
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Background_workers extends MX_Controller {
  5.  
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->database();
  10. $this->output->enable_profiler(FALSE);
  11. $this->configuration = $this->config->item("configuration_background_worker_module");
  12. }
  13.  
  14. public function sending_fcm_broadcast() {
  15. $time_run = time();
  16. $time_stop = strtotime("+1 hour");
  17.  
  18. do{
  19. $time_run = time();
  20.  
  21. modules::run("Background_worker_module/sending_fcm_broadcast",$this->configuration["fcm_broadcast"]["limit"]);
  22.  
  23. sleep(2);
  24. }
  25. while ($time_run < $time_stop);
  26. }
  27. }

这是来自codeigniter控制器的示例运行代码.

猜你在找的Laravel相关文章