class NewsServiceAPI: name = 'news_service_api' query_stack = RpcProxy('query_stack') command_stack = RpcProxy('command_stack') @http('POST', '/create_news') def create_news(self, request): schema = NewsSchema(strict=True) try: data = schema.loads(request.get_data(as_text=True)).data except ValueError: return 400, 'Invalid payload' try: news_id = self.command_stack.news_domain(data) localtion = { 'Location': 'http://*****:*****@http('GET', '/news/list/page/<int:page>/limit/<int:limit>') def list_news(self, request, page, limit): respose_data = self.query_stack.list_news(page, limit) return 200, {'Content-Type': 'application/json'}, respose_data @http('GET', '/news/<string:news_id>') def get_news(self, request, news_id): respose_data = self.query_stack.get_news(news_id) return 200, {'Content-Type': 'application/json'}, respose_data
class HttpService: name = "http_service" video_index = RpcProxy("video_index") search_engine = RpcProxy("search_engine") axi_service = RpcProxy("axi_service") @http('GET', '/videos/<string:series>') def load_videos(self, request, series): videos = self.video_index.videos("series") return json.dumps({'videos': videos}) @http('GET', '/subtitle/<string:query>') def search_subtitles(self, request, query): videos = self.search_engine.search('', query) return json.dumps({'videos': videos}) @http('GET', '/axi/write/<string:addr>/<string:data>/') def axi_write(self, request, addr, data): resp = self.axi_service.write('', addr, data) print(resp) return resp #return json.dumps({'resp': resp}) @http('GET', '/axi/read/<string:addr>') def axi_read(self, request, addr): resp = self.axi_service.read(addr) print(resp) return resp #return json.dumps({'resp': resp}) @http('GET', '/axi/reset') def axi_reset(self, request): self.axi_service.reset() return "reset"
class GatewayService: name = 'gateway' mailer_rpc = RpcProxy('mailer_service') sms_rpc = RpcProxy('sms_service') tweetbot_rpc = RpcProxy('tweetbot_service') @http('POST', '/mailer') def send_email(self, request): data = json.loads(request.get_data(as_text=True)) message = self.mailer_rpc.create(data['receiver_email'], data['receiver_name'], data['mail_message']) return message @http('POST', '/sms') def send_sms(self, request): data = json.loads(request.get_data(as_text=True)) message = self.sms_rpc.create(data['receiver_number'], data['sms_message']) return message @http('POST', '/tweet') def post_tweet(self, request): data = json.loads(request.get_data(as_text=True)) message = self.tweetbot_rpc.create(data['tweet_message']) return message
class FunctionChurnService: name = 'functionchurn' config = Config() redis = Redis() parser_rpc = RpcProxy('parser') project_rpc = RpcProxy('project') repository_rpc = RpcProxy('repository') @rpc def collect(self, project, **options): logger.debug(project) functionchurn = list() project = ProjectSchema().load(self.project_rpc.get(project)) if self.parser_rpc.is_supported(project.language): changes = self._get_changes(project) helper = Helper(project, self.repository_rpc, self.parser_rpc, self.redis) functionchurn = helper.collect(changes) return FunctionChurnSchema(many=True).dump(functionchurn) def _get_changes(self, project): changes = self.repository_rpc.get_changes(project.name) return ChangesSchema(many=True).load(changes)
def test_regular_parameters(self, parameter, mock_container, producer): """ Verify that most parameters can be specified at RpcProxy instantiation time. """ mock_container.config = {'AMQP_URI': 'memory://localhost'} mock_container.shared_extensions = {} mock_container.service_name = "service" worker_ctx = Mock() worker_ctx.container = mock_container worker_ctx.context_data = {} value = Mock() rpc_proxy = RpcProxy("service-name", **{ parameter: value }).bind(mock_container, "service_rpc") rpc_proxy.setup() rpc_proxy.rpc_reply_listener.setup() service_rpc = rpc_proxy.get_dependency(worker_ctx) service_rpc.method.call_async() assert producer.publish.call_args[1][parameter] == value
class MainEngineService: name = 'mainengine_service' translator_rpc = RpcProxy('translator_service') splitter_rpc = RpcProxy('splitter_service') adaptor_rpc = RpcProxy('adaptor_service') @rpc def hello_translator(self, name): message = self.translator_rpc.hello(name) return message @rpc def hello_splitter(self, name): message = self.splitter_rpc.split(name) return message @rpc def hello_adaptor(self, name): message = self.adaptor_rpc.hello(name) return message @rpc def adaptor_auth(self, username, password, mano): message = self.adaptor_rpc.hello(name) return message
class GatewayService: name='gateway' airports_rpc=RpcProxy('airports_sevice') trips_rpc=RpcProxy('trips_service') @http('GET','/airport/<string:airport_id>') def get_airport(self, request,airport_id): airport=self.airports_rpc.get(airport_id) return json.dumps({'airport':airport}) @http('POST','/airport') def post_airport(self,request): data=json.loads(request.get_data(as_text=True)) airport_id=self.airports_rpc.create(data['airport']) return airport_id @http('GET','/trip/<string:trip_id>') def get_trip(self,request,trip_id): trip=self.trips_rpc.get(trip_id) return json.dumps({'trips':trip}) @http('POST','/trip') def post_trip(self,request): data=json.loads(request.get_data(as_text=True)) trip_id=self.trips_rpc.create(data['airport_from'],data['airport_to']) return trip_id
class GatewayService: name = 'gateway_service' templates_rpc = RpcProxy('templates_service') crawl_rpc = RpcProxy('crawl_service') @http('POST', '/discover') def discover(self, request): data = json.loads(request.get_data(as_text=True)) result = self.crawl_rpc.discover(data.get('url'), data.get('browser')) return json.dumps(result) @http('POST', '/crawl') def crawl(self, request): data = json.loads(request.get_data(as_text=True)) result = {} if data.get('url'): result = self.crawl_rpc.crawl(url=data.get('url'), browser=data.get('browser')) elif data.get('urls'): result = self.crawl_rpc.crawl_urls(urls=data.get('urls'), browser=data.get('browser')) return json.dumps(result) @http('GET', '/') def index(self, request): webpack = json.loads(open('frontend/webpack-stats-prod.json').read()) if webpack['status'] == 'done': context = {'webpack': webpack['chunks']['main'][0]['path']} return Response( jinja_env.get_template('index.html').render(**context), mimetype='text/html') return Response()
class KeywordService: name = 'keyword' config = Config() project_rpc = RpcProxy('project') repository_rpc = RpcProxy('repository') @rpc def collect(self, project, **options): logger.debug(project) project = ProjectSchema().load(self.project_rpc.get(project)) if project.language.lower() not in self.config['KEYWORDS']: raise LanguageNotSupported(f'{project.language} not supported') keywords = self.config['KEYWORDS'].get(project.language.lower()) keywrd = Keywrd(keywords=keywords) commits = self.repository_rpc.get_commits(project.name) chunks = utilities.chunk(commits, size=round(len(commits) * 0.01)) pool = GreenPool(os.cpu_count()) arguments = [(project, c, self.repository_rpc) for c in chunks] keyword = list() for patches in pool.starmap(_get_patches, arguments): patches = PatchSchema(many=True).load(patches) keyword.extend(keywrd.get(patches)) return KeywordSchema(many=True).dump(keyword)
class ApiService: name = 'api' command_rpc = RpcProxy('command_stack') query_rpc = RpcProxy('query_stack') @http('POST', '/user') def post(self, request): data = json.loads(request.get_data(as_text=True)) if not data: return 400, 'Invalid payload' try: response = self.command_rpc.create_user(data) return 201, json.dumps(response) except Exception as e: return 500, e @http('GET', '/users/<int:page>/<int:limit>') def get_list(self, request, page, limit): response = self.query_rpc.get_all_users(page, limit) return 200, {'Content-Type': 'application/json'}, response @http('GET', '/users/<string:user_id>') def get(self, request, user_id): response = self.query_rpc.get_user(user_id) return 200, {'Content-Type': 'application/json'}, response
class CampaignProcessorService: """Process campaign and fetch subscribers """ name = "campaign_processor_service" stats_service = RpcProxy("stats_service") subscriber_processor_service = RpcProxy("subscriber_processor_service") @rpc def process_campaign(self, payload): logger.info("CampaignProcessorService.process_campaign: " f"processing campaign - {payload}") start_time = time.time() # @todo #1:15min daily count check # total_limit = payload['total_limit'] # total_count = (self.stats_service # .get_pushes_total_count(payload["id"])) # daily_count = (self.stats_service # .get_pushes_daily_count(payload["id"])) # if total_count >= total_limit or daily_count >= total_limit: # print("CampaignProcessorService.process_campaign: " # f"campaign limit exceeded: {payload}") # return None targetings = payload["targetings"] redis_client = redis.Redis(connection_pool=REDIS_POOL, socket_timeout=1) cab = Cabinet(settings.CABINET_URL) cached_cabinet = CachedCabinet( cab, RedisEngine(redis_client, prefix="CABINET_CACHE", ttl=30)) cabinet_settings = cached_cabinet.general() start_hour = cabinet_settings["start_hour"] end_hour = cabinet_settings["end_hour"] hours_whitelist = list(range(start_hour, end_hour + 1)) volume = payload["subscriber_selection_size"] with Timer() as t: subscribers = (get_subscribers(targetings, hours_whitelist, volume)) logger.debug("CampaignProcessorService.process_campaign: " "Request to get subsctiber took %.03f sec." % t.interval) if not subscribers: logger.info("CampaignProcessorService.process_campaign: " f"no subscribers found for campaign: #{payload['id']}") return for subscriber in subscribers: # time1 = time.time() message = dict(campaign=payload, subscriber=subscriber) subscriber_processor_ref.tell(message) # (self.subscriber_processor_service.process_subscriber # .call_async(dict(campaign=payload, subscriber=subscriber))) # time2 = time.time() # logger.debug("CampaignProcessorService.process_campaign: " # "called process_subscriber in " # f"{int((time2 - time1) * 1000)}ms") end_time = time.time() logger.info("CampaignProcessorService.process_campaign: " f"for campaign #{payload['id']} " f"processed {len(subscribers)} subscribers " f"in {(end_time - start_time) * 1000}ms")
class PutterService(object): name = 'PutterService' s0 = RpcProxy('DictService0') s1 = RpcProxy('DictService1') @timer(interval=TIMEOUT) def test(self): global counter global avg_time global fails echo('Putting counter') t1 = time.time() rv = self.s0.put('counter', str(counter)) t2 = time.time() if rv: echo('Success: ' + str(t2 - t1) + ' seconds') else: echo('Fail') fails += 1 echo('Getting counter') rv = self.s1.get('counter') echo('Success: counter = ' + str(rv)) counter += 1 avg_time = (avg_time * (counter - 1) + (t2 - t1)) / counter echo('Average put time: ' + str(avg_time)) echo('Fail percentage: ' + str(float(fails) / counter))
class StarTrekHTTPService: log = StructlogDependency() name = 'star_trek_service' ship_rpc = RpcProxy('ship_service') crew_rpc = RpcProxy('crew_service') @http('GET', '/ship/<string:ship_id>') def get_ship(self, request, ship_id): ship = self.ship_rpc.get(ship_id) return json.dumps(dict(ship=ship)) @http('POST', '/ship') def create_ship(self, request): data = json.loads(request.get_data(as_text=True)) ship_id = self.ship_rpc.add(data['ship']) return json.dumps(dict(ship_id=ship_id)) @http('GET', '/crew/<string:crew_id>') def get_crew(self, request, crew_id): crew = self.crew_rpc.get(crew_id) return json.dumps(dict(crew=crew)) @http('POST', '/crew') def add_crew(self, request): data = json.loads(request.get_data(as_text=True)) self.log.info("payload", data=data) crew_id = self.crew_rpc.add(data['ship_id'], data['name'], data['designation'], data['race']) return json.dumps(dict(crew_id=crew_id))
class GatewayService(object): name = "gateway_service" message_service_rpc = RpcProxy("message_service") user_service_rpc = RpcProxy("user_service") @http("GET", "/image") def captcha_service(self, request): image_id = request.args.get("image_id") _, code, image = captcha.generate_captcha() self.message_service_rpc.store_code.call_async(image_id, code) return Response( image, mimetype='img/jpg' ) @http("POST", "/sms") def send_sms_service(self, request): is_success = self.message_service_rpc.send_sms.call_async((request.get_data(as_text=True))) return json.dumps({"message": "ok", "code": 200}) @http("POST", "/login") def login_service(self, request): schema = LoginUserSchema(strict=True) try: user_data = schema.loads(request.get_data(as_text=True)).data except ValueError as e: print(e) user = self._login_user(user_data) token = generate_token(user) return token def _login_user(self, user_data): return self.user_service_rpc.login_user(user_data) @http("POST", "/register") def register_service(self, request): schema = RegisterUserSchema(strict=True) try: user_data = schema.loads(request.get_data(as_text=True)).data except ValueError as e: print(e) user = self._create_user(user_data) token = generate_token(user) return token def _create_user(self, user_data): return self.user_service_rpc.create_user(user_data) @http("get", "/user") def get_user(self, request): token = request.args.get("token") id, username = resolute_token(token) user = self._get_user(id) return user.get("username") def _get_user(self, id): return self.user_service_rpc.get_user(id)
class Service(object): name = "service" foo_proxy = RpcProxy("foo_service") bar_proxy = RpcProxy("bar_service") baz_proxy = RpcProxy("baz_service") @rpc def method(self, arg): self.foo_proxy.remote_method(arg)
class Timer: name = "timer_service" campaigns_runner_service = RpcProxy("campaigns_runner_service") syncer_service = RpcProxy("syncer_service") @timer(interval=1) def run_campaigns(self): logger.debug("tick") self.campaigns_runner_service.run.call_async()
class HotelGatewayService: """ A class for the hotel gateway service """ name = "hotel_gateway" bookings_rpc = RpcProxy('bookings_service') rooms_rpc = RpcProxy('rooms_service') @http('GET', '/bookings/<string:booking_id>') def get_bookings(self, request, booking_id): """ Get the bookings :param request: :param booking_id: :return: """ booking = self.bookings_rpc.get(booking_id) return json.dumps({'booking': booking}) @http('POST', '/bookings') def post_bookings(self, request): """ create a booking :param request: :return: """ data = json.loads(request.get_data(as_text=True)) booking_id = self.bookings_rpc.create(data['booking']) return booking_id @http('GET', '/room/<string:room_id>') def get_room(self, request, room_id): """ Get a particular room information :param request: :param room_id: :return: """ room = self.rooms_rpc.get(room_id) return json.dumps({'room': room}) @http('POST', '/room') def create_room(self, request): """ Make a room :param request: :return: """ data = json.loads(request.get_data(as_text=True)) room_id = self.rooms_rpc.create(data['room_user'], data['room_number']) return room_id
class WebService: name = "web_service" jukebox_service = RpcProxy("jukebox_service") player_service = RpcProxy("player_service") with open("index.html") as f: index_html = f.read() @http('GET', '/') def index(self, request): all_songs = self.jukebox_service.get_all() all_songs_sorted_by_value = sorted(all_songs.items(), key=lambda kv: kv[1]) all_songs_sorted_by_value.reverse() now_playing = self.player_service.get_now_playing() template = Template(self.index_html) h = template.render(all_songs=all_songs_sorted_by_value, now_playing=now_playing) r = Response(h) r.content_type = "text/html" return r @http('GET', '/like/<string:f_name>') def like(self, request, f_name): self.jukebox_service.like(f_name) return 302, {'Location': '/'}, "" @http('GET', '/delete/<string:f_name>') def delete(self, request, f_name): os.remove(os.path.join("mp3", f_name)) self.jukebox_service.update() return 302, {'Location': '/'}, "" @http('GET', '/play/<string:f_name>') def play(self, request, f_name): self.player_service.play(f_name) return 302, {'Location': '/'}, "" @http('GET', '/playnext/') def playnext(self, request): self.jukebox_service.play_next() return 302, {'Location': '/'}, "" @http('GET', '/stop_playing/') def stop_playing(self, request): self.player_service.pause() return 302, {'Location': '/'}, "" @http('POST', '/upload_files') def upload_files(self, request): for f in request.files.getlist("mp3_files"): if f.filename.endswith(".mp3"): f_name = os.path.join("mp3", f.filename) f.save(f_name) self.jukebox_service.update() return 302, {'Location': '/'}, ""
class PutterService(object): name = 'PutterService' dict_a = RpcProxy('DictService0') dict_b = RpcProxy('DictService1') @timer(interval=1) def test(self): global counter self.dict_a.put('counter', str(counter)) echo('counter = ' + str(self.dict_b.get('counter'))) counter += 1
class GatewayRpcServiceHost(object): name = 'gateway_rpc_service' level_rpc = RpcProxy('level_service') shotgun_rpc = RpcProxy('shotgun_service') def __init__(self): print dir(GatewayRpcServiceHost.level_rpc) @rpc def create_level(self, levelspec): gateway = GatewayService(self) gateway.build_level(levelspec)
class ExampleService(object): name = 'exampleservice' logger = WorkerErrorLogger() translate = Translator() example_rpc = RpcProxy('exampleservice') unknown_rpc = RpcProxy('unknown_service') @rpc def task_a(self, *args, **kwargs): return "result_a" @rpc def task_b(self, *args, **kwargs): return "result_b" @rpc(expected_exceptions=ExampleError) def broken(self): raise ExampleError("broken") @rpc(expected_exceptions=(KeyError, ValueError)) def very_broken(self): raise AttributeError @rpc def call_async(self): res1 = self.example_rpc.task_a.call_async() res2 = self.example_rpc.task_b.call_async() res3 = self.example_rpc.echo.call_async() return [res2.result(), res1.result(), res3.result()] @rpc def deprecated_async(self): return self.example_rpc.echo. async ().result() @rpc def call_unknown(self): return self.unknown_rpc.any_method() @rpc def echo(self, *args, **kwargs): return args, kwargs @rpc def say_hello(self): return self.translate(hello) @event_handler('srcservice', 'eventtype') def async_task(self): pass # pragma: no cover
class MLService(object): name = "mltask" dash_rpc = RpcProxy("dashtask") db_rpc = RpcProxy("dbtask") processor = PredictLife() @rpc def get_predict(self, data): result = self.processor.predict(data) self.dash_rpc.send_the_data_to_kafka(result) self.db_rpc.save_the_data_to_hbase(result) return result
class WebServer: name = 'web_server' message_service = RpcProxy('message_service') templates = Jinja2() @http('GET', '/') def home(self, request): messages = self.message_service.get_all_messages() rendered_template = self.templates.render_home(messages) html_response = create_html_response(rendered_template) return html_response @http('POST', '/messages') def post_message(self, request): data_as_text = request.get_data(as_text=True) try: data = json.loads(data_as_text) except json.JSONDecodeError: return 400, 'JSON payload expected' try: message = data['message'] except KeyError: return 400, 'No message given' self.message_service.save_message(message) return 204, '' @http('GET', '/messages') def get_messages(self, request): messages = self.message_service.get_all_messages() return create_json_response(messages)
class Service(object): name = "service" proxy = RpcProxy("foo_service") @rpc def method(self): pass
class ApiService: name = 'api' query_rpc = RpcProxy('query_stack') @http('POST', '/user') def post(self, request): data = json.loads(request.get_data(as_text=True)) if not data: return 400, 'Invalid payload' try: with ClusterRpcProxy(CONFIG) as cluster_rpc: data['id'] = str(uuid.uuid1()) cluster_rpc.command_stack.create_user.call_async(data) location = { 'Location': 'http://localhost/users/{}'.format(data['id']) } return 202, location, 'ACCEPTED' except Exception as e: return 500, e @http('GET', '/users/<int:page>/<int:limit>') def get_users(self, request, page, limit): response = self.query_rpc.get_all_users(page, limit) return 200, {'Content-Type': 'application/json'}, response @http('GET', '/users/<string:user_id>') def get_user(self, request, user_id): response = self.query_rpc.get_user(user_id) return 200, {'Content-Type': 'application/json'}, response
class OpenflowService: """ Openflow Service Microservice that translates the information sent by the api to commands applicable in Openflow devices Receive: this function receives a python dictionary, with at least the following information for each processing Return: - The microservice activates the application module via ssh and returns the result. If any incorrect information in the dictionary, the error message is returned Translations for NAT1toN and Route have not yet been implemented """ name = "openflow_translator" zipcode_rpc = RpcProxy('openflow_service_translator') @rpc def translate_intent(self, dict_intent): if 'intent_type' in dict_intent: output = check_values(dict_intent) if output is True: if dict_intent['intent_type'] == 'acl': return process_acl(dict_intent) elif dict_intent['intent_type'] == 'nat_1to1': return process_nat11(dict_intent) elif dict_intent['intent_type'] == 'traffic_shaping': return process_traffic_shaping(dict_intent) elif dict_intent['intent_type'] == 'dst_route': return process_dst_route(dict_intent) elif dict_intent['intent_type'] == 'nat_nto1': return process_natn1(dict_intent) elif dict_intent['intent_type'] == 'url_filter': return process_url_filter(dict_intent) else: return output else: return 'OPENFLOW TRANSLATOR: the key "intent_type" is unavailable in the dictionary'
class ConfluencePublish(object): name = "confluencepublish" mail = RpcProxy('mail') @rpc def confluencepublish(self): pass
class ExportToPDF(object): name = "exporttopdf" mail = RpcProxy('mail') @rpc def exporttopdf(self): pass
class publishFlowDock(object): name = "publishtoflowdock" mail = RpcProxy('mail') @rpc def exporttopdf(self): pass
class pagerduty(object): name = "pagerduty" mail = RpcProxy('mail') @rpc def pagerduty(self): pass