Class: Tap::Controllers::App

Inherits:
Tap::Controller show all
Defined in:
lib/tap/controllers/app.rb

Overview

:startdoc::controller builds and runs workflows

Constant Summary

Constants inherited from Tap::Controller

Tap::Controller::ServerError

Instance Attribute Summary

Attributes inherited from Tap::Controller

#controller_path, #request, #response, #server

Instance Method Summary collapse

Methods inherited from Tap::Controller

#action?, call, #call, #dispatch, get, inherited, #initialize, #module_path, #module_render, nest, #redirect, #render, #render_erb, #render_layout, #route, set, set_variables, #template_path, #uri

Constructor Details

This class inherits a constructor from Tap::Controller

Instance Method Details

#buildObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tap/controllers/app.rb', line 56

def build
  schema = request[:schema] || server.data.read(:schema, request[:id])
  
  unless request.post?
    return render('build.erb', :schema => schema, :layout => true)
  end
  
  schema = Tap::Schema.load(schema).resolve! do |type, key, data|
    server.env.manifest(type)[key]
  end.validate!
  
  if request[:reset] == "on"
    app.reset
  end
  
  tasks.merge!(server.env.build(schema, app))
  
  if request[:run] == "on"
    run
  else
    redirect uri(:enque)
  end
end

#enqueObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tap/controllers/app.rb', line 80

def enque
  unless request.post?
    return render('enque.erb', :layout => true)
  end
  
  queue = if request[:load]
    YAML.load(request[:queue] || "{}")
  else
    request[:queue] || {}
  end
  
  queue.each do |(key, inputs)|
    unless task = tasks[key]
      raise "no task for: #{key}"
    end
    app.enq(task, *inputs)
  end
  
  redirect uri(:info)
end

#infoObject

Returns the controls and current application info.



24
25
26
27
28
# File 'lib/tap/controllers/app.rb', line 24

def info
  render 'info.erb', :locals => {
    :actions => [:run, :stop, :terminate, :reset],
  }, :layout => true
end

#resetObject



39
40
41
42
# File 'lib/tap/controllers/app.rb', line 39

def reset
  app.reset if request.post?
  redirect uri(:info)
end

#runObject

Runs app on a separate thread (on post).



31
32
33
34
35
36
37
# File 'lib/tap/controllers/app.rb', line 31

def run
  if request.post?
    server.thread ||= Thread.new { app.run; server.thread = nil; }
  end
  
  redirect uri(:info)
end

#stateObject

Returns the state of app.



19
20
21
# File 'lib/tap/controllers/app.rb', line 19

def state
  app.state.to_s
end

#stopObject

Stops app (on post).



45
46
47
48
# File 'lib/tap/controllers/app.rb', line 45

def stop
  app.stop if request.post?
  redirect uri(:info)
end

#tail(id) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tap/controllers/app.rb', line 101

def tail(id)
  unless data.has?("#{id}.log")
    raise Tap::ServerError.new("invalid id: #{id}", 404)
  end
  
  path = data.path("#{id}.log")
  pos = request['pos'].to_i
  if pos > File.size(path)
    raise Tap::ServerError.new("tail position out of range (try update)", 500)
  end

  content = File.open(path) do |file|
    file.pos = pos
    file.read
  end
    
  if request.post?
    content
  else
    render('tail.erb', :locals => {
      :id => id,
      :path => File.basename(path),
      :update => true,
      :content => content
    }, :layout => true)
  end
end

#terminateObject

Teminates app (on post).



51
52
53
54
# File 'lib/tap/controllers/app.rb', line 51

def terminate
  app.terminate if request.post?
  redirect uri(:info)
end