Parent: [aab297] (diff)

Download this file

ModuleFE.coffee    341 lines (285 with data), 8.9 kB

  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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
Component = require "component"
http = require 'http-message'
util = require 'util'
express = require 'express'
bodyParser = require 'body-parser'
path = require 'path'
q = require 'q'
url = require 'url'
class ModuleFE extends Component
# Constructor
# Same parameters as Component.
#
constructor: (@runtime, @role, @iid, @incnum, @localData, @resources, @parameters, @requires, @provides) ->
@runtime.setLogger [ModuleFE]
super
@optServiceChannel = null
@jobsApiChannel = null
@jobNotificationServiceChannel = null
@taskExecutorChannel = null
#Front End specific properties
@app = null
@router = null
@path = '/opc'
@logger.info "constructor completed"
setupChannels: () =>
@logger.info "setupChannels()"
@logger.info 'provides : ', Object.keys(@provides)
@logger.info 'requires: ', Object.keys(@requires)
@optServiceChannel = @provides['optServiceChannel']
@logger.info "setupChannels initialized #{util.inspect(@optServiceChannel)}"
@jobsApiChannel = @requires['jobsApi']
@taskApiChannel = @requires['taskApi']
@jobNotificationChannel = @requires['jobNotificationReceive']
@jobNotificationChannel.on 'message', @onJobNotification
cfg = if @optServiceChannel.config? then @optServiceChannel.config else {}
cfg.timeout = 500000
@optServiceChannel.setConfig cfg
cfg = if @jobsApiChannel.config? then @jobsApiChannel.config else {}
cfg.timeout = 240000
@jobsApiChannel.setConfig cfg
cfg = if @taskApiChannel.config? then @taskApiChannel.config else {}
cfg.timeout = 240000
@taskApiChannel.setConfig cfg
@logger.info "setupChannels() completed"
###
Starts the execution
###
run: () =>
@logger.info "run()"
super
@setupChannels()
@initRouter()
@startExpress()
#@startHttpServer()
@logger.info "run() completed"
###
Start httpServer
###
#startHttpServer: () =>
# @logger.info 'startHttpServer'
# @httpServer = http.createServer()
# @httpServer.on 'request', @onHttpRequest
# @httpServer.on 'error', @onHttpError
# @httpServer.listen @optServiceChannel
###
Receive and process new http requests
###
#onHttpRequest: (request, response) =>
# pathname = url.parse(request.url).pathname
# if (pathname is "/opc")
# postData = ""
#
# request.on "data", (chunk) =>
# @logger.info "onHttpRequest onData"
# postData = postData + chunk
#
# request.on "end", () =>
# message = JSON.parse postData
# @logger.info "onHttpRequest onEnd. Data = #{message}"
#
# @handleRequest(message, response)
#
# else
# msg =
# success: false
# message: "Invalid resource #{pathname}"
# @sendResponse(response, msg, 400)
handleRequest: (msg, res) =>
@logger.info "handleRequest() OK"
msg.success = true
@sendResponse(res, msg)
###
Event listener for HTTP server "error" event.
###
onHttpError: (error) =>
@logger.info "Cfe:onHttpServerError"
unless (error.syscall is 'listen') then throw error
switch error.code
when "EACCES"
@logger.info 'Cfe:onHttpServerError: port requires elevated \
privileges'
when "EADDRINUSE"
@logger.info 'Cfe:onHttpServerError: port is already in use'
throw error
###
Configure Router
###
initRouter: () =>
@logger.info "initRouter()"
@router = express.Router()
@router.get '/jobsApi', @jobsApi
@router.get '/taskApi', @taskApi
@router.get '/ping', @ping
###@router.get '/api', @getApi###
@logger.info "initRouter() completed"
###
Check if component is up
###
ping: (req, rep) =>
@logger.info "ping()"
response =
success: true
message: 'PONG'
@sendResponse rep, response
###
Returns description of the Task Executor Front-End API
###
###getApi: (req, res) =>
@logger.info 'getApi()'
message =
success: true
response: [
{
url: 'PUT/jobsApi'
description: 'Redirects call to job component'
form:
action: "action"
params:
param1: 'param1'
param2: 'param2'
param3: 'param3'
example:
action: "execute"
form:
idPlan: 'idPlan'
idAlgorithm: 'idAlgorithm'
default:
ipserver: '192.168.183.60'
user: 'user'
password: 'c2net'
db: 'MSQh2dp1hc0r0_001_STables'
}
{
url: 'PUT/taskApi'
description: 'Redirects call to job component'
form:
action: "action"
params:
param1: 'param1'
param2: 'param2'
param3: 'param3'
example:
form:
action: "action"
params:
idPlan: 'idPlan'
idAlgorithm: 'idAlgorithm'
}
{
url: 'GET/taskExecutor/ping'
action: "ping"
description: 'Checks component is alive '
}
{
url: 'GET/taskExecutor/api'
action: "api"
description: 'Returns description of the JobsEX Front-End API'
}
]
@sendResponse res, message###
###
Forwards call to Jobs
###
jobsApi: (req, rep) =>
@logger.info "jobsApi() invoked"
msgString = JSON.stringify(req.query)
@logger.info "jobsApi() msgString # #{msgString}"
@jobsApiChannel.sendRequest msgString
.then (response) =>
message = JSON.parse(response.message.toString())
@logger.info "jobsApi() completed: #{util.inspect(message)}"
@sendResponse rep, message
.fail (err) =>
@logger.error "jobsApi() error #{err.message}"
@sendError rep, err.message
###
Forwards call to Task Executor
###
taskApi: (req, rep) =>
@logger.info "taskApi() started"
msgString = JSON.stringify(req.query)
@logger.info "taskApi() msg : #{msgString}"
@taskApiChannel.sendRequest msgString
.then (response) =>
@logger.info "taskApi() response received"
#[ [ { status: 'OK' }, <Buffer 7b 22 6d ... > ], null ]
message = JSON.parse(response.message.toString())
@logger.info "taskApi() completed #{util.inspect(message)}"
@sendResponse rep, message
.fail (err) =>
@logger.error "taskApi() error #{err.message}"
@sendError rep, err.message
###
A new job notification message has been published.
###
onJobNotification: (buffer) =>
@logger.info "onJobNotification()"
message = JSON.parse(buffer)
@logger.info "onJobNotification() message received:\n #{util.inspect message}"
if message.response?.job?.result
@logger.info "onJobNotification() result: \n#{util.inspect message.response.job.result}"
###
Sends notification to Jobs
###
sendNotification: (message) =>
@logger.info "sendNotificationToJobs() #{util.inspect(message)}"
###
Configure express
###
startExpress: () =>
@logger.info 'startExpress'
@app = express()
@app.use bodyParser.json()
@app.use bodyParser.urlencoded({extended: true})
# express routes
@app.use @path, @router
@logger.info "startExpress() app in path #{@path}"
@app.use (req, res, next) ->
return res.status(404).send('URL Not Found')
@httpServer = http.createServer(@app)
@httpServer.on 'error', @_onHttpError
@httpServer.listen @optServiceChannel
@logger.info "startExpress() completed"
###
Saves the state and stops the execution.
###
shutdown: ->
super()
@logger.info "shutdown()"
###
Changes the component instance parameters
Returns: 'true' if the reconfig can be take and 'false' otherwise.
###
reconfig: (resources, parameters) ->
@logger.info "reconfig()"
result = super parameters
return result
sendError: (res, msg) =>
res.writeHead 500, {"Content-type": "application/json"}
res.write msg
res.end()
@logger.error "sendError() #{util.inspect(msg)}"
@sendResponse(res, msg, 500)
sendResponse: (res, msg) =>
res.writeHead 200, {"Content-type": "application/json"}
res.write JSON.stringify msg
res.end()
@logger.info "sendResponse() #{util.inspect(msg)}"
###
Event listener for HTTP server "error" event.
###
_onHttpError: (error) =>
@logger.error 'Cfe:onHttpError'
throw error unless (error.syscall is 'listen')
switch error.code
when 'EACCES'
@logger.error 'Cfe:onHttpError: requires elevated privileges'
when 'EADDRINUSE'
@logger.error 'Cfe:onHttpError: address is already in use'
else
@logger.error "Cfe:onHttpError: code = #{error.code}"
throw error
module.exports = ModuleFE
## Patch for unit-tests: use ports instead of channels
module.exports.useNativeHttp = () -> http = require 'http'