Module: Perus::Server::DB
- Defined in:
- lib/perus/server/db.rb
Constant Summary collapse
- MAX_VACUUM_ATTEMPTS =
5
Class Method Summary collapse
Class Method Details
.cleanup ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/perus/server/db.rb', line 114 def self.cleanup puts 'Cleaning old values and metrics' keep_hours = Server..keep_hours # remove old values = Time.now.to_i - (keep_hours * 60 * 60) values = Value.where("timestamp < #{min_timestamp}") puts "Deleting #{values.count} values" values.each(&:destroy) # remove metrics from systems if they no longer have any values empty_deleted = 0 file_deleted = 0 Metric.each do |metric| if metric.file path = metric.path if !File.exists?(path) || File.mtime(path).to_i < metric.destroy file_deleted += 1 end elsif metric.values_dataset.empty? metric.destroy empty_deleted += 1 end end puts "#{empty_deleted} metrics were deleted as they had no values" puts "#{file_deleted} metrics were deleted as they had old files" end |
.db ⇒ Object
9 10 11 |
# File 'lib/perus/server/db.rb', line 9 def self.db @db end |
.start ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/perus/server/db.rb', line 13 def self.start puts 'Loading database' Sequel.extension :migration Sequel.extension :inflector # connect/create the database and run any new migrations @db = Sequel.sqlite(Server..db_path, integer_booleans: true) Sequel::Migrator.run(@db, File.join(__dir__, 'migrations')) # load models - these rely on an existing db connection Dir.chdir(File.join(__dir__, 'models')) do require './system' require './config' require './value' require './group' require './error' require './alert' require './action' require './metric' require './script' require './command_config' require './script_command' require './config_metric' require './active_alert' end end |
.start_timers ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/perus/server/db.rb', line 40 def self.start_timers # attempt to run vacuum twice a day. this is done to increase # performance rather than reclaim unused space. as old values and # metrics are deleted the data become very fragmented. vacuuming # restructures the db so system records in the values index should # be sequentially stored vacuum_task = Concurrent::TimerTask.new do attempts = 0 complete = false while !complete && attempts < MAX_VACUUM_ATTEMPTS begin puts "Vacuuming, attempt #{attempts + 1}" start = Time.now @db.execute('vacuum') Stats.vacuumed!(Time.now - start) complete = true puts "Vacuuming complete" rescue attempts += 1 if attempts < MAX_VACUUM_ATTEMPTS puts "Vacuum failed, will reattempt after short sleep" sleep(5) end end end if !complete puts "Vacuum failed more than MAX_VACUUM_ATTEMPTS" Stats.vacuumed!('failed') end end # fire every 12 hours vacuum_task.execution_interval = 60 * 60 * 12 vacuum_task.execute # a fixed number of hours of data are kept in the database. once an # hour, old values and files are removed. if all values of a metric # are removed from a system, the accompanying metric record is also # removed. cleanup_task = Concurrent::TimerTask.new do begin start = Time.now Perus::Server::DB.cleanup Stats.cleaned!(Time.now - start) rescue Stats.cleaned!('failed') end end # fire every hour cleanup_task.execution_interval = 60 * 60 cleanup_task.execute # alerts can be process intensive, so to keep page refreshes # responsive the 'active' state of an alert for each system is # cached so lookups can be done against the db, rather than running # each alert for each system on a page load. cache_alerts_task = Concurrent::TimerTask.new do begin start = Time.now Perus::Server::Alert.cache_active_alerts Stats.alerts_cached!(Time.now - start) rescue Stats.alerts_cached!('failed') end end cache_alerts_task.execution_interval = Server..cache_alerts_secs cache_alerts_task.execute end |