CronHeartbeat.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2020/3/27
  6. * Time: 15:22
  7. */
  8. namespace app\backend\modules\survey\models;
  9. use app\common\models\BaseModel;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. class CronHeartbeat extends BaseModel
  12. {
  13. //use SoftDeletes;
  14. protected $table = 'yz_cron_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. $status = 'not_open';
  38. switch ($execution_time) {
  39. //2分钟以内就是绿灯
  40. case $current_time < ($execution_time + 120):
  41. $status = 'green';
  42. break;
  43. //4分钟以内就是黄灯
  44. case $current_time < ($execution_time + 240):
  45. $status = 'yellow';
  46. break;
  47. //4分钟以上就是红灯
  48. default:
  49. $status = 'red';
  50. }
  51. return $status;
  52. }
  53. /**
  54. * @param int $current_time 指定时间
  55. * @param int $s 距离指定时间多少秒内
  56. * @return int
  57. */
  58. public function verifyRepeat($current_time, $s = 60)
  59. {
  60. return self::whereBetween('execution_time',[$current_time - $s,$current_time])->count();
  61. }
  62. }