setting.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Setting < ApplicationModel
  3. store :options
  4. store :state_current
  5. store :state_initial
  6. store :preferences
  7. before_create :state_check, :set_initial, :check_broadcast
  8. before_update :state_check, :check_broadcast
  9. after_create :reset_change_id
  10. after_update :reset_change_id
  11. attr_accessor :state
  12. @@current = {} # rubocop:disable Style/ClassVars
  13. @@raw = {} # rubocop:disable Style/ClassVars
  14. @@change_id = nil # rubocop:disable Style/ClassVars
  15. @@last_changed_at = nil # rubocop:disable Style/ClassVars
  16. @@lookup_at = nil # rubocop:disable Style/ClassVars
  17. @@lookup_timeout = if ENV['ZAMMAD_SETTING_TTL'] # rubocop:disable Style/ClassVars
  18. ENV['ZAMMAD_SETTING_TTL'].to_i.seconds
  19. else
  20. 15.seconds
  21. end
  22. =begin
  23. set config setting
  24. Setting.set('some_config_name', some_value)
  25. =end
  26. def self.set(name, value)
  27. setting = Setting.find_by(name: name)
  28. if !setting
  29. raise "Can't find config setting '#{name}'"
  30. end
  31. setting.state_current = { value: value }
  32. setting.save!
  33. logger.info "Setting.set('#{name}', #{value.inspect})"
  34. end
  35. =begin
  36. get config setting
  37. value = Setting.get('some_config_name')
  38. =end
  39. def self.get(name)
  40. load
  41. @@current[name]
  42. end
  43. =begin
  44. reset config setting to default
  45. Setting.reset('some_config_name')
  46. Setting.reset('some_config_name', force) # true|false - force it false per default
  47. =end
  48. def self.reset(name, force = false)
  49. setting = Setting.find_by(name: name)
  50. if !setting
  51. raise "Can't find config setting '#{name}'"
  52. end
  53. return true if !force && setting.state_current == setting.state_initial
  54. setting.state_current = setting.state_initial
  55. setting.save!
  56. logger.info "Setting.reset('#{name}', #{setting.state_current.inspect})"
  57. end
  58. =begin
  59. reload config settings
  60. Setting.reload
  61. =end
  62. def self.reload
  63. @@last_changed_at = nil # rubocop:disable Style/ClassVars
  64. load(true)
  65. end
  66. private
  67. # load values and cache them
  68. def self.load(force = false)
  69. # check if config is already generated
  70. return false if !force && @@current.present? && cache_valid?
  71. # read all or only changed since last read
  72. latest = Setting.order(updated_at: :desc).limit(1).pluck(:updated_at)
  73. settings = if @@last_changed_at && @@current.present?
  74. Setting.where('updated_at > ?', @@last_changed_at).order(:id).pluck(:name, :state_current)
  75. else
  76. Setting.order(:id).pluck(:name, :state_current)
  77. end
  78. if latest
  79. @@last_changed_at = latest[0] # rubocop:disable Style/ClassVars
  80. end
  81. if settings.present?
  82. settings.each do |setting|
  83. @@raw[setting[0]] = setting[1]['value']
  84. end
  85. @@raw.each do |key, value|
  86. if value.class != String
  87. @@current[key] = value
  88. next
  89. end
  90. @@current[key] = value.gsub(/\#\{config\.(.+?)\}/) do
  91. @@raw[$1].to_s
  92. end
  93. end
  94. end
  95. @@change_id = Cache.get('Setting::ChangeId') # rubocop:disable Style/ClassVars
  96. @@lookup_at = Time.zone.now # rubocop:disable Style/ClassVars
  97. true
  98. end
  99. private_class_method :load
  100. # set initial value in state_initial
  101. def set_initial
  102. self.state_initial = state_current
  103. true
  104. end
  105. def reset_change_id
  106. @@current[name] = state_current[:value]
  107. change_id = rand(999_999_999).to_s
  108. logger.debug { "Setting.reset_change_id: set new cache, #{change_id}" }
  109. Cache.write('Setting::ChangeId', change_id, { expires_in: 24.hours })
  110. @@lookup_at = nil # rubocop:disable Style/ClassVars
  111. true
  112. end
  113. # check if cache is still valid
  114. def self.cache_valid?
  115. if @@lookup_at && @@lookup_at > Time.zone.now - @@lookup_timeout
  116. #logger.debug "Setting.cache_valid?: cache_id has been set within last #{@@lookup_timeout} seconds"
  117. return true
  118. end
  119. change_id = Cache.get('Setting::ChangeId')
  120. if change_id == @@change_id
  121. @@lookup_at = Time.zone.now # rubocop:disable Style/ClassVars
  122. #logger.debug "Setting.cache_valid?: cache still valid, #{@@change_id}/#{change_id}"
  123. return true
  124. end
  125. #logger.debug "Setting.cache_valid?: cache has changed, #{@@change_id}/#{change_id}"
  126. false
  127. end
  128. private_class_method :cache_valid?
  129. # convert state into hash to be able to store it as store
  130. def state_check
  131. return true if !state
  132. return true if state.try(:key?, :value)
  133. self.state_current = { value: state }
  134. true
  135. end
  136. # notify clients about public config changes
  137. def check_broadcast
  138. return true if frontend != true
  139. value = state_current
  140. if state_current.key?(:value)
  141. value = state_current[:value]
  142. end
  143. Sessions.broadcast(
  144. {
  145. event: 'config_update',
  146. data: { name: name, value: value }
  147. },
  148. 'public'
  149. )
  150. true
  151. end
  152. end