Skip to content

Commit

Permalink
store send log after queue finished
Browse files Browse the repository at this point in the history
  • Loading branch information
druphliu committed Jun 4, 2015
1 parent 9d2f079 commit 6505cf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
25 changes: 15 additions & 10 deletions protected/components/yii-resque/Worker/DailySms.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@ class Worker_DailySms
public function setUp()
{
# Set up environment for this job
// echo "Set up\n";
echo "Set up\n";
}

public function perform()
{
# Run task
$logId = $this->args['id'];
$logModel = SmsLogModel::model()->findByPk($logId);
$accountId = $logModel->accountId;
$jobId= $this->job->payload['id'];
$phone = $this->args['phone'];
$accountId = $this->args['accountId'];
$accountInfo = AccountModel::model()->findByPk($accountId);
$content = $logModel->content . $accountInfo->template;
$content = $this->args['content'] . $accountInfo->template;
$logModel = new SmsLogModel();
$logModel->phone = $phone;
$logModel->accountId = $accountId;
$logModel->type = SmsLogModel::TYPE_DAILY;
$logModel->content = $content;
$logModel->queue_id = $jobId;
if (class_exists($accountInfo->class)) {
$class = new $accountInfo->class;
$result = $class->sendOneSms($logModel->phone,$content , $accountInfo->name, $accountInfo->pswd, $accountInfo->user_id);
$result = $class->sendOneSms($phone,$content , $accountInfo->name, $accountInfo->pswd, $accountInfo->user_id);
if ($result['success']) {
$logModel->status = 1;
} else {
$logModel->status = -1;
$logModel->errorMsg = $result['msg'];
}

}else{
$logModel->status = -1;
$logModel->errorMsg = 'utils class not exit';
}
$logModel->content = $content;
$logModel->save();
// echo "Run\n";
echo "Run".$jobId."\n";
// echo "Run\n";
}

public function tearDown()
{
# Remove environment for this job
// echo "Tear down\n";
echo "Tear down\n";
}
}
23 changes: 11 additions & 12 deletions protected/controllers/SmsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function actionIndex()
$sign = Yii::app()->request->getParam('sign');
$phone = Yii::app()->request->getParam('phone');
$content = Yii::app()->request->getParam('content');
$type = Yii::app()->request->getParam('type');
$type = Yii::app()->request->getParam('type', SmsLogModel::TYPE_DAILY);
//check sign
$secret = $this->groupInfo->secret;
$string = md5($phone . $content . $secret);
Expand All @@ -40,21 +40,22 @@ public function actionIndex()
}
//入队列
//判断类型 即时不用入队 非即时入队
//入库
$logModel = new SmsLogModel();
$logModel->content = $content;
$logModel->phone = $phone;
$logModel->accountId = $this->groupInfo->accountId;

switch ($type) {
case SmsLogModel::TYPE_ON_TIME:
//入库
$logModel = new SmsLogModel();
$logModel->phone = $phone;
$logModel->accountId = $this->groupInfo->accountId;
$logModel->type = SmsLogModel::TYPE_ON_TIME;
$logModel->content = $content;
$accountInfo = AccountModel::model()->findByPk($this->groupInfo->accountId);
if (class_exists($accountInfo->class)) {
$class = new $accountInfo->class;
$content = $logModel->content . $accountInfo->template;
$logModel->content = $content;
$result = $class->sendOneSms($logModel->phone, $content, $accountInfo->name, $accountInfo->pswd, $accountInfo->user_id);
if ($result['success']) {
$logModel->content = $content;
$logModel->status = 1;
$this->result['success'] = true;
$this->result['msg'] = '';
Expand All @@ -66,19 +67,17 @@ public function actionIndex()
$logModel->status = -1;
$this->result['msg'] = $logModel->errorMsg = 'utils class not exit';
}
$logModel->save();
break;
case SmsLogModel::TYPE_DAILY:
default:
$logModel->type = SmsLogModel::TYPE_DAILY;
$worker = 'Worker_DailySms';
break;
}
$logModel->save();
if ($type != SmsLogModel::TYPE_ON_TIME) {
$queueId = Yii::app()->resque->createJob('DailySms', $worker, $args = array('id' => $logModel->id));
$queueId = Yii::app()->resque->createJob('DailySms', $worker,
$args = array('phone' => $phone, 'content' => $content, 'accountId' => $this->groupInfo->accountId));
if ($queueId) {
$logModel->queue_id = $queueId;
$logModel->save();
$this->result['success'] = true;
$this->result['msg'] = '';
$this->result['queueId'] = $queueId;
Expand Down

0 comments on commit 6505cf5

Please sign in to comment.