Class: Nutella::RunListHash

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • app_id (String)

    the app_id the run_id belongs to

  • run_id (String)

    the run_id we are trying to add to the runs list

  • path_to_app_files (String)

    the path to the application files

Returns:

  • (Boolean)

    true if the run_id is added to the list (i.e. there is no other run_id with for the same app_id)



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_appsArray<String>

Returns a list of all the apps in the runlist

Returns:

  • (Array<String>)

    an array containing the app_ids 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_runsHash

Returns all the run_ids for ALL applications (i.e. the runslist)

Returns:

  • (Hash)

    the run list with all app_ids and run_ids



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

Parameters:

  • app_id (String)

    of the application we want to find the path of

Returns:

  • (String)

    the path of the app or nil if the app doesn’t exist



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_listObject

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

Parameters:

  • app_id (String)

    the app_id the run_id belongs to

  • run_id (String)

    the run_if we are trying to remove from the runs list

Returns:

  • (Boolean)

    true if the run_id is removed from the list (i.e. a run_id with that name exists and is successfully removed)



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

Returns:

  • (Boolean)

    true if the list is empty, false otherwise



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

Parameters:

  • app_id (String)

    the app_id the run_id belongs to

  • run_id (String)

    the run_if we are checking

Returns:

  • (Boolean)

    true if the run_id is in the list, false otherwise



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_fileObject

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

Parameters:

  • app_id (String)

    of the application we want to find run_ids for

Returns:

  • (Array<String>)

    list of run_ids associated to the specified app_id



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