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 =
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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/saro/dat/dat_cms_manager.rb', line 18

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
  @lock = Mutex.new
  @lifecycle = Mutex.new
  @stop_cond = ConditionVariable.new
  @stopped = false
  @logger = Logger.new($stdout)
  @logger.level = Logger::DEBUG
  @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)

Returns the value of attribute last_error.



56
57
58
# File 'lib/saro/dat/dat_cms_manager.rb', line 56

def last_error
  @last_error
end

Class Method Details

.builderObject



165
166
167
# File 'lib/saro/dat/dat_cms_manager.rb', line 165

def self.builder
  DatCmsManagerBuilder.new
end

.http_status_error(status) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/saro/dat/dat_cms_manager.rb', line 143

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



153
154
155
# File 'lib/saro/dat/dat_cms_manager.rb', line 153

def get_manager
  @manager
end

#issue(plain, secure) ⇒ Object



157
158
159
# File 'lib/saro/dat/dat_cms_manager.rb', line 157

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

#parse(dat) ⇒ Object



161
162
163
# File 'lib/saro/dat/dat_cms_manager.rb', line 161

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

#stopObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/saro/dat/dat_cms_manager.rb', line 40

def stop
  thread = nil
  @lifecycle.synchronize do
    return if @stopped
    @stopped = true
    @stop_cond.broadcast
    thread = @thread
  end
  thread&.join(STOP_JOIN_TIMEOUT_SECONDS)
  nil
end

#stopped?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/saro/dat/dat_cms_manager.rb', line 52

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

#syncObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/saro/dat/dat_cms_manager.rb', line 62

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



58
59
60
# File 'lib/saro/dat/dat_cms_manager.rb', line 58

def version
  @version
end