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)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/config/runlist.rb', line 60

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

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



120
121
122
123
124
125
126
127
128
# File 'lib/config/runlist.rb', line 120

def clean_list
  all_runs.each do |app, _|
    runs_for_app(app).each do |run|
      unless Tmux.session_exist? Tmux.session_name(app, run)
        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)



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/config/runlist.rb', line 87

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



105
106
107
# File 'lib/config/runlist.rb', line 105

def empty?
  @ph.empty?
end

#remove_fileObject

Removes the runs list file



111
112
113
# File 'lib/config/runlist.rb', line 111

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