Class: Saro::Dat::DatCmsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/saro/dat/dat_cms_manager.rb

Constant Summary collapse

DAT_CMS_API_VERSION =
"v1"
STOP_JOIN_TIMEOUT_SECONDS =

stop waits at most this long for an in-flight HTTP sync to finish. The thread is never killed: past the grace period it is left to complete its request and exit on its own at the top of the loop.

1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, token:, interval_seconds: 60, verify_only: false, dat_manager: nil) ⇒ DatCmsManager

Returns a new instance of DatCmsManager.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/saro/dat/dat_cms_manager.rb', line 21

def initialize(uri:, token:, interval_seconds: 60, verify_only: false, dat_manager: nil)
  @uri = uri
  @token = token
  @interval_seconds = interval_seconds
  @verify_only = verify_only
  @manager = dat_manager || DatManager.new
  @version = 0
  # Two separate locks: @lock guards an in-flight sync (taken non-blocking,
  # held across the HTTP request), @lifecycle guards the stop flag and the
  # sleep condition. Sharing one lock made `stop` block for the whole
  # request timeout.
  @lock = Mutex.new
  @lifecycle = Mutex.new
  @stop_cond = ConditionVariable.new
  @stopped = false
  @logger = Logger.new($stdout)
  @logger.level = Logger::DEBUG
  # 최초 sync 실패는 여전히 생성을 막지 않는다(list.md F-3). 다만 이제 로그로만
  # 사라지지 않고 #last_error 로 조회할 수 있다.
  @last_error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::CMS_NOT_SYNCED)

  sync

  if @interval_seconds > 0
    schedule_sync
  end
end

Instance Attribute Details

#last_errorObject (readonly)

마지막 동기화 실패. 한 번도 성공하지 못했으면 DAT_CMS_NOT_SYNCED, 정상이면 nil. 재시도 여부는 err.retry 로 판정한다.



69
70
71
# File 'lib/saro/dat/dat_cms_manager.rb', line 69

def last_error
  @last_error
end

Class Method Details

.builderObject



194
195
196
# File 'lib/saro/dat/dat_cms_manager.rb', line 194

def self.builder
  DatCmsManagerBuilder.new
end

.http_status_error(status) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/saro/dat/dat_cms_manager.rb', line 172

def self.http_status_error(status)
  case status
  when 401 then Saro::Dat::Error.new(Saro::Dat::ErrorCode::CMS_UNAUTHORIZED, "http 401")
  when 403 then Saro::Dat::Error.new(Saro::Dat::ErrorCode::CMS_FORBIDDEN, "http 403")
  when 404 then Saro::Dat::Error.new(Saro::Dat::ErrorCode::CMS_ENDPOINT_NOT_FOUND, "http 404")
  when 500..599 then Saro::Dat::Error.new(Saro::Dat::ErrorCode::CMS_SERVER_ERROR, "http #{status}")
  else Saro::Dat::Error.new(Saro::Dat::ErrorCode::CMS_HTTP_STATUS, "http #{status}")
  end
end

Instance Method Details

#get_managerObject



182
183
184
# File 'lib/saro/dat/dat_cms_manager.rb', line 182

def get_manager
  @manager
end

#issue(plain, secure) ⇒ Object



186
187
188
# File 'lib/saro/dat/dat_cms_manager.rb', line 186

def issue(plain, secure)
  @manager.issue(plain, secure)
end

#parse(dat) ⇒ Object



190
191
192
# File 'lib/saro/dat/dat_cms_manager.rb', line 190

def parse(dat)
  @manager.parse(dat)
end

#stopObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/saro/dat/dat_cms_manager.rb', line 49

def stop
  thread = nil
  @lifecycle.synchronize do
    return if @stopped
    @stopped = true
    # Wakes the scheduler out of its sleep immediately instead of killing
    # it in the middle of a request.
    @stop_cond.broadcast
    thread = @thread
  end
  thread&.join(STOP_JOIN_TIMEOUT_SECONDS)
  nil
end

#stopped?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/saro/dat/dat_cms_manager.rb', line 63

def stopped?
  @lifecycle.synchronize { @stopped }
end

#syncObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/saro/dat/dat_cms_manager.rb', line 75

def sync
  err = sync_or_raise
  @last_error = nil
  err
rescue Saro::Dat::Error => e
  # 상태 신호는 실패로 기록하지 않는다 — 이전 동기화가 도는 중일 뿐이다.
  unless e.retry == :state
    @last_error = e
    @logger.error("[CRITICAL] DAT CMS SYNC #{@uri}: #{e.code} #{e.detail}")
  end
  nil
rescue StandardError => e
  @last_error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::CMS_UNKNOWN, "unclassified cms failure", cause: e)
  @logger.error("[CRITICAL] DAT CMS SYNC #{@uri}: #{e.message}")
  nil
end

#versionObject



71
72
73
# File 'lib/saro/dat/dat_cms_manager.rb', line 71

def version
  @version
end