Exemplo n.º 1
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['key']) || empty($args['header']) || empty($args['type']) || empty($args['smsRecordId'])) {
         ResqueUtil::log(['status' => 'fail to export bulk sms record', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     $accountId = new \MongoId($args['accountId']);
     $smsRecordId = new \MongoId($args['smsRecordId']);
     $header = $args['header'];
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $rows = null;
     if ($args['type'] == 'all') {
         $rows = BulkSmsLog::preProcessBulkSmsRecordData($smsRecordId, $accountId);
     } else {
         if ($args['type'] == 'faild') {
             // 暂时还没用到
         }
     }
     ExcelUtil::exportCsv($header, $rows, $filePath, 1);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL . $args['accountId']]);
         return true;
     } else {
         ResqueUtil::log(['status' => 'fail to export early bird sms details', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]);
         return false;
     }
 }
 public static function generateData($accountId, $memberPropertyId, $year, $quarter, $propertyOperate)
 {
     $statsQuarterly = ModelStatsMemberCampaignLogDaily::getPropAvgTradeQuarterly($accountId, (string) $memberPropertyId, $year, $quarter);
     $statsAvg = [];
     foreach ($statsQuarterly as $stats) {
         $propValueKey = 'memProperty.' . $memberPropertyId;
         if (empty($stats[$propValueKey])) {
             continue;
         }
         $propId = $propertyOperate;
         $propValue = $stats[$propValueKey];
         $avg = $stats['avg'];
         $statsAvg = ModelStatsMemberPropAvgTradeQuarterly::getByPropAndDate($accountId, $propId, $propValue, $year, $quarter);
         if (!empty($statsAvg)) {
             $statsAvg->avg = $avg;
         } else {
             $statsAvg = new ModelStatsMemberPropAvgTradeQuarterly();
             $statsAvg->propId = $propId;
             $statsAvg->propValue = $propValue;
             $statsAvg->avg = $avg;
             $statsAvg->year = $year;
             $statsAvg->quarter = $quarter;
             $statsAvg->accountId = $accountId;
         }
         try {
             $statsAvg->save();
         } catch (Exception $e) {
             ResqueUtil::log(['Update StatsMemberPropAvgTradeQuarterly error' => $e->getMessage(), 'StatsMemberPropAvgTradeQuarterly' => $statsAvg]);
             continue;
         }
     }
 }
Exemplo n.º 3
0
 public function perform()
 {
     # Run task
     $args = $this->args;
     ResqueUtil::log(['info' => 'update tags', 'args' => $args]);
     $channelId = $args['channelId'];
     $query = $args['query'];
     $tags = $args['tags'];
     $query['pageSize'] = 20;
     $query['pageNum'] = 1;
     $raw = Yii::$app->weConnect->getFollowers($channelId, $query);
     while (!empty($raw['results'])) {
         $followerIds = [];
         foreach ($raw['results'] as $followers) {
             $followerIds[] = $followers['id'];
         }
         if (empty($args['add']) || !$args['add']) {
             $result = Yii::$app->weConnect->removeTags($channelId, $followerIds, $tags);
         } else {
             $result = Yii::$app->weConnect->addTagsToFollowers($channelId, $followerIds, $tags);
         }
         if (!$result) {
             ResqueUtil::log(['error' => 'add tags', 'error followers' => $followerIds]);
             return false;
         }
         $query['pageNum']++;
         $raw = Yii::$app->weConnect->getFollowers($channelId, $query);
     }
     return true;
 }
Exemplo n.º 4
0
 /**
  * @args {"description": "Delay: Stats of StatsMemberDaily", "runNextJob": true}
  * @author Rex Chen
  */
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     if (!empty(WECONNECT_DOMAIN)) {
         $channelNameMap = $this->_getChannelNameMap();
     }
     $start = new \MongoDate($datetime);
     $end = new \MongoDate(strtotime('+1 day', $datetime));
     $memberStats = Member::getNewMemberStats($start, $end);
     $rowStats = [];
     foreach ($memberStats as $stats) {
         $accountId = $stats['_id']['accountId'];
         $origin = $stats['_id']['origin'];
         $socialAccountId = $stats['_id']['socialAccountId'];
         $originName = empty($channelNameMap[$socialAccountId]) ? '' : $channelNameMap[$socialAccountId];
         $total = $stats['total'];
         $statsMember = ModelStatsMemberDaily::getByDateAndOriginInfo($dateStr, $origin, $originName, $accountId);
         if (!empty($statsMember)) {
             $statsMember->total = $total;
             try {
                 $statsMember->save(true, ['total']);
             } catch (Exception $e) {
                 ResqueUtil::log(['Update StatsMemberDaily error' => $e->getMessage(), 'StatsMemberDaily' => $statsMember]);
                 continue;
             }
         } else {
             $rowStats[] = ['date' => $dateStr, 'origin' => $origin, 'originName' => $originName, 'total' => $total, 'accountId' => $accountId];
         }
     }
     ModelStatsMemberDaily::batchInsert($rowStats);
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['key']) || empty($args['header']) || empty($args['accountId'])) {
         ResqueUtil::log(['status' => 'fail to export registration record', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     $keyword = $args['keyword'];
     $header = $args['header'];
     $fileName = $args['key'];
     $accountId = unserialize($args['accountId']);
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $query = new Query();
     if ($keyword == '') {
         $records = $query->from('uhkklpRegistration')->select(['name', 'mobile', 'restaurantName', 'zipCode', 'city', 'address'])->where(['accountId' => $accountId])->all();
     } else {
         $records = $query->from('uhkklpRegistration')->select(['name', 'mobile', 'restaurantName', 'zipCode', 'city', 'address'])->where(['accountId' => $accountId])->andWhere(['like', 'mobile', $keyword])->all();
     }
     $rows = $records;
     ExcelUtil::exportCsv($header, $rows, $filePath, 1);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL]);
         return true;
     } else {
         ResqueUtil::log(['status' => 'fail to export registration', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]);
         return false;
     }
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['key']) || empty($args['header']) || empty($args['activityName'])) {
         ResqueUtil::log(['status' => 'fail to export lucky draw winners record', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     // $accountId = new \MongoId($args['accountId']);
     $condition = unserialize($args['condition']);
     $header = $args['header'];
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $rows = array();
     if ($args['activityName'] == 'cny') {
         $rows = LuckyDrawWinner::preProcessCnyWinnerData($condition);
     }
     ExcelUtil::exportCsv($header, $rows, $filePath, 1);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL . $args['accountId']]);
         return true;
     } else {
         ResqueUtil::log(['status' => 'fail to export lucky draw winners record', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]);
         return false;
     }
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['language']) || empty($args['header']) || empty($args['key']) || empty($args['accountId']) || empty($args['condition'])) {
         ResqueUtil::log(['status' => 'fail to export code', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     //set the language
     Yii::$app->language = empty($args['language']) ? LanguageUtil::DEFAULT_LANGUAGE : $args['language'];
     $header = $args['header'];
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $condition = unserialize($args['condition']);
     $object = GoodsExchangeLog::find();
     $classFunction = '\\backend\\modules\\product\\models\\GoodsExchangeLog::preProcessExportData';
     $backendUser = Account::findByPk(new \MongoId($args['accountId']));
     ExcelUtil::processMultiData($header, $filePath, $backendUser, $condition, $object, $classFunction, ['changeTostring' => ['goods']]);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         \Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL . $args['accountId']]);
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 8
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['key']) || empty($args['header']) || empty($args['condition'])) {
         ResqueUtil::log(['status' => 'fail to export sms result record', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     $condition = unserialize($args['condition']);
     $header = $args['header'];
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $rows = BulkSmsFailed::find()->where($condition)->all();
     $rowsSuccess = BulkSmsFailed::find()->where($condition)->all();
     $finalRows = array_merge($rows, $rowsSuccess);
     ExcelUtil::exportCsv($header, $finalRows, $filePath, 1);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL]);
         return true;
     } else {
         ResqueUtil::log(['status' => 'fail to export sms result', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]);
         return false;
     }
 }
 /**
  * @args {"description": "Delay: Stats of StatsMemberGrowthMonthly every day"}
  * @author Rex Chen
  */
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m', $datetime);
     $startTime = new \MongoDate(strtotime($dateStr . '-01'));
     $endTime = new \MongoDate(strtotime($dateStr . '-01 +1 month'));
     $accounts = Account::findAll(['enabledMods' => 'member']);
     foreach ($accounts as $account) {
         $accountId = $account->_id;
         $totalMember = Member::countByAccount($accountId, null, $endTime);
         $totalActive = MemberLogs::getTotalActiveByAccount($accountId, $startTime, $endTime);
         $totalNew = Member::countByAccount($accountId, $startTime, $endTime);
         $statsGrowth = ModelStatsMemberGrowthMonthly::getByMonthAndAccount($accountId, $dateStr);
         if (empty($statsGrowth)) {
             $statsGrowth = new ModelStatsMemberGrowthMonthly();
             $statsGrowth->accountId = $accountId;
             $statsGrowth->month = $dateStr;
         }
         $statsGrowth->totalNew = $totalNew;
         $statsGrowth->totalActive = $totalActive;
         $statsGrowth->totalInactive = $totalMember - $totalActive;
         try {
             $statsGrowth->save();
         } catch (Exception $e) {
             ResqueUtil::log(['Update StatsMemberGrowthMonthly error' => $e->getMessage(), 'StatsMemberGrowthMonthly' => $statsGrowth]);
             continue;
         }
     }
     return true;
 }
Exemplo n.º 10
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['key']) || empty($args['header'])) {
         ResqueUtil::log(['status' => 'fail to export slotgame useRecord', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     $condition = unserialize($args['condition']);
     $activityId = $condition['activityId'];
     $condition = ['activityId' => $activityId];
     $accountId = new \MongoId($args['accountId']);
     $header = $args['header'];
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $object = ActivityUser::find();
     if (empty($object)) {
         ResqueUtil::log(['status' => 'fail to export slotgame useRecord', 'message' => 'no data found in DB', 'args' => $args]);
         return false;
     }
     $classFunction = '\\backend\\modules\\uhkklp\\models\\ActivityUser::preProcessBarUseRecordData';
     ExcelUtil::processMultiData($header, $filePath, $args, $condition, $object, $classFunction);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL . $args['accountId']]);
         return true;
     } else {
         ResqueUtil::log(['status' => 'fail to export slotgame useRecord', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]);
         return false;
     }
 }
Exemplo n.º 11
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['mainMember']) || empty($args['otherMemberIds'])) {
         ResqueUtil::log(['Merge member args error' => $args]);
         return;
     }
     $mainMember = unserialize($args['mainMember']);
     $otherMemberIds = unserialize($args['otherMemberIds']);
     //Get Name and phone
     $name = $phone = '';
     foreach ($mainMember->properties as $mainProperty) {
         if ($mainProperty['name'] === Member::DEFAULT_PROPERTIES_NAME) {
             $name = $mainProperty['value'];
         }
         if ($mainProperty['name'] === Member::DEFAULT_PROPERTIES_MOBILE) {
             $phone = $mainProperty['value'];
         }
     }
     ScoreHistory::updateAll(['$set' => ['memberId' => $mainMember->_id]], ['memberId' => ['$in' => $otherMemberIds]]);
     MemberLogs::deleteAll(['memberId' => ['$in' => $otherMemberIds]]);
     CampaignLog::updateAll(['$set' => ['member.id' => $mainMember->_id, 'member.cardNumber' => $mainMember->cardNumber, 'member.name' => $name, 'member.phone' => $phone]], ['member.id' => ['$in' => $otherMemberIds]]);
     PromotionCode::updateAll(['$set' => ['usedBy.memberId' => $mainMember->_id, 'usedBy.memberNumber' => $mainMember->cardNumber]], ['usedBy.memberId' => ['$in' => $otherMemberIds]]);
     GoodsExchangeLog::updateAll(['$set' => ['memberId' => $mainMember->_id, 'memberName' => $name, 'telephone' => $phone]], ['memberId' => ['$in' => $otherMemberIds]]);
     $otherMemberStrIds = [];
     foreach ($otherMemberIds as $otherMemberId) {
         $otherMemberStrIds[] = (string) $otherMemberId;
     }
     Order::updateAll(['$set' => ['consumer.id' => (string) $mainMember->_id, 'consumer.name' => $name, 'consumer.phone' => $phone]], ['consumer.id' => ['$in' => $otherMemberStrIds]]);
     Qrcode::deleteAll(['type' => Qrcode::TYPE_MEMBER, 'associatedId' => ['$in' => $otherMemberIds]]);
 }
Exemplo n.º 12
0
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m', $datetime);
     $monthData = ModelStatsMemberDaily::getMonthData($dateStr);
     $rowsMonthly = [];
     foreach ($monthData as $item) {
         $origin = $item['_id']['origin'];
         $originName = $item['_id']['originName'];
         $accountId = $item['_id']['accountId'];
         $total = $item['total'];
         $statsMemberMonthly = ModelStatsMemberMonthly::getByDateAndOriginInfo($dateStr, $origin, $originName, $accountId);
         if (!empty($statsMemberMonthly)) {
             $statsMemberMonthly->total = $total;
             try {
                 $statsMemberMonthly->save(true, ['total']);
             } catch (Exception $e) {
                 ResqueUtil::log(['Update StatsMemberMonthly error' => $e->getMessage(), 'StatsMemberMonthly' => $statsMemberMonthly]);
                 continue;
             }
         } else {
             $rowsMonthly[] = ['month' => $dateStr, 'origin' => $origin, 'originName' => $originName, 'accountId' => $accountId, 'total' => $total];
         }
     }
     ModelStatsMemberMonthly::batchInsert($rowsMonthly);
     return true;
 }
Exemplo n.º 13
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['data']) || empty($args['accountId']) || empty($args['modelContent']) || empty($args['smsBatch'])) {
         ResqueUtil::log(['status' => 'fail to send sms', 'message' => 'missing params', 'args' => $args]);
         LogUtil::error(['message' => 'missing params in job', 'args' => $args], 'Sms');
     }
     $data = $args['data'];
     $accountId = $args['accountId'];
     $modelContent = $args['modelContent'];
     $smsBatch = $args['smsBatch'];
     $failureCount = 0;
     $successCount = 0;
     $totalCount = 0;
     try {
         if (!empty($data)) {
             foreach ($data as $sms) {
                 if ($sms['mobile'] != '') {
                     $response = MessageUtil::sendMobileMessage($sms['mobile'], $sms['content'], $accountId);
                     // $response = MessageUtil::sendMobileMessage($sms['mobile'], $sms['content']);
                     BulkSmsLog::createSmsLog($sms['mobile'], $sms['content'], $response, $smsBatch, $accountId);
                     if (!$response) {
                         $failureCount++;
                         LogUtil::error(['message' => '群發簡訊失敗', 'mobile' => $sms['mobile'], 'SMSContent' => $sms['content']], 'bulkSms');
                         BulkSmsFailed::createSmsFailed($sms['mobile'], $sms['content'], $smsBatch, $accountId);
                     } else {
                         $successCount++;
                         LogUtil::error(['message' => '群發簡訊成功', 'mobile' => $sms['mobile'], 'SMSContent' => $sms['content']], 'bulkSms');
                         BulkSmsSuccess::createSmsSuccess($sms['mobile'], $sms['content'], $smsBatch, $accountId);
                     }
                     unset($response);
                 } else {
                     LogUtil::error(date('Y-m-d h:i:s') . '号码为空.');
                     $failureCount++;
                 }
             }
             $totalCount = $successCount + $failureCount;
             //record result
             $SmsResultModel = new SmsResultModel();
             $SmsResultModel->successRecord = $successCount;
             $SmsResultModel->failureRecord = $failureCount;
             $SmsResultModel->totalRecord = $totalCount;
             $SmsResultModel->smsBatch = $smsBatch;
             $SmsResultModel->accountId = $accountId;
             $SmsResultModel->modelContent = $modelContent;
             $SmsResultModel->save();
         }
     } catch (\Exception $e) {
         LogUtil::error(['message' => 'Sms發送失敗', 'error' => $e], 'sms');
         throw $e;
     }
 }
Exemplo n.º 14
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['key']) || empty($args['header'])) {
         ResqueUtil::log(['status' => 'fail to export member', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     Yii::$app->language = empty($args['language']) ? LanguageUtil::DEFAULT_LANGUAGE : $args['language'];
     $accountId = new \MongoId($args['accountId']);
     $header = $args['header'];
     // get member's customized properties
     $memberProperties = MemberProperty::getByAccount($accountId);
     foreach ($memberProperties as $memberProperty) {
         if ($memberProperty->isDefault) {
             $header[$memberProperty->name] = Yii::t('member', $memberProperty->name);
         } else {
             $header[$memberProperty->name] = $memberProperty->name;
         }
     }
     $socialAccountsMap = [];
     $account = Account::findByPk($accountId);
     $channelIds = Channel::getEnableChannelIds($accountId);
     if (!empty($channelIds)) {
         $socialAccounts = \Yii::$app->weConnect->getAccounts($channelIds);
         foreach ($socialAccounts as $socialAccount) {
             $socialAccountsMap[$socialAccount['id']] = $socialAccount['name'];
         }
     }
     $cardMap = [];
     $cards = MemberShipCard::getByAccount($accountId);
     foreach ($cards as $card) {
         $cardMap[(string) $card->_id] = $card->name;
     }
     $condition = unserialize($args['condition']);
     //get properties
     $memberProperties = MemberProperty::findAll(['accountId' => $accountId]);
     $base = ['cardMap' => $cardMap, 'socialAccountsMap' => $socialAccountsMap, 'memberProperties' => $memberProperties];
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $orderBy = Member::normalizeOrderBy($args['params']);
     $object = Member::find();
     $classFunction = '\\backend\\modules\\member\\models\\Member::preProcessMemberData';
     ExcelUtil::processMultiData($header, $filePath, $base, $condition, $object, $classFunction, [], $orderBy);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         \Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL . $args['accountId']]);
         return true;
     } else {
         return false;
     }
 }
 /**
  * @args {"description": "Delay: Stats of questionnaire answer"}
  */
 public function perform()
 {
     $args = $this->args;
     //Get date from args or today
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     //in case of too much data, get stats by questionnaire
     $skip = 0;
     $limit = 100;
     $query = Questionnaire::find()->orderBy(['_id' => SORT_ASC]);
     $query = $query->offset($skip)->limit($limit);
     $questionnaires = $query->all();
     while (!empty($questionnaires)) {
         $statsRows = [];
         foreach ($questionnaires as $questionnaire) {
             $stats = QuestionnaireLog::getAnswerStats($questionnaire->_id, $dateStr);
             //group stats by questionId
             $rows = [];
             foreach ($stats as $stat) {
                 $questionIdStr = (string) $stat['_id']['questionId'];
                 $optionValue = $stat['_id']['value'];
                 $rows[$questionIdStr][] = ['option' => $optionValue, 'count' => $stat['count']];
             }
             //format stats data, and save it
             foreach ($rows as $questionIdStr => $answerStats) {
                 $questionId = new MongoId($questionIdStr);
                 $statsAnswerDaily = ModelStatsQuestionnaireAnswerDaily::getByQuestionIdAndDate($questionId, $dateStr);
                 if (empty($statsAnswerDaily)) {
                     $statsRows[] = ['questionId' => $questionId, 'stats' => $answerStats, 'date' => $dateStr, 'accountId' => $questionnaire->accountId];
                 } else {
                     $statsAnswerDaily->stats = $answerStats;
                     try {
                         $statsAnswerDaily->save();
                     } catch (Exception $e) {
                         ResqueUtil::log(['Update StatsQuestionnaireAnswerDaily error' => $e->getMessage(), 'StatsQuestionnaireAnswerDaily' => $statsAnswerDaily]);
                         continue;
                     }
                 }
             }
         }
         ModelStatsQuestionnaireAnswerDaily::batchInsert($statsRows);
         $skip += $limit;
         $query = $query->offset($skip)->limit($limit);
         //free $questionnaires
         unset($questionnaires);
         $questionnaires = $query->all();
     }
     return true;
 }
Exemplo n.º 16
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['condition'])) {
         ResqueUtil::log(['status' => 'fail to send early bird sms', 'message' => 'missing params', 'args' => $args]);
         LogUtil::error(['message' => 'EarlyBirdSms發送失敗', 'message' => 'missing params in job', 'args' => $args], 'earlybird');
     }
     $condition = unserialize($args['condition']);
     $smsRecord = new \MongoId($args['smsRecord']);
     if ($args['smsTag'] == 'sms_four') {
         EarlybirdSmsUtil::sendSmsByExchangeGoodsScore($condition, $args['smsTag'], $smsRecord);
     } else {
         EarlybirdSmsUtil::sendSmsBycondition($condition, $args['smsTag'], $smsRecord);
     }
 }
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $accountId = $args['accountId'];
     //Assume that the subChannel is the secode element in properties
     $propertyKey = $args['properties'][1];
     $memberProperty = MemberProperty::findOne(['propertyId' => $propertyKey]);
     if ($memberProperty != null) {
         return ModelStatsMemberPropTradeCodeQuarterly::generateByYearAndQuarter((string) $memberProperty['_id'], $accountId, $datetime);
     } else {
         ResqueUtil::log("Fail to get memberProperty with propertyId {$propertyKey}");
     }
     return false;
 }
Exemplo n.º 18
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['condition']) || empty($args['smsName'])) {
         ResqueUtil::log(['status' => 'fail to send early bird sms', 'message' => 'missing params', 'args' => $args]);
         LogUtil::error(['message' => 'missing params in job', 'args' => $args], 'bulkSms');
     }
     $condition = unserialize($args['condition']);
     $smsRecordId = new \MongoId($args['smsRecord']);
     $smsData = null;
     switch ($args['smsName']) {
         case 'cny_winners':
             $smsData = LuckyDrawWinner::preProcessCnyWinnerSmsData($condition);
             break;
         default:
             break;
     }
     BulkSmsUtil::sendSms($smsData, $args['smsName'], $smsRecordId, $condition['accountId']);
 }
 public function perform()
 {
     //accountId, properties are required fileds
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['properties'])) {
         ResqueUtil::log('Missing required arguments accountId or properties!');
         return false;
     }
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     $start = new MongoDate($datetime);
     $end = new MongoDate(strtotime('+1 day', $datetime));
     $accountId = new MongoId($args['accountId']);
     $condition = ['accountId' => $accountId, 'createdAt' => ['$gte' => $start, '$lt' => $end]];
     $campaignLogs = self::getCampaignLog($condition);
     if (empty($campaignLogs)) {
         LogUtil::info(['date' => date('Y-m-d H:i:s'), 'message' => $dateStr . ': campaignLogs is empty,no need to store data'], 'resque');
         return true;
     }
     //get all member info
     $memberInfos = self::getMemberInfo($condition);
     //Get all the property mongo id for comparison
     $condition = ['propertyId' => ['$in' => $args['properties']], 'accountId' => $accountId];
     $propertyIdStrs = self::getPropertyIds($condition);
     //Generate the meta data for inserting
     $statsRows = [];
     foreach ($campaignLogs as $campaignLog) {
         $campaignLog = $campaignLog['_id'];
         $redeemTime = self::getRedeemTime($campaignLog);
         //check the redeem time whether exists
         $condition = ['code' => $campaignLog['code'], 'productId' => $campaignLog['productId'], 'month' => date('Y-m', $redeemTime)];
         $memberCampaignLogDaily = ModelStatsMemberCampaignLogDaily::findOne($condition);
         if (empty($memberCampaignLogDaily)) {
             $memProperty = self::getProperty((string) $campaignLog['member']['id'], $memberInfos, $propertyIdStrs);
             $statsRows[] = ['memberId' => $campaignLog['member']['id'], 'memProperty' => $memProperty, 'productId' => $campaignLog['productId'], 'code' => $campaignLog['code'], 'year' => date('Y', $redeemTime), 'month' => date('Y-m', $redeemTime), 'quarter' => TimeUtil::getQuarter($redeemTime), 'accountId' => $accountId, 'createdAt' => new MongoDate(strtotime('+1 day', $datetime) - 1)];
         }
     }
     ModelStatsMemberCampaignLogDaily::batchInsert($statsRows);
     unset($statsRows, $memberCampaignLogDaily, $condition, $memProperty, $memberInfos);
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['properties'][0])) {
         ResqueUtil::log('Missing required arguments accountId or properties!');
         return false;
     }
     $accountId = new MongoId($args['accountId']);
     $property = $args['properties'][0];
     $memberProperty = MemberProperty::getByPropertyId($accountId, $property);
     if (empty($memberProperty)) {
         ResqueUtil::log('Can not find member property with propertyId:' . $property);
         return false;
     }
     $date = empty($args['date']) ? '' : $args['date'];
     $date = TimeUtil::getDatetime($date);
     $year = date('Y', $date);
     $quarter = TimeUtil::getQuarter($date);
     self::generateData($memberProperty, $property, $year, $quarter, $accountId);
     return true;
 }
Exemplo n.º 21
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['key']) || empty($args['header']) || empty($args['accountId'])) {
         ResqueUtil::log(['status' => 'fail to export goods record', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     $keyword = $args['keyword'];
     $header = $args['header'];
     $fileName = $args['key'];
     $accountId = unserialize($args['accountId']);
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $query = new Query();
     if ($keyword == '') {
         $records = $query->from('uhkklpOrder')->select(['_id', 'createdAt', 'name', 'mobile', 'restaurantName', 'address', 'businessForm', 'product'])->where(['accountId' => $accountId])->all();
     } else {
         $records = $query->from('uhkklpOrder')->select(['_id', 'createdAt', 'name', 'mobile', 'restaurantName', 'address', 'businessForm', 'product'])->where(['accountId' => $accountId])->andWhere(['like', 'mobile', $keyword])->all();
     }
     for ($i = 0; $i < count($records); $i++) {
         $records[$i]['createdAt'] = MongodbUtil::MongoDate2String($records[$i]['createdAt'], 'Y-m-d H:i:s', null);
         $records[$i]['_id'] = (string) $records[$i]['_id'];
         $pro = "";
         for ($j = 0; $j < count($records[$i]['product']); $j++) {
             $pro .= "  " . $records[$i]['product'][$j];
         }
         $records[$i]['product'] = $pro;
     }
     $rows = $records;
     ExcelUtil::exportCsv($header, $rows, $filePath, 1);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL]);
         return true;
     } else {
         ResqueUtil::log(['status' => 'fail to export order', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]);
         return false;
     }
 }
Exemplo n.º 22
0
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['header']) || empty($args['key']) || empty($args['channelId']) || empty($args['qrcodeId']) || empty($args['condition'])) {
         ResqueUtil::log(['message' => 'missing param in export qrcode info', 'args' => $args]);
         return false;
     }
     $condition = unserialize($args['condition']);
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $classFunction = '\\backend\\models\\Qrcode::preProcessData';
     $condition = ['condition' => $condition, 'channelId' => $args['channelId'], 'qrcodeId' => $args['qrcodeId']];
     ExcelUtil::processData($args['header'], $filePath, $classFunction, $condition);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL . $args['accountId']]);
         return true;
     } else {
         return false;
     }
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['properties'][0])) {
         ResqueUtil::log('Missing required arguments accountId or properties!');
         return false;
     }
     $accountId = new \MongoId($args['accountId']);
     $property = $args['properties'][0];
     $memberProperty = MemberProperty::findOne(['propertyId' => $property, 'accountId' => $accountId]);
     if (empty($memberProperty)) {
         ResqueUtil::log('Can not find member property with propertyId:' . $property);
         return false;
     }
     $date = empty($args['date']) ? '' : $args['date'];
     $date = TimeUtil::getDatetime($date);
     $month = date('Y-m', $date);
     $startDate = strtotime($month);
     $endDate = strtotime(date('Y-m', strtotime('+1 month', $date)));
     $raw = Member::getCollection()->aggregate([['$unwind' => '$properties'], ['$match' => ['createdAt' => ['$gte' => new \MongoDate($startDate), '$lt' => new \MongoDate($endDate)], 'properties.id' => $memberProperty->_id, 'accountId' => $accountId]], ['$group' => ['_id' => '$properties.value', 'total' => ['$sum' => 1]]]]);
     foreach ($raw as $item) {
         $total = $item['total'];
         $propValue = $item['_id'];
         // save the stats member property monthly
         $statsMemberPropMonthly = ModelStatsMemberPropMonthly::findOne(['propId' => $property, 'propValue' => $propValue, 'month' => $month, 'accountId' => $accountId]);
         if (empty($statsMemberPropMonthly)) {
             $statsMemberPropMonthly = new ModelStatsMemberPropMonthly();
             $statsMemberPropMonthly->propId = $property;
             $statsMemberPropMonthly->propValue = $propValue;
             $statsMemberPropMonthly->month = $month;
             $statsMemberPropMonthly->accountId = $accountId;
         }
         $statsMemberPropMonthly->total = $total;
         ResqueUtil::log($statsMemberPropMonthly->attributes);
         $statsMemberPropMonthly->save();
     }
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['beginTime']) || empty($args['endTime']) || empty($args['type'])) {
         ResqueUtil::log(['error' => 'missing params', 'args' => $args]);
         return false;
     }
     $beginTime = strtotime($args['beginTime']) + 3600 * 24;
     $endTime = strtotime($args['endTime']);
     if ($endTime > time()) {
         $endTime = strtotime(date('Y-m-d', time()));
     }
     $endTime += 3600 * 24;
     $type = new \MongoInt32($args['type']);
     switch ($args['type']) {
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_PARTICIPATE:
             //delete data and create data
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 //create data begin
                 $createWhere = PromotionCodeAnalysis::getCreateTime($t);
                 $campaignIds = CampaignLog::distinct('campaignId', $createWhere);
                 $campaignLogs = [];
                 if (!empty($campaignIds)) {
                     $where = array_merge($createWhere, ['campaignId' => ['$in' => $campaignIds]]);
                     $campaignLogs = CampaignLog::getCollection()->aggregate([['$match' => $where], ['$group' => ['_id' => ['campaignId' => '$campaignId', 'accountId' => '$accountId', 'productId' => '$productId', 'memberId' => '$member.id']]]]);
                 }
                 if (!empty($campaignLogs)) {
                     //get total for take part in a campaign
                     $campaignData = [];
                     foreach ($campaignLogs as $data) {
                         $campaignId = $data['_id']['campaignId'];
                         $key = (string) $campaignId . (string) $data['_id']['productId'];
                         if (isset($campaignData[$key])) {
                             //to sum the total in every product in same campaign
                             $campaignData[$key]['total'] += 1;
                         } else {
                             $product = Product::findByPk($data['_id']['productId']);
                             $productName = empty($product['name']) ? '' : $product['name'];
                             $result = ['productId' => $data['_id']['productId'], 'productName' => $productName, 'campaignId' => $campaignId, 'accountId' => $data['_id']['accountId'], 'createdAt' => new \MongoDate($t - 3600 * 24), 'total' => 1, 'type' => $type];
                             $campaignData[$key] = $result;
                         }
                     }
                     PromotionCodeAnalysis::batchInsert($campaignData);
                     unset($datas, $campaignIds, $campaignData);
                 }
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL:
             //delete data and create data
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 //get campaignlogs in yesterday
                 $campaignLogs = PromotionCodeAnalysis::getMemberCampaignLog(false, $t);
                 //create datas
                 $yesterday = new \MongoDate($t - 2 * 24 * 3600);
                 if (!empty($campaignLogs)) {
                     $campaignData = [];
                     foreach ($campaignLogs as $key => $campaignLog) {
                         //get total the day yesterday
                         $productId = $campaignLog['_id']['productId'];
                         $campaignId = $campaignLog['_id']['campaignId'];
                         $accountId = $campaignLog['_id']['accountId'];
                         $condition = ['productId' => $productId, 'campaignId' => $campaignId, 'accountId' => $accountId, 'createdAt' => $yesterday, 'type' => $type];
                         $yesterdayData = PromotionCodeAnalysis::findOne($condition);
                         if (empty($yesterdayData)) {
                             $yesterdayData['total'] = 0;
                         }
                         $condition = ['campaignId' => $campaignId, 'accountId' => $accountId, 'productId' => $productId];
                         $number = PromotionCodeAnalysis::checkMemberUnique($condition, $t);
                         //subtract the member who is recorded before
                         $total = $yesterdayData['total'] + $number;
                         $campaignLogs[$key]['total'] = $total;
                     }
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 24 * 3600));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
                 //set the default value when the value is not exists
                 PromotionCodeAnalysis::setDefault($t - 2 * 24 * 3600, $type);
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_EVERYDAY_PRIZE:
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 //delete data
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 $campaignLogs = PromotionCodeAnalysis::getCampaignLog(false, $t);
                 if (!empty($campaignLogs)) {
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 3600 * 24));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL_PARTICIPATE:
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 //delete data
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 unset($where);
                 $where = PromotionCodeAnalysis::getCreateTime($t);
                 $campaignLogs = PromotionCodeAnalysis::getMemberAllTimes($where);
                 if (!empty($campaignLogs)) {
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 3600 * 24));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
             }
             break;
     }
     return true;
 }
Exemplo n.º 25
0
 public function perform()
 {
     $args = $this->args;
     $accountId = new \MongoId($args['accountId']);
     $channels = $args['channels'];
     if (!empty($args['storeIds'])) {
         // Sync store data to wechat
         $userId = $args['userId'];
         $storeIds = $args['storeIds'];
         $storeData = [];
         foreach ($storeIds as $key => $storeId) {
             $storeId = new \MongoId($storeId);
             $store = Store::findByPk($storeId);
             $storeData[] = $store->toData();
         }
         $failResult = [];
         foreach ($channels as $channelId) {
             try {
                 $result = \Yii::$app->weConnect->addStore($channelId, $storeData);
                 if (!empty($result['location_id_list']) && in_array(-1, $result['location_id_list'])) {
                     foreach ($result['location_id_list'] as $index => $locationId) {
                         if ($locationId === -1) {
                             $failResult[] = ['channelId' => $channelId, 'storeId' => $storeIds[$index], 'storeName' => $storeData[$index]['business_name']];
                         }
                     }
                 }
             } catch (\Exception $e) {
                 ResqueUtil::log($e);
                 foreach ($storeData as $index => $store) {
                     $failResult[] = ['channelId' => $channelId, 'storeId' => $storeIds[$index], 'storeName' => $store['business_name']];
                 }
             }
         }
         if (!empty($failResult)) {
             \Yii::$app->cache->set(Store::CACHE_PREFIX . $userId . Store::SYNC_TO_WECHAT, $failResult, Store::CACHE_EXPIRE_TIME);
             throw new \Exception('Sync store data to wechat failed.');
         }
     } else {
         // Sync store data from wechat
         $account = Account::findByPk($accountId);
         $syncWechat = [];
         if (!empty($account->syncWechat)) {
             $syncWechat = $account->syncWechat;
         }
         $sizeCondition = ['offset' => static::STORE_START_NUM, 'count' => static::STORE_END_NUM];
         foreach ($channels as $channelId) {
             $stores = \Yii::$app->weConnect->getStores($channelId, $sizeCondition);
             ResqueUtil::log($stores);
             if (!empty($stores['location_list'])) {
                 foreach ($stores['location_list'] as $storeData) {
                     $store = new Store();
                     $store->loadData($storeData);
                     $store->accountId = $accountId;
                     $store->save();
                 }
             }
             array_push($syncWechat, $channelId);
         }
         $account->syncWechat = $syncWechat;
         $account->save();
     }
 }
Exemplo n.º 26
0
 public static function add_worker($queue)
 {
     // $server_workers = self::server_workers(self::get_all_workers($queue));
     // $current_workers = isset($server_workers[self::get_hostname()]) ? $server_workers[self::get_hostname()] : array();
     // if(sizeof($current_workers) > 0) {
     // $pid = pcntl_fork();
     // if($pid == -1) {
     // ResqueUtil::log("Could not fork worker ".$i."\n");
     // }
     // // Child, start the worker
     // else if(!$pid) {
     // // if there are more than 1 types of workers on this machine, we don't know which kind to create. just create the first one.
     // $worker = new Resque_Worker($current_workers[0]['queues']);
     // // TODO: set logLevel
     // //$worker->logLevel = 2;
     // ResqueUtil::log('*** Starting worker '.$worker."\n");
     // // TODO: set interval
     // $worker->work();
     // }
     // return TRUE;
     // }
     // return FALSE;
     $supervisor = new SupervisorClient(SUPERVISOR_SOCK);
     $queueName = $queue . uniqid();
     $result = $supervisor->addProgramToGroup(GROUP_NAME, $queueName, array('command' => 'php ' . REPO_PATH . '/src/protected/modules/resque/components/bin/resque', 'number' => '1', 'redirect_stderr' => 'true', 'autostart' => 'true', 'autorestart' => 'true', 'environment' => 'QUEUE="' . $queue . '", PROGRAM="' . $queueName . '", APP_INCLUDE="' . REPO_PATH . '/src/protected/modules/resque/components/lib/Resque/RequireFile.php"'));
     $result && ResqueUtil::log('Create process ' . $queueName . ' successfully');
     return $result;
 }
Exemplo n.º 27
0
 public function perform()
 {
     # Run task
     $args = $this->args;
     //job update productId
     switch ($args['type']) {
         case self::JOB_UPDATE:
             if (!empty($args['oldProductId']) && !empty($args['newProductId'])) {
                 ModelPromotionCode::updateAll(['$set' => ['productId' => new \MongoId($args['newProductId'])]], ['productId' => new \MongoId($args['oldProductId']), 'isUsed' => false]);
                 return true;
             }
             break;
         case self::JOB_GENERATE:
             //job generateCodes
             if (empty($accountId = $args['accountId']) || empty($productId = $args['productId']) || !array_key_exists('count', $args)) {
                 ResqueUtil::log(['error' => 'generate codes params error', 'param' => $args]);
                 return false;
             }
             $createdAt = new \MongoDate(time(), 0);
             $count = $args['count'];
             $successCount = 0;
             $batchCount = $count / self::PER_BATCH;
             for ($i = 0; $i < $batchCount; $i++) {
                 $restCount = $count - self::PER_BATCH * $i;
                 $rowsCount = $restCount >= self::PER_BATCH ? self::PER_BATCH : $restCount;
                 $codes = ModelPromotionCode::generateCodes($rowsCount, new \MongoId($productId));
                 ResqueUtil::log(['info' => 'generate code ok', 'param' => $args]);
                 $promotionCodes = [];
                 foreach ($codes as $code) {
                     $promotionCode = ['productId' => new \MongoId($productId), 'code' => $code, 'isUsed' => false, 'accountId' => new \MongoId($accountId), 'createdAt' => $createdAt, 'random' => rand()];
                     $promotionCodes[] = $promotionCode;
                 }
                 ResqueUtil::log(['info' => 'generate codes', 'param' => $args]);
                 $result = ModelPromotionCode::batchInsert($promotionCodes);
                 if (!$result) {
                     ResqueUtil::log(['error' => 'save codes failed', 'param' => $args, 'success count' => $successCount]);
                 } else {
                     $successCount += $rowsCount;
                 }
             }
             //change the isBindCode
             $this->changeProductStatus(new \MongoId($productId));
             return true;
         case self::JOB_DELETE:
             if (!empty($args['productId'])) {
                 $productId = new \MongoId($args['productId']);
                 $condition = ['productId' => $productId];
             } else {
                 ResqueUtil::log(['error' => 'delete codes params error', 'param' => $args]);
                 return false;
             }
             if (!empty($args['createdAt'])) {
                 $createdAt = new \MongoDate($args['createdAt']);
                 $condition = array_merge($condition, ['createdAt' => $createdAt]);
             }
             //if delete all code successfully,judge to change isBindCode
             if (ModelPromotionCode::deleteAll($condition)) {
                 $count = ModelPromotionCode::count(["productId" => $productId]);
                 if ($count <= 0) {
                     Product::updateAll(['isBindCode' => false], ['_id' => $productId]);
                 }
             }
             return true;
         case self::JOB_INSERT:
             if (!isset($args['accountId']) || !isset($args['productId']) || !isset($args['filename'])) {
                 ResqueUtil::log(['error' => 'missing param accountId or productId or filename', 'param' => $args]);
                 return false;
             }
             if (!isset($args['import'])) {
                 $args['import'] = false;
             }
             $redis = \Yii::$app->cache->redis;
             $createdAt = new \MongoDate(time());
             $product = Product::findOne($args['productId']);
             if (empty($product)) {
                 ResqueUtil::log(['error' => 'product has been deleted', 'param' => $args]);
                 return false;
             }
             $productId = $product['sku'];
             $cacheKey = $args['accountId'] . "_" . $productId . "_" . $args['filename'];
             $cacheSet = PromotionCheckCode::SET_HEAD . md5($cacheKey);
             $total = $redis->scard($cacheSet);
             //redis hash key name
             $cacheHash = md5($cacheKey);
             $k = 0;
             $promotionCodes = [];
             for ($i = 1; $i <= $total; ++$i) {
                 // insert the code
                 if ($args['import']) {
                     $key = $redis->spop($cacheSet);
                     if (!empty($key)) {
                         $promotionCode = ['productId' => new \MongoId($args['productId']), 'code' => $key, 'isUsed' => false, 'accountId' => new \MongoId($args['accountId']), 'createdAt' => $createdAt];
                         $promotionCodes[$k++] = $promotionCode;
                     }
                     //set default value in redis to avoid to insert data in mongo failly
                     $redis->HSET($cacheHash, 'right', 0);
                     $redis->HSET($cacheHash, 'wrong', self::PROMO_CODE_REPEATED);
                     //have data to store
                     if (count($promotionCodes) > 0) {
                         if ($k % self::PER_BATCH == 0 || $i == $total) {
                             try {
                                 $result = ModelPromotionCode::batchInsert($promotionCodes);
                             } catch (MongoDuplicateKeyException $e) {
                                 return false;
                             }
                             $promotionCodes = [];
                             if (!$result) {
                                 ResqueUtil::log(['error' => 'save codes failed', 'param' => $args, 'success count' => $i - count($promotionCodes)]);
                             }
                         }
                     }
                 }
             }
             $redis->del($cacheSet);
             //change the isBindCode
             $this->changeProductStatus($product['_id']);
             //update duplicate key error
             $redis->HSET($cacheHash, 'wrong', 0);
             return true;
             break;
         case self::JOB_DELETE_REDIS_CODE:
             if (!isset($args['filename']) || !isset($args['productId']) || !isset($args['accountId'])) {
                 ResqueUtil::log(['error' => 'missing param', 'param' => $args]);
                 return false;
             }
             $redis = \Yii::$app->cache->redis;
             $cacheSet = PromotionCheckCode::SET_HEAD . md5($args['accountId'] . '_' . $args['productId'] . '_' . $args['filename']);
             $redis->del($cacheSet);
             unset($commonKey, $cacheTotalKey, $count);
             return true;
             break;
         default:
             break;
     }
 }
Exemplo n.º 28
0
 public static function afterRetry($payload, $enqueuedAt)
 {
     $enqueuedAt = ResqueScheduler::getTimestamp($enqueuedAt);
     ResqueUtil::log('[afterRetry]Failed job ' . CJSON::encode($payload) . 'is enqueued again at ' . $enqueuedAt);
 }
Exemplo n.º 29
0
 /**
  * Output a given log message to STDOUT.
  *
  * @param string $message
  *            Message to output.
  * @return boolean True if the message is logged
  */
 public function log($message, $code = self::LOG_TYPE_INFO)
 {
     if ($this->logLevel === self::LOG_NONE) {
         return false;
     }
     /*
      * if ($this->logger === null) { if ($this->logLevel === self::LOG_NORMAL && $code !== self::LOG_TYPE_DEBUG) { ResqueUtil::log("*** " . $message['message'] . "\n"); } else if ($this->logLevel === self::LOG_VERBOSE) { ResqueUtil::log("** [" . strftime('%T %Y-%m-%d') . "] " . $message['message'] . "\n"); } else { return false; } return true; } else {
      */
     $extra = array();
     if (is_array($message)) {
         $extra = $message['data'];
         $message = $message['message'];
     }
     if (!isset($extra['worker'])) {
         if ($this->child > 0) {
             $extra['worker'] = $this->hostname . ':' . getmypid();
         } else {
             list($host, $pid, $queues) = explode(':', (string) $this, 3);
             $extra['worker'] = $host . ':' . $pid;
         }
     }
     if (($this->logLevel === self::LOG_NORMAL || $this->logLevel === self::LOG_VERBOSE) && $code !== self::LOG_TYPE_DEBUG) {
         if ($this->logger === null) {
             ResqueUtil::log($message);
         } else {
             switch ($code) {
                 case self::LOG_TYPE_INFO:
                     $this->logger->addInfo($message, $extra);
                     break;
                 case self::LOG_TYPE_WARNING:
                     $this->logger->addWarning($message, $extra);
                     break;
                 case self::LOG_TYPE_ERROR:
                     $this->logger->addError($message, $extra);
                     break;
                 case self::LOG_TYPE_CRITICAL:
                     $this->logger->addCritical($message, $extra);
                     break;
                 case self::LOG_TYPE_ALERT:
                     $this->logger->addAlert($message, $extra);
             }
         }
     } else {
         if ($code === self::LOG_TYPE_DEBUG && $this->logLevel === self::LOG_VERBOSE) {
             if ($this->logger === null) {
                 ResqueUtil::log($message);
             } else {
                 $this->logger->addDebug($message, $extra);
             }
         } else {
             return false;
         }
     }
     return true;
     // }
 }
Exemplo n.º 30
0
 public static function afterRetry($payload, $enqueuedAt)
 {
     ResqueUtil::log('[afterRetry]Failed job ' . CJSON::encode($payload) . 'is enqueued again at ' . $enqueuedAt);
 }