forked from iberianpig/fusuma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusuma.rb
195 lines (162 loc) · 5.64 KB
/
fusuma.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true
require_relative "./fusuma/version"
require_relative "./fusuma/multi_logger"
require_relative "./fusuma/config"
require_relative "./fusuma/environment"
require_relative "./fusuma/device"
require_relative "./fusuma/plugin/manager"
# this is top level module
module Fusuma
# main class
class Runner
class << self
def run(option = {})
set_trap
read_options(option)
instance = new
## NOTE: Uncomment following line to measure performance
# instance.run_with_lineprof
instance.run
end
private
def set_trap
Signal.trap("INT") { puts exit } # Trap ^C
Signal.trap("TERM") { puts exit } # Trap `Kill `
end
def read_options(option)
MultiLogger.filepath = option[:log_filepath]
MultiLogger.instance.debug_mode = option[:verbose]
load_custom_config(option[:config_path])
Plugin::Manager.require_base_plugins
Environment.dump_information
Kernel.exit(0) if option[:version]
if option[:list]
Environment.print_device_list
Kernel.exit(0)
end
# TODO: remove keep_device_from_option from command line options
Plugin::Filters::LibinputDeviceFilter::KeepDevice.from_option = option[:device]
Process.daemon if option[:daemon]
end
def load_custom_config(config_path = nil)
Config.custom_path = config_path
end
end
def initialize
@inputs = Plugin::Inputs::Input.plugins.map(&:new)
@filters = Plugin::Filters::Filter.plugins.map(&:new)
@parsers = Plugin::Parsers::Parser.plugins.map(&:new)
@buffers = Plugin::Buffers::Buffer.plugins.map(&:new)
@detectors = Plugin::Detectors::Detector.plugins.map(&:new)
@executors = Plugin::Executors::Executor.plugins.map(&:new)
end
def run
loop { pipeline }
end
def pipeline
event = input || return
clear_expired_events
filtered = filter(event) || return
parsed = parse(filtered) || return
buffered = buffer(parsed) || return
detected = detect(buffered) || return
condition, context, event = merge(detected) || return
execute(condition, context, event)
end
# For performance monitoring
def run_with_lineprof(count: 1000)
require "rblineprof"
require "rblineprof-report"
profile = lineprof(%r{#{Pathname.new(__FILE__).parent}/.}) do
count.times { pipeline }
end
LineProf.report(profile)
exit 0
end
# @return [Plugin::Events::Event]
def input
Plugin::Inputs::Input.select(@inputs)
end
# @param [Plugin::Events::Event]
# @return [Plugin::Events::Event]
def filter(event)
event if @filters.any? { |f| f.filter(event) }
end
# @param [Plugin::Events::Event]
# @return [Plugin::Events::Event]
def parse(event)
@parsers.reduce(event) { |e, p| p.parse(e) if e }
end
# @param [Plugin::Events::Event]
# @return [Array<Plugin::Buffers::Buffer>]
# @return [NilClass]
def buffer(event)
@buffers.select { |b| b.buffer(event) }
end
# @param buffers [Array<Buffer>]
# @return [Array<Event>]
def detect(buffers)
matched_detectors = @detectors.select do |detector|
detector.watch? ||
buffers.any? { |b| detector.sources.include?(b.type) }
end
events = matched_detectors.each_with_object([]) do |detector, detected|
Array(detector.detect(@buffers)).each { |e| detected << e }
end
return if events.empty?
events
end
# @param events [Array<Plugin::Events::Event>]
# @return [Plugin::Events::Event] Event merged all records from arguments
# @return [NilClass] when event is NOT given
def merge(events)
index_events, context_events = events.partition { |event| event.record.type == :index }
main_events, modifiers = index_events.partition { |event| event.record.mergable? }
request_context = context_events.each_with_object({}) do |e, results|
results[e.record.name] = e.record.value
end
main_events.sort_by! { |e| e.record.trigger_priority }
matched_condition = nil
matched_context = nil
event = main_events.find do |main_event|
matched_context = Config::Searcher.find_context(request_context) do
matched_condition, modified_record = Config::Searcher.find_condition do
main_event.record.merge(records: modifiers.map(&:record))
end
if matched_condition && modified_record
main_event.record = modified_record
else
matched_condition, = Config::Searcher.find_condition do
Config.search(main_event.record.index) &&
Config.find_execute_key(main_event.record.index)
end
end
end
end
return if event.nil?
[matched_condition, matched_context, event]
end
# @param event [Plugin::Events::Event]
def execute(condition, context, event)
return unless event
# Find executable condition and executor
executor = Config::Searcher.with_context(context) do
Config::Searcher.with_condition(condition) do
@executors.find { |e| e.executable?(event) }
end
end
return if executor.nil?
# Check interval and execute
Config::Searcher.with_context(context) do
Config::Searcher.with_condition(condition) do
executor.enough_interval?(event) &&
executor.update_interval(event) &&
executor.execute(event)
end
end
end
def clear_expired_events
@buffers.each(&:clear_expired)
end
end
end