adding cic-eth as sub dir
This commit is contained in:
1
apps/cic-eth/cic_eth/callbacks/__init__.py
Normal file
1
apps/cic-eth/cic_eth/callbacks/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .callback import Callback
|
||||
16
apps/cic-eth/cic_eth/callbacks/callback.py
Normal file
16
apps/cic-eth/cic_eth/callbacks/callback.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# third-party imports
|
||||
import celery
|
||||
|
||||
class Callback(celery.Task):
|
||||
"""Provides static properties for web connection context. The properties should be set directly.
|
||||
"""
|
||||
ssl = False
|
||||
"""If true, a SSL client certificate with default protocol for standard library ssl will be used for the HTTP connection."""
|
||||
ssl_cert_file = None
|
||||
"""Absolute path to client certificate PEM file"""
|
||||
ssl_key_file = None
|
||||
"""Absolute path to client key file"""
|
||||
ssl_password=None
|
||||
"""Password to unlock key file"""
|
||||
ssl_ca_file = None
|
||||
"""Client certificate CA chain"""
|
||||
64
apps/cic-eth/cic_eth/callbacks/http.py
Normal file
64
apps/cic-eth/cic_eth/callbacks/http.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# standard imports
|
||||
import json
|
||||
import ssl
|
||||
import os
|
||||
import urllib
|
||||
from urllib import request
|
||||
from urllib.request import urlopen
|
||||
|
||||
# third-party imports
|
||||
from . import Callback
|
||||
import celery
|
||||
|
||||
celery_app = celery.current_app
|
||||
logg = celery_app.log.get_default_logger()
|
||||
|
||||
|
||||
@celery_app.task(base=Callback, bind=True)
|
||||
def http(self, result, url, status_code):
|
||||
"""A generic web callback implementation for task results.
|
||||
|
||||
Input parameters depend on whether the callback is used as an error callback, or as a part of a celery chain.
|
||||
|
||||
The callback receives:
|
||||
|
||||
{
|
||||
'root_id': the uuid of the topmost task in the chain, which is known to the API caller.
|
||||
'status': <status_code>,
|
||||
'result': <result>,
|
||||
}
|
||||
|
||||
:param result: Task context object (on error) or return value of previous task (on success)
|
||||
:type result: Varies
|
||||
:param url: Url to HTTP POST results to
|
||||
:type url: str
|
||||
:param status_code: 0 on success, any other value is error
|
||||
:type status_code: int
|
||||
"""
|
||||
req = request.Request(url)
|
||||
data = {
|
||||
'root_id': self.request.root_id,
|
||||
'status': status_code,
|
||||
'result': result,
|
||||
}
|
||||
data_str = json.dumps(data)
|
||||
data_bytes = data_str.encode('utf-8')
|
||||
req.add_header('Content-Type', 'application/json')
|
||||
req.data = data_bytes
|
||||
|
||||
ctx = None
|
||||
if self.ssl:
|
||||
ctx = ssl.SSLContext()
|
||||
ctx.load_cert_chain(
|
||||
self.ssl_cert_file,
|
||||
self.ssl_key_file,
|
||||
self.ssl_password,
|
||||
)
|
||||
|
||||
response = urlopen(
|
||||
req,
|
||||
context=ctx,
|
||||
)
|
||||
|
||||
if response.status != 200:
|
||||
raise RuntimeError('Expected status 200 from remote server, but got {} {}'.format(response.status, response.msg))
|
||||
21
apps/cic-eth/cic_eth/callbacks/noop.py
Normal file
21
apps/cic-eth/cic_eth/callbacks/noop.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import celery
|
||||
|
||||
celery_app = celery.current_app
|
||||
logg = celery_app.log.get_default_logger()
|
||||
|
||||
|
||||
@celery_app.task(bind=True)
|
||||
def noop(self, result, param, status_code):
|
||||
"""A noop callback for task chains executed by external api methods. Logs the callback arguments.
|
||||
|
||||
:param result: Task context object (on error) or return value of previous task (on success)
|
||||
:type result: Varies
|
||||
:param param: Static value passed from api caller
|
||||
:type param: Varies
|
||||
:param status_code: 0 on success, any other value is error
|
||||
:type status_code: int
|
||||
:returns: True
|
||||
:rtype: bool
|
||||
"""
|
||||
logg.info('noop callback {} {} {}'.format(result, param, status_code))
|
||||
return True
|
||||
24
apps/cic-eth/cic_eth/callbacks/redis.py
Normal file
24
apps/cic-eth/cic_eth/callbacks/redis.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# standard imports
|
||||
import logging
|
||||
import json
|
||||
import redis as redis_interface
|
||||
|
||||
# third-party imports
|
||||
import celery
|
||||
|
||||
# local imports
|
||||
from . import Callback
|
||||
|
||||
celery_app = celery.current_app
|
||||
|
||||
logg = celery_app.log.get_default_logger()
|
||||
|
||||
|
||||
@celery_app.task(base=Callback, bind=True)
|
||||
def redis(self, result, destination, status_code):
|
||||
(host, port, db, channel) = destination.split(':')
|
||||
r = redis_interface.Redis(host=host, port=port, db=db)
|
||||
s = json.dumps(result)
|
||||
logg.debug('redis callback on host {} port {} db {} channel {}'.format(host, port, db, channel))
|
||||
r.publish(channel, s)
|
||||
r.close()
|
||||
24
apps/cic-eth/cic_eth/callbacks/tcp.py
Normal file
24
apps/cic-eth/cic_eth/callbacks/tcp.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# standard imports
|
||||
import socket
|
||||
import logging
|
||||
import json
|
||||
|
||||
# third-party imports
|
||||
import celery
|
||||
|
||||
# local imports
|
||||
from . import Callback
|
||||
|
||||
celery_app = celery.current_app
|
||||
|
||||
logg = celery_app.log.get_default_logger()
|
||||
|
||||
|
||||
@celery_app.task(base=Callback, bind=True)
|
||||
def tcp(self, result, destination, status_code):
|
||||
s = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
|
||||
(host, port) = destination.split(':')
|
||||
logg.debug('tcp callback to {} {}'.format(host, port))
|
||||
s.connect((host, int(port)))
|
||||
s.send(json.dumps(result).encode('utf-8'))
|
||||
s.close()
|
||||
Reference in New Issue
Block a user