Class: Pazuzu::Application

Inherits:
Object
  • Object
show all
Includes:
Utility::Runnable
Defined in:
lib/pazuzu/application.rb

Instance Attribute Summary collapse

Attributes included from Utility::Runnable

#started_at, #stopped_at

Instance Method Summary collapse

Methods included from Utility::Runnable

#run_state, #start!, #stop!, #wait_for_state_change!

Constructor Details

#initialize(supervisor, name) ⇒ Application

Returns a new instance of Application.



7
8
9
10
11
12
13
14
# File 'lib/pazuzu/application.rb', line 7

def initialize(supervisor, name)
  super()
  @supervisor = supervisor
  @name = name
  @logger = Utility::AnnotatedLogger.new(supervisor.logger, qname)
  @workers = Utility::RunnablePool.new
  @mutex = Mutex.new
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



77
78
79
# File 'lib/pazuzu/application.rb', line 77

def group
  @group
end

#nameObject (readonly)

Returns the value of attribute name.



74
75
76
# File 'lib/pazuzu/application.rb', line 74

def name
  @name
end

#supervisorObject (readonly)

Returns the value of attribute supervisor.



75
76
77
# File 'lib/pazuzu/application.rb', line 75

def supervisor
  @supervisor
end

#userObject (readonly)

Returns the value of attribute user.



76
77
78
# File 'lib/pazuzu/application.rb', line 76

def user
  @user
end

Instance Method Details

#configure!(procfile_path, configuration) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pazuzu/application.rb', line 16

def configure!(procfile_path, configuration)      
  procfile_path = Procfiles.normalize_procfile_path(procfile_path)

  root_path = File.dirname(procfile_path)

  @user = configuration['user']
  @group = configuration['group']

  workers_config = configuration['workers'] || {}

  leftover_workers = @workers.children.dup

  procfile = YAML.load(File.open(procfile_path))
  procfile = {} unless procfile.is_a?(Hash)
  procfile.each do |name, command_line|
    name = name.to_s

    worker_config = workers_config[name] || {}

    worker = @workers.children.select { |w| w.name == name }.first
    if worker
      leftover_workers.delete(worker)
    else
      @logger.info "Adding worker #{name}"
      worker = Worker.new(self, name)
    end
    worker.configure!(root_path, command_line, worker_config)
    @workers.register(worker)
  end

  leftover_workers.each do |worker|
    @logger.info "Removing worker #{worker.name}"
    @workers.unregister(worker)
  end
end

#log_entriesObject



67
68
69
70
71
72
# File 'lib/pazuzu/application.rb', line 67

def log_entries
  entries = @workers.children.map(&:log_entries)
  entries.flatten!(1)
  entries.sort_by! { |(source, time, message)| time }
  entries
end

#qnameObject



52
53
54
# File 'lib/pazuzu/application.rb', line 52

def qname
  @name
end

#setup_spawned_process!Object

Call to set up a forked process according to the application’s configuration.



58
59
60
61
# File 'lib/pazuzu/application.rb', line 58

def setup_spawned_process!
  Utility::ProcessSpawning.set_user_and_group!(@user, @group)
  Utility::ProcessSpawning.prepare_child_process!
end

#workersObject



63
64
65
# File 'lib/pazuzu/application.rb', line 63

def workers
  @workers.children
end