일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- ubuntu
- MYSQL
- Vue
- Cordova
- AWS
- pip
- selenium
- OAuth
- 안드로이드
- Android
- Prometheus
- https
- nginx
- Passport
- SSH
- alb
- PostgreSQL
- MacOS
- php
- 파이썬
- Laravel
- 구글 API
- python
- 해시키
- Vue.js
- flutter
- FCM
- node
- window
- mac
- Today
- Total
print( sjw.iq == 200) output : true
[Lalavel Framework] Queue 사용하기 본문
안녕하세요! 요즘 너무 바빠서... 블로그 근처도 오지 못했었습니다...
오늘은 laravel에서 background로 프로세스를 돌릴 수 있는 Queue에 대해서 포스팅을 해보겠습니다.
전에 외주개발로 해당 작업을 진행했었는데요...
저희 서비스에도 필요하게 됬는데 잊어버려서 정리를 하려고 합니다!
우선 데이터베이스 테이블이 필요합니다.
라라벨 공식 문서를 살펴보면 Queue를 사용가능한 데이터베이스 들이 나와있습니다!
저는 Mysql을 자주 사용하기 때문에 Mysql로 설정해보겠습니다!
1. 드라이버 사전준비사항
database 큐 드라이버를 사용하기 위해서는 Job들을 담아둘 데이터베이스 테이블이 필요합니다. 이 테이블을 추가하기 위한 마이그레이션을 생성하려면 queue:table 아티즌 명령을 실행하면 됩니다. 마이그레이션 파일이 생성되고 나면 migrate 명령어를 사용하여 데이터베이스 테이블을 생성할 수 있습니다:
명령어 : php artisan queue:table
php artisan migrate
그리고
2. Job 클래스 생성하기
기본적으로, 애플리케이션을 위한 모든 큐 Job들은 app/Jobs 디렉토리에 저장됩니다. app/Jobs 디렉토리가 존재하지 않는다면, make:job 아티즌 명령어를 실행할 때 생성됩니다. 새로운 큐 Job 클래스는 아티즌 CLI를 통해서 생성할 수 있습니다:
명령어 : php artisan make:job InsertReminderEvent
/app/Jobs/ 경로에 다음과 같이 파일이 생성됩니다.
<?php
namespace App\Jobs;
use App\Http\Controllers\NewsController;
use App\News;
use Illuminate\Bus\Queueable;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendNews implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $uid;
private $targetContentId;
private $type;
private $contentId;
private $sendPush;
private $newsController;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($uid, $targetContentId, $type, $contentId, $sendPush)
{
$this->uid = $uid;
$this->targetContentId = $targetContentId;
$this->type = $type;
$this->contentId = $contentId;
$this->sendPush = $sendPush;
$this->newsController = new NewsController(new News());
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
$this->newsController->sendNewsFromJobs($this->uid,$this->targetContentId,$this->type,$this->contentId,$this->sendPush);
}
}
생성된 jobs는 다음과 같은 구조로 되어있습니다.
# handle 함수에 실행하려는 로직을 작성하면 되고
# Queue를 사용하려면
$job = new SendNews($uid,$targetContentId,$type,$contentId,$sendPush);
dispatch($job)->onConnection('database');
위와 같은 코드를 작성해주면 됩니다.
# 로컬에서 테스트 방법
php artisan queue:work database --queue
간결하게 적어주는 방법도 있으나
이와 같이 적어주는게 개인적으로 에러가 없더라구요~
3. 우분투에서 Queue 설정하는 좋은 방법 - Supervisor
설치 명령어 : sudo apt-get install supervisor
위치 : /etc/supervisor/conf.d/
laravel-worker.conf 파일을 만들어주고
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/sjwiq200/artisan queue:work database --queue --sleep=3 --tries=3
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/worker.log
실행 방법!
1. sudo supervisorctl reread
2. sudo supervisorctl update
3. sudo supervisorctl start laravel-worker:*
# 중지
sudo service supervisor stop
# 재실행
sudo service supervisor start
다음과 같이 이용하시면 Queue를 쉽게 이용할 수 있습니다.!
자세하게 적지는 않았는데 조만간 저희 서비스에 적용시킬 때
다시 한번 수정하러 오겠습니다~
그럼 안녕히계세요.
'PHP' 카테고리의 다른 글
[Laravel] laravelcollective/html https 적용하기 (0) | 2020.02.04 |
---|---|
[Laravel] Laravel에서 AWS SES를 이용한 Mail 전송하기 (0) | 2020.01.28 |
[Laravel] Storage 권한 관련 문제 (0) | 2020.01.16 |
[Laravel] Excel 파일 관리하기 (0) | 2019.10.31 |
[PHP] Ubuntu18.04에 php, apache, laravel 설치 및 설정 (0) | 2019.08.01 |