Class: Nutella::RunListHash
- Inherits:
-
Object
- Object
- Nutella::RunListHash
- Defined in:
- lib/config/runlist.rb
Overview
Manages the list of nutella applications and runs handled by the framework. The list has a structure similar this one: {
"app_a": {
"runs": [ "default", "run_1", "run_2" ],
"path": "/path/to/app/a/files/"
},
"app_b": {
"runs": [ "run_1", "run_3" ],
"path": "/path/to/app/b/files/"
}
}
Instance Method Summary collapse
-
#add?(app_id, run_id, path_to_app_files) ⇒ Boolean
Adds a run_id to the runlist.
-
#all_apps ⇒ Array<String>
Returns a list of all the apps in the runlist.
-
#all_runs ⇒ Hash
Returns all the run_ids for ALL applications (i.e. the runslist).
-
#app_has_no_bots(app_id) ⇒ Object
Returns true if the app has no bots.
-
#app_path(app_id) ⇒ String
Returns the path for a certain application.
-
#clean_list ⇒ Object
This method checks that the list reflects the actual state of the system.
-
#delete?(app_id, run_id) ⇒ Boolean
Remove a run_id from the list.
-
#empty? ⇒ Boolean
Returns true if the runs list is empty.
-
#include?(app_id, run_id) ⇒ Boolean
Checks if a certain run is contained in the list.
-
#initialize(file) ⇒ RunListHash
constructor
A new instance of RunListHash.
-
#remove_file ⇒ Object
Removes the runs list file.
-
#runs_for_app(app_id) ⇒ Array<String>
Returns all the run_ids for a certain application.
Constructor Details
#initialize(file) ⇒ RunListHash
Returns a new instance of RunListHash.
20 21 22 |
# File 'lib/config/runlist.rb', line 20 def initialize( file ) @ph = PersistedHash.new file end |
Instance Method Details
#add?(app_id, run_id, path_to_app_files) ⇒ Boolean
Adds a run_id to the runlist
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/config/runlist.rb', line 70 def add?( app_id, run_id, path_to_app_files ) # If no run_id is specified, we are adding the "default" run run_id = 'default' if run_id.nil? # Check if we are adding the first run for a certain application if @ph.add_key_value?(app_id, Hash.new) t = @ph[app_id] # Add path and initialize runs t['path'] = path_to_app_files t['runs'] = [run_id] else t = @ph[app_id] # Check a run with this name doesn't already exist return false if t['runs'].include? run_id # Add the run_id to list of runs t['runs'].push(run_id) end @ph[app_id] = t true end |
#all_apps ⇒ Array<String>
Returns a list of all the apps in the runlist
28 29 30 |
# File 'lib/config/runlist.rb', line 28 def all_apps @ph.to_h.keys end |
#all_runs ⇒ Hash
Returns all the run_ids for ALL applications (i.e. the runslist)
36 37 38 |
# File 'lib/config/runlist.rb', line 36 def all_runs @ph.to_h end |
#app_has_no_bots(app_id) ⇒ Object
Returns true if the app has no bots
155 156 157 |
# File 'lib/config/runlist.rb', line 155 def app_has_no_bots( app_id ) Dir.entries("#{app_path(app_id)}/bots").select{|entry| File.directory?(File.join("#{app_path(app_id)}/bots",entry)) && !(entry =='.' || entry == '..') }.empty? end |
#app_path(app_id) ⇒ String
Returns the path for a certain application
57 58 59 60 |
# File 'lib/config/runlist.rb', line 57 def app_path( app_id ) return nil if @ph[app_id].nil? @ph[app_id]['path'] end |
#clean_list ⇒ Object
This method checks that the list reflects the actual state of the system. It does so by checking that there is still a tmux session with the run name. If that’s not the case, it removes the missing runs from the list.
143 144 145 146 147 148 149 150 151 |
# File 'lib/config/runlist.rb', line 143 def clean_list all_runs.each do |app, _| runs_for_app(app).each do |run| unless Tmux.session_exist?(Tmux.session_name(app, run)) || app_has_no_bots(app) delete? app, run end end end end |
#delete?(app_id, run_id) ⇒ Boolean
Remove a run_id from the list
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/config/runlist.rb', line 97 def delete?( app_id, run_id ) # If there is no app, then return false and do nothing return false if @ph[app_id].nil? t = @ph[app_id] result = t['runs'].delete run_id if t['runs'].empty? # If run_id was the last run for this app, remove the app as well @ph.delete_key_value? app_id else # otherwise write the hash back @ph[app_id] = t end result.nil? ? false : true end |
#empty? ⇒ Boolean
Returns true if the runs list is empty
128 129 130 |
# File 'lib/config/runlist.rb', line 128 def empty? @ph.empty? end |
#include?(app_id, run_id) ⇒ Boolean
Checks if a certain run is contained in the list
118 119 120 121 122 123 |
# File 'lib/config/runlist.rb', line 118 def include? (app_id, run_id) # If there is no app, then return false and do nothing return false if @ph[app_id].nil? # Otherwise check the runs array @ph[app_id]['runs'].include? run_id end |
#remove_file ⇒ Object
Removes the runs list file
134 135 136 |
# File 'lib/config/runlist.rb', line 134 def remove_file @ph.remove_file end |
#runs_for_app(app_id) ⇒ Array<String>
Returns all the run_ids for a certain application
45 46 47 48 49 50 |
# File 'lib/config/runlist.rb', line 45 def runs_for_app( app_id ) # If there is no app, then return false and do nothing return [] if @ph[app_id].nil? runs = @ph[app_id]['runs'] runs.nil? ? [] : runs end |