CronAction.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. class CronAction extends Action {
  3. public function releaseRedisExpiredLicenseplate( ){
  4. $start = time();
  5. $preselect_plate_queue = Redis("wjw_preselect_plate_queue","queue");
  6. $plate_preselect_pool = Redis("plate_preselect_pool","set");
  7. do{
  8. $info = $preselect_plate_queue -> pop();
  9. if(!$info){
  10. echo "no more message".PHP_EOL;
  11. sleep(1);
  12. continue;
  13. }
  14. if(!$info['LicensePlate']){
  15. echo "LicensePlate empty!".PHP_EOL;
  16. continue;
  17. }
  18. //检测车牌是否已生成待开户订单,已生成的丢弃,未生成释放到公选池
  19. $cond = array(
  20. 'LicensePlate' => $info['LicensePlate'],
  21. 'OrderStatus' => C('订单状态_待开户')
  22. );
  23. $order_info = M('jms_order')->where($cond)->field('LicensePlate,OrderStatus')->find();
  24. if(!$order_info){
  25. $cond = array(
  26. 'LicensePlate' => $info['LicensePlate'],
  27. 'OrderStatus' => C('订单状态_已开户')
  28. );
  29. $order_info = M('jms_order')->where($cond)->field('LicensePlate,OrderStatus')->find();
  30. }
  31. if($order_info && $order_info['OrderStatus'] === C('订单状态_待开户')){
  32. echo 'LicensePlate: '.$info['LicensePlate'].' has waiting order,discard!'.PHP_EOL;
  33. continue;
  34. }
  35. if($order_info && $order_info['OrderStatus'] === C('订单状态_已开户')){
  36. echo 'LicensePlate: '.$info['LicensePlate'].' has finish order,discard!'.PHP_EOL;
  37. continue;
  38. }
  39. //过期的预约车牌,释放到公选池
  40. if(time()>$info['ExpireTime']){
  41. $result = $plate_preselect_pool -> add($info['LicensePlate']);
  42. if(!$result){
  43. echo 'LicensePlate: '.$info['LicensePlate'].' push to plate_preselect_pool failed!'.PHP_EOL;
  44. continue;
  45. }
  46. echo 'LicensePlate: '.$info['LicensePlate'].' timeout, push to plate_preselect_pool'.PHP_EOL;
  47. continue;
  48. }
  49. //没有过期的,放回队列
  50. $result = $plate_preselect_pool -> push($info);
  51. if(!$result){
  52. echo 'info push to plate_preselect_pool failed!'.PHP_EOL;
  53. continue;
  54. }
  55. usleep(1000);
  56. }while(time()-$start<60);
  57. /*
  58. $config = parse_url(C('REDIS_DSN'));
  59. $redis = new Redis();
  60. $redis->connect($config["host"],$config["port"]?:6379);
  61. $redis->auth($config["pass"]?: "");
  62. $redis->select(trim($config['path'],'//') ? : 0);
  63. $all = $redis->hgetall('wjw_plate');
  64. foreach($all as $key=>$val){
  65. $data = json_decode($val,true);
  66. $orderstatus = M('jms_order')->where(array('LicensePlate'=>$key))->getField('OrderStatus');
  67. if($orderstatus == NULL){
  68. $time = time() - (C('EXPIREIN')*60);
  69. if($time > $data['Timestamp']){
  70. $result = $redis->sAdd('plate_preselect_pool' , $key);
  71. if(!$result){
  72. json_fail('释放过期车牌失败');
  73. }
  74. }
  75. }
  76. if($orderstatus == 2){
  77. $p = Redis("wjw_plate","hash");
  78. $res = $p->where($key)->delete();
  79. if(!$res){
  80. json_fail('删除过期车牌失败');
  81. }
  82. $result = $redis->sAdd('plate_preselect_pool' , $key);
  83. if(!$result){
  84. json_fail('释放过期车牌失败');
  85. }
  86. }
  87. if($orderstatus == 1){
  88. $p = Redis("wjw_plate","hash");
  89. $res = $p->where($key)->delete();
  90. if(!$res){
  91. json_fail('删除已开户车牌失败');
  92. }
  93. }
  94. }
  95. json_success('执行完毕');
  96. */
  97. }
  98. }