/**
  * メールマガジン送信
  *
  * @access public
  * @param int $mail_magazine_id メルマガID
  * @param int $administrator_id 管理者ID
  * @param int $limit 1回で処理する件数
  * @return void
  * @author ida
  */
 public function run($mail_magazine_id, $administrator_id = 0, $limit = 1000)
 {
     $this->openLog($mail_magazine_id);
     $this->log('メルマガID: ' . $mail_magazine_id . ' の送信を開始します' . "\n");
     if (\Model_Mail_Magazine::isProcess()) {
         $this->log('メルマガ送信中のため送信できません' . "\n");
         exit;
     }
     $mail_magazine = \Model_Mail_Magazine::startProcess($mail_magazine_id);
     $offset = 0;
     if (!is_numeric($limit) || $limit <= 0) {
         $limit = 1000;
     }
     $total_count = 0;
     $success_count = 0;
     $fail_count = 0;
     while ($limit > 0) {
         $mail_magazine_users = \Model_Mail_Magazine_User::findByMailMagazineId($mail_magazine_id, $offset, $limit);
         if (count($mail_magazine_users) == 0) {
             break;
         }
         $is_stop = false;
         $replace_data = $this->makeReplaceData($mail_magazine);
         foreach ($mail_magazine_users as $mail_magazine_user) {
             try {
                 usleep(300000);
                 if (!\Model_Mail_Magazine::isProcess($mail_magazine_id)) {
                     $is_stop = true;
                     $this->log($mail_magazine_user->user_id . ": cancel.\n");
                     break;
                 }
                 $send_result = $this->send($mail_magazine_user, $mail_magazine, $replace_data);
                 \Model_Mail_Magazine_User::updateStatus(array('send_status' => $send_result, 'error' => null, 'updated_user' => $administrator_id), array('mail_magazine_user_id' => $mail_magazine_user->mail_magazine_user_id));
                 $this->log($mail_magazine_user->user_id . " : success\n");
                 $success_count++;
             } catch (\Exception $e) {
                 $message = $e->getMessage();
                 $this->log($mail_magazine_user->user_id . ' : fail ' . $message . "\n");
                 \Model_Mail_Magazine_User::updateStatus(array('send_status' => \Model_Mail_Magazine_User::SEND_STATUS_ERROR_END, 'error' => $message, 'updated_user' => $administrator_id), array('mail_magazine_user_id' => $mail_magazine_user->mail_magazine_user_id));
                 $fail_count++;
             }
             $total_count++;
         }
         $offset += $limit;
     }
     $this->log('[total] ' . $total_count . ' [success] ' . $success_count . ' [fail] ' . $fail_count . "\n");
     $this->log('送信を終了しました');
     $this->closeLog();
     $mail_magazine->send_datetime = \Date::forge()->format('mysql');
     if ($is_stop) {
         $mail_magazine->send_status = \Model_Mail_Magazine::SEND_STATUS_CANCEL;
     } else {
         $mail_magazine->send_status = \Model_Mail_Magazine::SEND_STATUS_NORMAL_END;
     }
     $mail_magazine->save();
 }
 /**
  * 送信中止
  *
  * @access public
  * @param
  * @return void
  * @author ida
  */
 public function action_stop()
 {
     $this->template = '';
     $success = false;
     $mail_magazine_id = \Input::post('mail_magazine_id');
     try {
         $is_process = \Model_Mail_Magazine::isProcess($mail_magazine_id);
         if ($is_process) {
             \Model_Mail_Magazine::cancelProcess($mail_magazine_id);
         }
         $success = true;
     } catch (\Exception $e) {
         $message = $e->getMessage();
         $success = false;
     }
     $response = array();
     if ($success) {
         if ($is_process) {
             $response = array('status' => 200);
         } else {
             $response = array('status' => 300);
         }
     } else {
         $response = array('status' => 400, 'message' => $message);
     }
     return $this->responseJson($response);
 }