CronAction.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 = $preselect_plate_queue -> add($info);
  51. if(!$result){
  52. echo 'info push to preselect_plate_queue failed!'.json_encode($info).PHP_EOL;
  53. }
  54. usleep(10000);
  55. }while(time()-$start<60);
  56. /*
  57. $config = parse_url(C('REDIS_DSN'));
  58. $redis = new Redis();
  59. $redis->connect($config["host"],$config["port"]?:6379);
  60. $redis->auth($config["pass"]?: "");
  61. $redis->select(trim($config['path'],'//') ? : 0);
  62. $all = $redis->hgetall('wjw_plate');
  63. foreach($all as $key=>$val){
  64. $data = json_decode($val,true);
  65. $orderstatus = M('jms_order')->where(array('LicensePlate'=>$key))->getField('OrderStatus');
  66. if($orderstatus == NULL){
  67. $time = time() - (C('EXPIREIN')*60);
  68. if($time > $data['Timestamp']){
  69. $result = $redis->sAdd('plate_preselect_pool' , $key);
  70. if(!$result){
  71. json_fail('释放过期车牌失败');
  72. }
  73. }
  74. }
  75. if($orderstatus == 2){
  76. $p = Redis("wjw_plate","hash");
  77. $res = $p->where($key)->delete();
  78. if(!$res){
  79. json_fail('删除过期车牌失败');
  80. }
  81. $result = $redis->sAdd('plate_preselect_pool' , $key);
  82. if(!$result){
  83. json_fail('释放过期车牌失败');
  84. }
  85. }
  86. if($orderstatus == 1){
  87. $p = Redis("wjw_plate","hash");
  88. $res = $p->where($key)->delete();
  89. if(!$res){
  90. json_fail('删除已开户车牌失败');
  91. }
  92. }
  93. }
  94. json_success('执行完毕');
  95. */
  96. }
  97. }