Class: Swee::AppExecutor

Inherits:
Object show all
Defined in:
lib/swee/app_executor.rb

Defined Under Namespace

Classes: ExeThread

Constant Summary collapse

DEFAULT_THTEADS =
20

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAppExecutor

Returns a new instance of AppExecutor.



123
124
125
126
127
128
129
# File 'lib/swee/app_executor.rb', line 123

def initialize
  @queue = []
  @threadpoll = Array.new
  DEFAULT_THTEADS.times do
    @threadpoll << ExeThread.new
  end
end

Class Method Details

.execute_after_filter(controller_intance, route) ⇒ Object

执行后置过滤器



51
52
53
# File 'lib/swee/app_executor.rb', line 51

def execute_after_filter controller_intance,route
  execute_filter :after, controller_intance, route
end

.execute_before_filter(controller_intance, route) ⇒ Object

执行前置过滤器



46
47
48
# File 'lib/swee/app_executor.rb', line 46

def execute_before_filter controller_intance,route
  execute_filter :before, controller_intance, route
end

.execute_controller(controller_intance, action) ⇒ Object

执行控制器



56
57
58
59
60
# File 'lib/swee/app_executor.rb', line 56

def execute_controller controller_intance,action
  result = controller_intance.send(action)
  result.freeze
  result
end

.execute_filter(_type, controller_intance, route) ⇒ Object

执行过滤器



63
64
65
66
67
68
# File 'lib/swee/app_executor.rb', line 63

def execute_filter _type, controller_intance,route
  _filter_actions = Controller.find_filter_methods route.controller_name,_type,route.action
  _filter_actions.each do |_action|
    controller_intance.send _action
  end
end

.path_to_route(env) ⇒ Object

app_path转路由 获取 路由结构



23
24
25
26
# File 'lib/swee/app_executor.rb', line 23

def path_to_route env
  _path_info = env["PATH_INFO"]
  Routes.tables[_path_info.to_s]
end

.rack_responsed?(result) ⇒ Boolean

检测是否为 rack 格式得响应 rack标准格式: [status,headers,]

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/swee/app_executor.rb', line 72

def rack_responsed? result
  if result.is_a?(Array) && result.size == 3
     status =  result[0]
    headers =  result[1]
       body =  result[2]
    if status.is_a?(Integer) && headers.is_a?(Hash) && body.respond_to?(:each)
      return true
    end
  end
  return false
end

.render_view(controller_intance, route) ⇒ Object

渲染视图 渲染 -> 前置 后置 环绕控制器



38
39
40
41
42
43
# File 'lib/swee/app_executor.rb', line 38

def render_view controller_intance, route
  execute_before_filter controller_intance, route
  result = execute_controller controller_intance, route.action
  execute_after_filter controller_intance, route
  return !rack_responsed?(result) ? controller_intance.render : result
end

.route_to_controller(route, env) ⇒ Object

执行 app controller 方法



29
30
31
32
33
34
# File 'lib/swee/app_executor.rb', line 29

def route_to_controller route, env
  controller = route.controller
  controller_intance = route.create_controller_instance
  controller_intance.warp_request env,route
  controller_intance
end

.run(env) ⇒ Object

启动 app 渲染器 app_path 转路由 执行 app controller 方法 渲染 filter (:before, :after, :round) 生成 View



11
12
13
14
15
16
17
18
19
# File 'lib/swee/app_executor.rb', line 11

def run env
  route = path_to_route(env)
  request_method = env["REQUEST_METHOD"].downcase.to_sym
  if route.nil? || !route.request_methods.include?(request_method)
    return View.render_404
  end
  controller_intance = route_to_controller route, env
  render_view controller_intance, route
end

Instance Method Details

#<<Object



149
150
151
# File 'lib/swee/app_executor.rb', line 149

def <<()
  @queue.push()
end

#current_threadObject



131
132
133
# File 'lib/swee/app_executor.rb', line 131

def current_thread
  _current_thread = @threadpoll.select { |t| !t.busy? }
end

#runObject



135
136
137
138
139
140
141
# File 'lib/swee/app_executor.rb', line 135

def run
  f = Fiber.new do
    while true
      Fiber.yield
    end
  end
end

#waitObject



143
144
145
146
147
# File 'lib/swee/app_executor.rb', line 143

def wait
  while exist_alive_thread?
    sleep 0.1
  end
end