123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace huolib\queue\job;
- use huo\model\member\MemGameModel;
- use huolib\constant\CacheConst;
- use huolib\constant\IaConst;
- use huolib\constant\MemConst;
- use huolib\tool\Time;
- use huomp\controller\member\MemberOut;
- use huomp\model\log\OpenGameModel;
- use think\Cache;
- use think\Log;
- use think\queue\Job;
- class OpenGameFire extends Fire {
- static $g_i = 0;
-
- public function fire(Job $job, $data) {
- if ($job->attempts() > 1) {
- $job->delete();
- }
- self::$g_i++;
- $_is_job_done = $this->doJob($data);
- $runtime = round(microtime(true) - THINK_START_TIME, 10);
- echo $runtime.' '.self::$g_i;
- if ($_is_job_done) {
-
- $job->delete();
- } else {
- if ($job->attempts() > 3) {
-
- $job->delete();
-
-
- }
- }
- }
-
- public function doJob($data) {
- $_cache_key = CacheConst::CACHE_MEM_OPEN_GAME_PREFIX.md5(json_encode(array($data['mem_id'], $data['game_id'])));
- $_cache = Cache::get($_cache_key);
- $_time = time();
- \think\Log::write(array($data,$_cache),'error',true);
- if (!empty($_cache) && MemConst::OPEN_GAME_STATUS_2 == $_cache['status']) {
- $_mem_game_model = new MemGameModel();
- list($today_start, $today_end) = Time::today();
- $_rs = $_mem_game_model->hasOpen($data['mem_id'], $data['game_id'], $today_start);
- if (true != $_rs) {
- $_ext = ['app_id' => $data['app_id'], 'game_id' => $data['game_id']];
- (new MemberOut())->setPlayCnt($data['mem_id'], 1, IaConst::IA_STATUS_NOT, $_ext);
- $_mem_game_model->rmHasOpen($data['mem_id'], $data['game_id'], $today_start);
- }
- $_data['status'] = $_cache['status'];
- }
- $_data['end_time'] = $_time;
- $_data['duration'] = $_time - $_cache['start_time'];
- $_og_model = (new OpenGameModel());
- $_og_map = [
- 'date' => date('Y-m-d', $_time),
- 'mem_id' => $data['mem_id'],
- 'game_id' => $data['game_id'],
- ];
- $_og_data = $_og_model->where($_og_map)->find()->toArray();
- if (!empty($_og_data)) {
- $_og_data['start_time'] = $data['start_time'];
- $_og_data['status'] = $_cache['status'];
- $_og_data['end_time'] = $_time;
- $_og_data['duration'] = $_time - $_cache['start_time'];
- $_og_data['update_time'] = $_time;
- $_map = ['id' => $_og_data['id']];
- $_rs = $_og_model->save($_og_data, $_map);
- } else {
- $_rs = $_og_model->insertLog($_data);
- }
- if (false == $_rs) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."step=".self::$g_i."&msg=".json_encode($_rs)."&data="
- .json_encode($data),
- LOG::QUEUE
- );
- }
- if (MemConst::OPEN_GAME_STATUS_1 == $_cache['status']) {
- Cache::rm($_cache_key);
- }
- return true;
- }
- }
|