-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunraku
executable file
·184 lines (148 loc) · 3.59 KB
/
bunraku
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
#!/usr/bin/env lua
package.path = table.concat({
'libs/?/init.lua',
'libs/?.lua',
'',
}, ';') .. package.path
local nixio = require'nixio'
local reload = ...
-- Bootstrap the daemon
if not reload then
do
local pid = nixio.fork()
if(pid ~= 0) then
print(string.format("Forking into the background as: %d", pid))
os.exit()
end
end
end
local zmq = require"zmq"
local mongo = require"mongo"
local ev = require"ev"
local csv = require"csv"
local loop = ev.Loop.default
local db = mongo.Connection.New()
local socket = require"socket"
require'logging.file'
-- XXX: Temporary location.
local log = logging.file('/tmp/bunraku.log')
db:connect"localhost"
local bunraku = {
db = db,
loop = loop,
}
local safeFormat = function(format, ...)
if select('#', ...) > 0 then
local success, message = pcall(string.format, format, ...)
if success then
return message
end
else
return format
end
end
function bunraku:Log(level, ...)
local message = safeFormat(...)
if message then
log[level](log, message)
end
end
function bunraku:HandleMsg(data)
local data = csv(data)
if not data then
return self:Log('error', 'Unable to parse CSV')
end
local mName = data[1]
if not self[mName] then
return self:Log('error', 'Trying to use module that is not loaded: %s', mName)
end
self[mName]:Queue(data)
end
function bunraku:LoadModule(mName)
local mFile, mError = loadfile('modules/' .. mName.. '.lua')
if not mFile then
return self:Log('error', 'Unable to load module %s: %s.', mFile, mError)
end
local env = {
bunraku = self,
package = package,
}
local proxy = setmetatable(env, { __index = _G })
setfenv(mFile, proxy)
local success, message = pcall(mFile, self)
if not success then
self:Log('error', 'Unable to execute module %s: %s', mFile, message)
else
self[mName] = message
end
end
function bunraku:LoadModules()
if self.modules then
for _, m in next, self.modules do
self:LoadModule(m)
end
end
end
function bunraku:Reload()
local coreFunc, coreError = loadfile'bunraku'
if not coreFunc then
return self:Log('error', 'Unable to reload core: %s.', coreError)
end
local success, message = pcall(coreFunc, true)
if not success then
return self:Log('error', 'Unable to execute new core: %s.', message)
else
self.control:stop(self.loop)
self.s_io_idle:stop(self.loop)
message.ctx = self.ctx
message.socket = self.socket
message.loop = self.loop
message.db = self.dbi
message.modules = self.modules
self = message
self.control = assert(loadfile('core/control.lua'))(self)
self.control:start(loop)
self:Init()
self.s_io_idle:start(loop)
self:Log('info', 'Successfully reloaded core.')
end
end
function bunraku:Init()
self.modules = {
"anidb",
"moviedb",
"tvdb",
}
self.ctx = zmq.init(1)
self.socket = self.ctx:socket(zmq.SUB)
self.socket:setopt(zmq.SUBSCRIBE, "")
self.socket:bind"ipc:///tmp/bunraku.sock"
if not self.control then
self.control = assert(loadfile('core/control.lua'))(bunraku)
self.control:start(loop)
end
self.apikey = assert(loadfile('config/apikey.lua'))(bunraku)
self.s_io_idle = ev.Idle.new(function()
local msg, err = self.socket:recv(zmq.NOBLOCK)
if err then
self.s_io_idle:stop(loop)
self.s_io_read:start(loop)
return
end
self:HandleMsg(msg)
end)
self.s_io_read = ev.IO.new(function()
self.s_io_idle:start(loop)
self.s_io_read:stop(loop)
end,
self.socket:getopt(zmq.FD), ev.READ)
self:LoadModules()
end
if not reload then
bunraku:Init()
bunraku.s_io_idle:start(bunraku.loop)
bunraku:Log('info', "Bunraku loaded, let the show begin.")
bunraku.loop:loop()
end
return bunraku
-- vim: ft=lua