worker service 매물 등록 및 redis 다운시 처리
This commit is contained in:
@@ -4,7 +4,7 @@ namespace Config;
|
||||
|
||||
use CodeIgniter\Config\BaseConfig;
|
||||
use CodeIgniter\Session\Handlers\BaseHandler;
|
||||
use CodeIgniter\Session\Handlers\FileHandler;
|
||||
use CodeIgniter\Session\Handlers\DatabaseHandler;
|
||||
use CodeIgniter\Session\Handlers\RedisHandler;
|
||||
|
||||
class Session extends BaseConfig
|
||||
@@ -15,14 +15,11 @@ class Session extends BaseConfig
|
||||
* --------------------------------------------------------------------------
|
||||
*
|
||||
* The session storage driver to use:
|
||||
* - `CodeIgniter\Session\Handlers\FileHandler`
|
||||
* - `CodeIgniter\Session\Handlers\DatabaseHandler`
|
||||
* - `CodeIgniter\Session\Handlers\MemcachedHandler`
|
||||
* - `CodeIgniter\Session\Handlers\RedisHandler`
|
||||
*
|
||||
* @var class-string<BaseHandler>
|
||||
*/
|
||||
// public string $driver = FileHandler::class;
|
||||
public string $driver = RedisHandler::class;
|
||||
|
||||
/**
|
||||
@@ -51,43 +48,59 @@ class Session extends BaseConfig
|
||||
*
|
||||
* The location to save sessions to and is driver dependent.
|
||||
*
|
||||
* For the 'files' driver, it's a path to a writable directory.
|
||||
* WARNING: Only absolute paths are supported!
|
||||
*
|
||||
* For the 'database' driver, it's a table name.
|
||||
* Please read up the manual for the format with other session drivers.
|
||||
* For Redis: tcp://host:port?database=n&password=xxx
|
||||
*
|
||||
* IMPORTANT: You are REQUIRED to set a valid save path!
|
||||
*
|
||||
* For Redis: tcp://host:port?database=n&password=xxx
|
||||
*/
|
||||
// public string $savePath = WRITEPATH . 'session';
|
||||
public string $savePath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Redis 설정: .env 우선, 없으면 Docker 환경변수 사용
|
||||
if ($this->driver === RedisHandler::class) {
|
||||
// .env 파일이 우선, 없으면 Docker compose 환경변수 사용
|
||||
$redisHost = env('SESSION_REDIS_HOST') ?: getenv('REDIS_HOST') ?: '127.0.0.1';
|
||||
$redisPort = env('SESSION_REDIS_PORT') ?: getenv('REDIS_PORT') ?: '6379';
|
||||
$redisDatabase = env('SESSION_REDIS_DATABASE') ?: getenv('REDIS_DATABASE') ?: '0';
|
||||
$redisPassword = env('SESSION_REDIS_PASSWORD') ?: getenv('REDIS_PASSWORD') ?: '';
|
||||
// 환경변수로 강제로 Database 모드 설정 가능 (Redis 장애 시 수동 전환)
|
||||
$forceDatabase = env('SESSION_FORCE_DATABASE', false);
|
||||
|
||||
$this->savePath = sprintf(
|
||||
'tcp://%s:%s?database=%s',
|
||||
$redisHost,
|
||||
$redisPort,
|
||||
$redisDatabase
|
||||
);
|
||||
// Redis 설정: Redis 연결 실패 시 Database로 폴백
|
||||
if ($this->driver === RedisHandler::class && !$forceDatabase) {
|
||||
helper('redis');
|
||||
|
||||
try {
|
||||
// Redis 연결 테스트
|
||||
$testRedis = get_redis_connection('session');
|
||||
|
||||
if (!$testRedis) {
|
||||
throw new \Exception('Redis connection failed');
|
||||
}
|
||||
|
||||
// Redis 정상 - Redis 설정 사용
|
||||
$config = get_redis_config('session');
|
||||
|
||||
$this->savePath = sprintf(
|
||||
'tcp://%s:%s?database=%s',
|
||||
$config['host'],
|
||||
$config['port'],
|
||||
$config['database']
|
||||
);
|
||||
|
||||
if (!empty($redisPassword)) {
|
||||
$this->savePath .= '&password=' . $redisPassword;
|
||||
if (!empty($config['password'])) {
|
||||
$this->savePath .= '&password=' . $config['password'];
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Redis 실패 - DatabaseHandler로 폴백
|
||||
log_message('warning', 'Session: Redis unavailable (' . $e->getMessage() . '), falling back to DatabaseHandler');
|
||||
$this->driver = DatabaseHandler::class;
|
||||
$this->savePath = 'ci_sessions'; // 테이블 이름
|
||||
}
|
||||
} else {
|
||||
$this->savePath = WRITEPATH . 'session';
|
||||
// Database 강제 모드 또는 기본 Database 설정
|
||||
if ($forceDatabase && $this->driver === RedisHandler::class) {
|
||||
log_message('info', 'Session: Forced to use DatabaseHandler (SESSION_FORCE_DATABASE=true)');
|
||||
$this->driver = DatabaseHandler::class;
|
||||
}
|
||||
$this->savePath = 'ci_sessions';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user