JobHeartbeat.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2020/3/27
  6. * Time: 15:23
  7. */
  8. namespace app\backend\modules\survey\models;
  9. use app\common\models\BaseModel;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. class JobHeartbeat extends BaseModel
  12. {
  13. //use SoftDeletes;
  14. protected $table = 'yz_job_heartbeat';
  15. public $timestamps = false;
  16. //protected $dates = ['execution_time'];
  17. /**
  18. * 获取队列情况
  19. * @param $current_time
  20. * @return array
  21. */
  22. public static function getLog($current_time)
  23. {
  24. $model = self::select('execution_time')->orderBy('id', 'desc')->first();
  25. return [
  26. 'queue_status' => self::queueStatus($current_time, $model->execution_time),
  27. 'is_repeat' => 0,//self::verifyRepeat($current_time),
  28. ];
  29. }
  30. /**
  31. * @param $current_time 当前时间
  32. * @param $execution_time 执行时间
  33. * @return string 状态
  34. */
  35. public static function queueStatus($current_time, $execution_time)
  36. {
  37. if (empty($execution_time)) {
  38. return 'not_open';
  39. }
  40. switch ($execution_time) {
  41. //2分钟以内就是绿灯
  42. case $current_time < ($execution_time + 120):
  43. $status = 'green';
  44. break;
  45. //4分钟以内就是黄灯
  46. case $current_time < ($execution_time + 240):
  47. $status = 'yellow';
  48. break;
  49. //4分钟以上就是红灯
  50. default:
  51. $status = 'red';
  52. }
  53. return $status;
  54. }
  55. /**
  56. * @param int $current_time 指定时间
  57. * @param int $s 距离指定时间多少秒内
  58. * @return int
  59. */
  60. public static function verifyRepeat($current_time, $s = 60)
  61. {
  62. return self::whereBetween('execution_time',[$current_time - $s,$current_time])->count();
  63. }
  64. }