-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
713 lines (623 loc) · 27.5 KB
/
service.py
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# -*- coding: utf-8 -*-
##############################################################################
#
# nxsugarpy, a Python library for building nexus services with python
# Copyright (C) 2016 by the nxsugarpy team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import pynexus as nxpy
import nxsugarpy.info as info
from nxsugarpy.log import *
from nxsugarpy.errors import *
from nxsugarpy.helpers import *
from nxsugarpy.stats import *
from nxsugarpy.signal import *
from six import string_types
import time
import threading
import traceback
try:
from Queue import Queue, Full
except ImportError:
from queue import Queue, Full
StateInitializing, StateConnecting, StateServing, StateStopped = range(4)
_connStateStr = {
StateInitializing: "initializing",
StateConnecting: "connecting",
StateServing: "serving",
StateStopped: "stopped",
}
class Method(object):
def __init__(self, f, testf = None, inSchema=None, resSchema=None, errSchema=None, pacts=[], methodOpts={}):
_populateMethodOpts(methodOpts)
self.disablePullLog = methodOpts["disablePullLog"]
self.enableResponseResultLog = methodOpts["enableResponseResultLog"]
self.enableResponseErrorLog = methodOpts["enableResponseErrorLog"]
self.inSchema = inSchema
self.resSchema = resSchema
self.errSchema = errSchema
self.pacts = pacts
self.f = f
self.testf = testf
def _populateMethodOpts(opts={}):
if opts == None:
opts = {}
if "disablePullLog" not in opts:
opts["disablePullLog"] = False
if "enableResponseResultLog" not in opts:
opts["enableResponseResultLog"] = False
if "enableResponseErrorLog" not in opts:
opts["enableResponseErrorLog"] = False
def _populateOpts(opts={}):
if opts == None:
opts = {}
if "pulls" not in opts or opts["pulls"] <= 0:
opts["pulls"] = 1
if "pullTimeout" not in opts:
opts["pullTimeout"] = 3600
if opts["pullTimeout"] <= 0:
opts["pullTimeout"] = 0
if "maxThreads" not in opts or opts["maxThreads"] <= 0:
opts["maxThreads"] = 1
if opts["maxThreads"] < opts["pulls"]:
opts["maxThreads"] = opts["pulls"]
if "testing" not in opts:
opts["testing"] = False
if "preaction" not in opts:
opts["preaction"] = None
if "postaction" not in opts:
opts["postaction"] = None
return opts
class Service(object):
def __init__(self, url, path, opts = {}):
opts = _populateOpts(opts)
self.name = "service"
self.description = ""
self.url = url
self._showurl = ""
self._connurl = ""
self.user = ""
self.password = ""
self.path = path
self.pulls = opts["pulls"]
self.pullTimeout = opts["pullTimeout"]
self.maxThreads = opts["maxThreads"]
self.statsPeriod = 300
self.gracefulExit = 20
self.logLevel = InfoLevel
self.version = "0.0.0"
self.testing = opts["testing"]
self.connState = None
self._nc = None
self._methods = {}
self._handler = None
self._stats = None
self._cmdQueue = Queue(self.pulls + 1024)
self._threadsSem = None
self._taskWorkers = {}
self._taskWorkersLock = None
self._statsTicker = None
self._stopLock = None
self._stopping = False
self._addedAsStoppable = False
self._debugEnabled = False
self._sharedConn = False
self._connid = ""
# only in nsugar-py
self._preaction = opts["preaction"]
self._postaction = opts["postaction"]
def getConn(self):
return self._nc
def _setConn(self, conn):
self._nc = conn
self._sharedConn = True
self._connid = conn.connid
def addMethod(self, name, f, testf=None, schema=None, methodOpts={}):
if len(self._methods) == 0:
self._initMethods()
method = Method(_defMethodWrapper(f), methodOpts=methodOpts)
if testf != None:
method.testf = _defMethodWrapper(testf)
self._methods[name] = method
if schema != None:
err, errM = self._addSchemaToMethod(name, schema)
if err != None:
self.logWithFields(ErrorLevel, errM, err)
return err
return None
def _initMethods(self):
self._methods["@schema"] = Method(self._schemaMethod)
self._methods["@info"] = Method(self._infoMethod)
self._methods["@ping"] = Method(self._pingMethod)
def _schemaMethod(self, task):
r = {}
for name, m in self._methods.items():
d = {}
if m.inSchema != None:
d["input"] = m.inSchema.json
if m.resSchema != None:
d["result"] = m.resSchema.json
if m.errSchema != None:
d["error"] = m.errSchema.result
if len(m.pacts) != 0:
d["pacts"] = m.pacts
r[name] = d
task.sendResult(r)
def _infoMethod(self, task):
task.sendResult({
"name": self.name,
"description": self.description,
"version": self.version,
"nxcli-version": "not implemented",
"wan-ips": info.wanIps,
"lan-ips": info.lanIps,
"user": info.user,
"directory": info.directory,
"uptime": int(time.time() - info.started),
"testing": self.isTesting(),
"stats": self._logStatsMap(),
})
def _pingMethod(self, task):
task.sendResult("pong")
def setHandler(self, h, methodOpts={}):
self._handler = Method(_defMethodWrapper(h), methodOpts=methodOpts)
def setDescription(self, description):
self.description = description
def setUrl(self, url):
self.url = url
def setUser(self, user):
self.user = user
def setPassword(self, password):
self.password = password
def setPath(self, path):
self.path = path
def setPulls(self, pulls):
self.pulls = pulls
def setMaxThreads(self, maxThreads):
self.maxThreads = maxThreads
def setPullTimeout(self, pullTimeout):
self.pullTimeout = pullTimeout
def setLogLevel(self, t):
t = t.lower()
setLogLevel(t)
if getLogLevel() == t:
self.logLevel = t
self._debugEnabled = (t == DebugLevel)
def setStatsPeriod(self, t):
self.statsPeriod = t
def setGracefulExit(self, t):
self.gracefulExit = t
def setVersion(self, major, minor, patch):
self.version = "{0}.{1}.{2}".format(major, minor, patch)
def setTesting(self, t):
if t:
self.testing = True
else:
self.testing = False
def isTesting(self):
return self.testing
def getMethods(self):
if self._handler != None:
return []
return [x for x in self._methods.keys()]
def gracefulStop(self):
if self._cmdQueue != None:
try:
self._cmdQueue.put_nowait(("graceful", ""))
except Full:
log(PanicLevel, "queue", "cmdQueue is full")
pass
def stop(self):
if self._cmdQueue != None:
try:
self._cmdQueue.put_nowait(("stop", ""))
except Full:
log(PanicLevel, "queue", "cmdQueue is full")
pass
def _setState(self, state):
if self.connState != None:
self.connState(self.getConn(), state)
def serve(self, errQueue=None):
self._setState(StateInitializing)
self.setLogLevel(self.logLevel)
# Check service
if len(self._methods) == 0 and self._handler == None:
errs = "no methods to serve"
self.logWithFields(ErrorLevel, {"type": "no_methods"}, errs)
return errs
if self.maxThreads < 0:
self.maxThreads = 1
if self.pulls < 0:
self.pulls = 1
if self.maxThreads < self.pulls:
self.maxThreads = self.pulls
if self.pullTimeout < 0:
self.pullTimeout = 0
if self.statsPeriod < 0.1:
if self.statsPeriod < 0:
self.statsPeriod = 0
else:
self.statsPeriod = 0.1
if self.gracefulExit < 1:
self.gracefulExit = 1
if self.version == "":
self.version = "0.0.0"
if not self._sharedConn:
self._setState(StateConnecting)
# Dial and login
scheme, user, password, host, port = parseNexusUrl(self.url, self.user, self.password)
self.user = user
self.password = password
self._showurl = "{0}://{1}:{2}".format(scheme, host, port)
self._connurl = "{0}://{1}:{2}@{3}:{4}".format(scheme, user, password, host, port)
try:
self._nc = nxpy.Client(self._connurl)
except Exception as e:
errs = "can't connect to nexus server ({0}): {1}".format(self._showurl, str(e))
logWithFields(ErrorLevel, "server", {"type": "connection_error"}, errs)
return errs
if not self._nc.is_version_compatible:
logWithFields(WarnLevel, "server", {"type": "incompatible_version"}, "connecting to an incompatible version of nexus at ({0}): client ({1}) server ({2})", self._showurl, nxpy.__version__, self._nc.nexus_version)
if not self._nc.is_logged:
errs = "can't login to nexus server ({0}) as ({1}): {2}".format(self._showurl, self.user, errToStr(self._nc.login_error))
logWithFields(ErrorLevel, "server", {"type": "login_error"}, errs)
return errs
self._connid = self._nc.connid
self._setState(StateServing)
# Output
self.logWithFields(InfoLevel, self._logMap(), str(self))
# Serve
self._stats = Stats()
self._stopping = False
self._stopLock = threading.Lock()
if self._threadsSem == None:
self._threadsSem = threading.Semaphore(self.maxThreads)
self._taskWorkers = {}
self._taskWorkersLock = threading.Lock()
pullWorkers = []
for i in range(self.pulls):
worker = threading.Thread(target=self._taskPull, args=(i+1,))
worker.daemon = True
pullWorkers.append(worker)
worker.start()
if not self._sharedConn and not self._addedAsStoppable:
addStoppable(self)
self._addedAsStoppable = True
gracefulTimeout = None
if self.statsPeriod > 0:
self._statsTicker = threading.Timer(self.statsPeriod, self._statsTickerHandler)
self._statsTicker.daemon = True
self._statsTicker.start()
graceful = False
errs = None
while True:
cmd, reason = self._cmdQueue.get(block=True, timeout=None)
if cmd == "stats_ticker":
if self._debugEnabled:
self.logWithFields(DebugLevel, self._logStatsMap(), self._logStatsMsg())
elif cmd == "graceful" or cmd == "stop":
if cmd == "stop":
graceful = False
self._setStopping()
self._nc.close()
gracefulTimeout = threading.Timer(1.0, self._gracefulTimeoutHandler)
gracefulTimeout.daemon = True
gracefulTimeout.start()
continue
graceful = True
if not self._isStopping():
self._setStopping()
gracefulTimeout = threading.Timer(self.gracefulExit, self._gracefulTimeoutHandler)
gracefulTimeout.daemon = True
gracefulTimeout.start()
waitWorkers = threading.Thread(target=self._waitWorkers)
waitWorkers.daemon = True
waitWorkers.start()
elif cmd == "task_workers_done":
self._nc.close()
waitPullers = threading.Thread(target=self._waitPullers, args=(pullWorkers,))
waitPullers.daemon = True
waitPullers.start()
continue
elif cmd == "graceful_timeout":
if not graceful:
self.logWithFields(DebugLevel, {"type": "stop"}, "stop: done")
break
self._nc.close()
errs = "graceful: timeout after {0}".format(secondsToStr(self.gracefulExit))
self.logWithFields(ErrorLevel, {"type": "graceful_timeout"}, errs)
break
elif cmd == "connection_ended":
if self._isStopping():
if graceful:
self.logWithFields(DebugLevel, {"type": "graceful"}, "graceful: done")
else:
self.logWithFields(DebugLevel, {"type": "stop"}, "stop: done")
break
errs = "stop: nexus connection ended: {0}".format(reason)
self.logWithFields(ErrorLevel, {"type": "connection_ended"}, errs)
break
if gracefulTimeout != None:
gracefulTimeout.cancel()
if self._statsTicker != None:
self._statsTicker.cancel()
self._nc = None
self._setState(StateStopped)
if errQueue != None:
try:
errQueue.put_nowait(errs)
except Full:
log(PanicLevel, "queue", "errQueue is full")
pass
return errs
def _taskPull(self, i):
while True:
if self._isStopping():
return
self._threadsSem.acquire()
self._stats.addThreadsUsed(1)
if self._isStopping():
self._threadsSem.release()
self._stats.addThreadsUsed(-1)
return
# Make a task pull
self._stats.addTaskPullsDone(1)
task, err = self._nc.taskPull(self.path, self.pullTimeout)
if err != None:
if isNexusErrCode(err, ErrTimeout):
self._stats.addTaskPullsTimeouts(1)
self._threadsSem.release()
self._stats.addThreadsUsed(-1)
continue
if not self._isStopping() or not (isNexusErrCode(err, ErrCancel) or isNexusErrCode(err, ErrConnClosed)):
errReason = errToStr(err)
self.logWithFields(ErrorLevel, {"type": "pull_error"}, "pull {0}: pulling task: {1}", i, errReason)
try:
self._cmdQueue.put_nowait(("connection_ended", errReason))
except Full:
log(PanicLevel, "queue", "cmdQueue is full")
pass
self._nc.close()
self._threadsSem.release()
self._stats.addThreadsUsed(-1)
return
# A task has been pulled
self._stats.addTasksPulled(1)
# Get method or global handler
method = self._handler
if method == None:
if task.method not in self._methods:
task.sendError(ErrMethodNotFound, "", None)
self._stats.addTasksMethodNotFound(1)
self._threadsSem.release()
self._stats.addThreadsUsed(-1)
continue
method = self._methods[task.method]
# Log the task
if not method.disablePullLog:
self.logWithFields(InfoLevel, {"type": "pull", "path": task.path, "method": task.method, "params": task.params, "tags": task.tags}, "pull {0}: task[ path={1} method={2} params={3} tags={4} ]", i, task.path, task.method, task.params, task.tags)
# Execute the task
taskExecute = threading.Thread(target=self._taskExecute, args=(i, method, task))
taskExecute.daemon = True
taskExecute.start()
def _taskExecute(self, n, method, task):
self._taskWorkersLock.acquire()
th = threading.current_thread()
self._taskWorkers[th] = True
self._taskWorkersLock.release()
self._stats.addTasksRunning(1)
try:
metadata = {}
if isinstance(task.params, dict) and "@metadata" in task.params:
metadata = task.params["@metadata"]
if self._preaction != None:
try:
self._preaction(task)
except Exception:
tbck = traceback.format_exc()
self.log(ErrorLevel, {"type": "task_exception"}, "pull {0}: panic serving task on preaction: {1}", n, tbck)
# Pact: return mock
if "pact" in metadata and metadata["pact"]:
pactOk = False
for pact in method.pacts:
if isinstance(pact.input, dict):
pact.input["@metadata"] = metadata
if pact.input == task.params:
pactOk = True
task.sendResult(pact.output)
break
if not pactOk:
task.SendError(ErrPactNotDefined, ErrStr[ErrPactNotDefined], None)
else:
errReturned = False
# Validate input schema
if method.inSchema != None:
self.log(InfoLevel, "not implemented: we should check the following input schema here: {0}", method.inSchema)
# Implement json schema validation!
# Execute the task
if not errReturned:
if "testing" in metadata and metadata["testing"]:
if method.testf == None:
task.sendError(ErrTestingMethodNotProvided, ErrStr[ErrTestingMethodNotProvided], None)
errReturned = True
else:
method.testf(task)
else:
method.f(task)
# Log response
if method.enableResponseResultLog and task.tags and "@local-response-result" in task.tags and task.tags["@local-response-result"] != None:
self.logWithFields(InfoLevel, {"type": "response_result", "path": task.path, "method": task.method}, "pull {0}: task[ path={1} method={2} result={3} ]", n, task.path, task.method, task.tags["@local-response-result"])
if method.enableResponseErrorLog and task.tags and "@local-response-error" in task.tags and task.tags["@local-response-error"] != None:
self.logWithFields(InfoLevel, {"type": "response_error", "path": task.path, "method": task.method}, "pull {0}: task[ path={1} method={2} error={3} ]", n, task.path, task.method, task.tags["@local-response-error"])
# Validate result and error schema
if not errReturned:
if method.resSchema != None and "@local-response-result" in task.tags and task.tags["@local-response-result"] != None:
self.log(InfoLevel, "not implemented: we should check the following result schema here: {0}", method.resSchema)
# Implement jscon schema validation!
if method.errSchema != None and "@local-response-error" in task.tags and task.tags["@local-response-error"] != None:
self.log(InfoLevel, "not implemented: we should check the following error schema here: {0}", method.errSchema)
# Implement jscon schema validation!
if self._postaction != None:
try:
self._postaction(task)
except Exception:
tbck = traceback.format_exc()
self.log(ErrorLevel, {"type": "task_exception"}, "pull {0}: panic serving task on postaction: {1}", n, tbck)
self._stats.addTasksServed(1)
except Exception:
self._stats.addTaskPanic(1)
tbck = traceback.format_exc()
self.logWithFields(ErrorLevel, {"type": "task_exception"}, "pull {0}: panic serving task: {1}", n, tbck)
task.sendError(ErrInternal, tbck, None)
self._taskWorkersLock.acquire()
th = threading.current_thread()
if th in self._taskWorkers:
self._taskWorkers.pop(th, None)
self._taskWorkersLock.release()
self._threadsSem.release()
self._stats.addThreadsUsed(-1)
self._stats.addTasksRunning(-1)
def _waitWorkers(self):
while True:
self._taskWorkersLock.acquire()
wlist = list(self._taskWorkers.keys())
self._taskWorkersLock.release()
if len(wlist) == 0:
break
for worker in wlist:
worker.join()
try:
self._cmdQueue.put_nowait(("task_workers_done", ""))
except Full:
log(PanicLevel, "queue", "cmdQueue is full")
pass
def _waitPullers(self, pullers):
for worker in pullers:
worker.join()
try:
self._cmdQueue.put_nowait(("connection_ended", "closed by service"))
except Full:
log(PanicLevel, "queue", "cmdQueue is full")
pass
def _gracefulTimeoutHandler(self):
try:
self._cmdQueue.put_nowait(("graceful_timeout", ""))
except Full:
log(PanicLevel, "queue", "cmdQueue is full")
pass
def _statsTickerHandler(self):
try:
self._cmdQueue.put_nowait(("stats_ticker", ""))
except Full:
log(PanicLevel, "queue", "cmdQueue is full")
pass
self._statsTicker = threading.Timer(self.statsPeriod, self._statsTickerHandler)
self._statsTicker.daemon = True
self._statsTicker.start()
def _setStopping(self):
self._stopLock.acquire()
self._stopping = True
self._stopLock.release()
def _isStopping(self):
self._stopLock.acquire()
stopping = self._stopping
self._stopLock.release()
return stopping
def log(self, level, message, *args, **kwargs):
fields = {}
if self._connid != "":
fields["connid"] = self._connid
logWithFields(level, self.name, fields, message, *args, **kwargs)
def logWithFields(self, level, fields, message, *args, **kwargs):
if not isinstance(fields, dict):
fields = {}
if self._connid != "":
fields["connid"] = self._connid
logWithFields(level, self.name, fields, message, *args, **kwargs)
def _logMap(self):
return {
"type": "start",
"url": self.url,
"user": self.user,
"connid": self._connid,
"version": self.version,
"path": self.path,
"pulls": self.pulls,
"pullTimeout": secondsToStr(self.pullTimeout),
"maxThreads": self.maxThreads,
"logLevel": self.logLevel,
"statsPeriod": secondsToStr(self.statsPeriod),
"gracefulExit": secondsToStr(self.gracefulExit),
}
def __repr__(self):
tup = (self.url, self.user, self._connid, self.version, self.path, self.pulls, secondsToStr(self.pullTimeout), self.maxThreads, self.logLevel, secondsToStr(self.statsPeriod), secondsToStr(self.gracefulExit))
return "config: url={0} user={1} connid={2} version={3} path={4} pulls={5} pullTimeout={6} maxThreads={7} logLevel={8} statsPeriod={9} gracefulExit={10}".format(*tup)
def _logStatsMap(self):
return {
"threadsUsed": self._stats.threadsUsed,
"threadsMax": self.maxThreads,
"taskPullsDone": self._stats.taskPullsDone,
"taskPullTimeouts": self._stats.taskPullTimeouts,
"tasksPulled": self._stats.tasksPulled,
"tasksPanic": self._stats.tasksPanic,
"tasksMethodNotFound": self._stats.tasksMethodNotFound,
"tasksServed": self._stats.tasksServed,
"tasksRunning": self._stats.tasksRunning,
}
def _logStatsMsg(self):
tup = (self._stats.threadsUsed, self.maxThreads, self._stats.taskPullsDone, self._stats.taskPullTimeouts, self._stats.tasksPulled, self._stats.tasksPanic, self._stats.tasksMethodNotFound, self._stats.tasksServed, self._stats.tasksRunning)
return "stats: threads[ {0}/{1} ] task_pulls[ done={2} timeouts={3} ] tasks[ pulled={4} panic={5} errmethod={6} served={7} running={8} ]".format(*tup)
def _defMethodWrapper(f):
def wrapped(task):
res, err = f(task)
if res != None:
task.tags["@local-response-result"] = res
if err != None:
task.tags["@local-response-error"] = err
if "@local-repliedTo" in task.tags:
return
if err != None:
err = formatAsJsonRpcErr(err)
task.sendError(err["code"], err["message"], err["data"])
else:
task.sendResult(res)
return wrapped
def replyToWrapper(f):
def wrapped(task):
if isinstance(task.params, dict) and "replyTo" in task.params and isinstance(task.params["replyTo"], dict):
replyTo = task.params["replyTo"]
if "path" in replyTo and isinstance(replyTo["path"], string_types) and "type" in replyTo and isinstance(replyTo["type"], string_types) and replyTo["type"] in ["pipe", "service"]:
res, errm = f(task)
task.tags["@local-repliedTo"] = True
_, err = task.accept()
if err != None:
log(WarnLevel, "replyto wrapper", "could not accept task: {0}", errToStr(err))
elif replyTo["type"] == "pipe":
pipe, err = task.nexusConn.pipeOpen(replyTo["path"])
if err != None:
log(WarnLevel, "replyto wrapper", "could not open received pipeId ({0}): {1}", replyTo["path"], errToStr(err))
else:
_, err = pipe.write({"result": res, "error": errm, "task": {"path": task.path, "method": task.method, "params": task.params, "tags": task.tags}})
if err != None:
log(WarnLevel, "replyto wrapper", "error writing response to pipe: {0}", errToStr(err))
elif replyTo["type"] == "service":
_, err = task.nexusConn.taskPush(replyTo["path"], {"result": res, "error": errm, "task": {"path": task.path, "method": task.method, "params": task.params, "tags": task.tags}}, timeout=30, detach=True)
if err != None:
log(WarnLevel, "replyto wrapper", "could not push response task to received path ({0}): {1}", replyTo["path"], errToStr(err))
return res, errm
return f(task)
return f(task)
return wrapped