CI4 공식 Queue 시스템

codeigniter4/queue 패키지 — Database 핸들러(SQLite)로 잡 추가·처리·재시도·실패 관리

0
대기(Pending)
0
처리 중
0
실패(Failed)
잡 추가 (Push)
잡 처리
처리 로그
// 잡을 추가하고 처리 버튼을 눌러보세요
대기 중인 잡 0
ID잡 유형데이터상태시도등록시간
대기 중인 잡이 없습니다.
실패한 잡 0
ID잡 유형예외 메시지실패시간액션
실패한 잡이 없습니다.
1. 잡 클래스 작성
// app/Jobs/Queue/SendEmailJob.php
namespace App\Jobs\Queue;

use CodeIgniter\Queue\BaseJob;
use CodeIgniter\Queue\Interfaces\JobInterface;

class SendEmailJob extends BaseJob implements JobInterface
{
    protected int $retryAfter = 30; // 재시도 대기 (초)
    protected int $tries      = 3;  // 최대 시도 횟수

    public function process(): void
    {
        $to = $this->data['to'];

        // 실제 이메일 발송 로직
        // service('email')->send(...);

        log_message('info', "이메일 발송: {$to}");
    }
}
2. 잡 핸들러 등록
// app/Config/Queue.php
public array $jobHandlers = [
    'send-email'      => \App\Jobs\Queue\SendEmailJob::class,
    'process-data'    => \App\Jobs\Queue\ProcessDataJob::class,
    'generate-report' => \App\Jobs\Queue\GenerateReportJob::class,
];
3. 잡 추가 (Push)
// 즉시 실행
service('queue')->push(
    'playground',   // 큐 이름
    'send-email',   // 잡 핸들러 키
    ['to' => 'user@example.com', 'subject' => '안녕']
);

// 30초 지연 후 실행
service('queue')
    ->setDelay(30)
    ->push('playground', 'send-email', $data);

// 체이닝 (순서 보장)
service('queue')->chain(function($chain) {
    $chain->push('process-data',    ['batch_id' => 'A']);
    $chain->push('generate-report', ['type'     => 'daily']);
});
4. 워커 실행 (CLI)
# 큐 워커 시작 (데몬으로 실행)
php spark queue:work playground

# 옵션 지정
php spark queue:work playground \
    --sleep 5 \       # 잡 없을 때 대기 초
    --tries 3 \       # 재시도 횟수
    --retry-after 60  # 재시도 대기 초

# 잡 1건만 처리하고 종료
php spark queue:work playground --max-jobs 1

# 실패 잡 목록 확인
php spark queue:failed

# 실패 잡 재시도
php spark queue:retry --id 5
php spark queue:retry --queue playground
5. 설치 및 설정
# 패키지 설치
composer require codeigniter4/queue

# 설정 파일 생성
php spark queue:publish

# 마이그레이션 실행 (queue_jobs, queue_jobs_failed 테이블 생성)
php spark migrate --all
지원 핸들러
  • Database — MySQL, SQLite, PostgreSQL 이 예제 (SQLite)
  • Redis — 고성능 인메모리 큐
  • Predis — PHP Redis 클라이언트
  • RabbitMQ — 엔터프라이즈 메시지 브로커
#40 DIY 큐 vs #42 공식 패키지 비교
항목 #40 DIY 커스텀 큐 #42 codeigniter4/queue
구현 방식 직접 구현 (QueueManager 라이브러리) 공식 패키지 (Composer 설치)
테이블 custom_queue_jobs, queue_failed_jobs queue_jobs, queue_jobs_failed
잡 클래스 커스텀 BaseJob 추상 클래스 공식 CodeIgniter\Queue\BaseJob
핸들러 Database only (SQLite) Database / Redis / Predis / RabbitMQ
워커 CLI 없음 (웹에서 직접 처리) php spark queue:work 데몬
우선순위 없음 지원 (setPriority())
지연 실행 available_at 필드 setDelay(seconds)
체이닝 없음 chain(callback) 지원
재시도 수동 retry 구현 자동 ($tries, $retryAfter)
이벤트 없음 QueueEventManager
추천 상황 큐 시스템 동작 원리 이해, 경량 프로젝트 실제 프로젝트, 프로덕션 환경