Add scdf-python-app. fix polyglot-python-task issue
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import sys
|
||||
import threading
|
||||
|
||||
from flask import Flask, Response
|
||||
|
||||
|
||||
class Actuator:
|
||||
"""Actuator is used to expose operational information about the running application, such as `health/liveliness`,
|
||||
`info`, `env`, etc. It uses HTTP endpoints to enable us to interact with it.
|
||||
|
||||
The `/actuator/health` and `/actuator/info` handles the Kubernetes liveness and readiness probes requests.
|
||||
Kubernetes expects HTTP 200 status code to consider the application live and ready.
|
||||
|
||||
:param port: the HTTP port used by the Actuator.
|
||||
:param info_content: The text response to be returned by the /actuator/info endpoint.
|
||||
"""
|
||||
|
||||
def __init__(self, port=8080, info_content='Info'):
|
||||
self.http_app = Actuator.__create_http_app(info_content)
|
||||
self.port = port
|
||||
print(info_content)
|
||||
sys.stdout.flush()
|
||||
|
||||
def __run(self):
|
||||
self.http_app.run(port=self.port, host='0.0.0.0')
|
||||
|
||||
@staticmethod
|
||||
def __create_http_app(info_description):
|
||||
app = Flask(__name__)
|
||||
app.debug = False
|
||||
app.use_reloader = False
|
||||
|
||||
@app.route('/actuator/health')
|
||||
def health():
|
||||
return Response('Alive', status=200)
|
||||
|
||||
@app.route('/actuator/info')
|
||||
def info():
|
||||
return Response(info_description, status=200, content_type='text/plain')
|
||||
|
||||
return app
|
||||
|
||||
@staticmethod
|
||||
def start(port=8080, info='Info'):
|
||||
"""Starts the `Actuator` in a separate thread.
|
||||
|
||||
:param port: the HTTP port used by the Actuator. Defaults to 8080.
|
||||
:param info: The text response to be returned by the /actuator/info endpoint.
|
||||
"""
|
||||
try:
|
||||
thread = threading.Thread(target=Actuator(port, info).__run)
|
||||
thread.setDaemon(True)
|
||||
thread.start()
|
||||
print('Actuator started!')
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
@@ -0,0 +1,76 @@
|
||||
import os
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def get_cmd_arg(name):
|
||||
"""Extracts argument value by name. (@author: Chris Schaefer)
|
||||
|
||||
Assumes the exec (default) spring-cloud-deployer-k8s argument passing mode.
|
||||
|
||||
Args:
|
||||
name: argument name.
|
||||
Returns:
|
||||
value of the requested argument.
|
||||
"""
|
||||
d = defaultdict(list)
|
||||
for k, v in ((k.lstrip('-'), v) for k, v in (a.split('=') for a in sys.argv[1:])):
|
||||
d[k].append(v)
|
||||
|
||||
if bool(d[name]):
|
||||
return d[name][0]
|
||||
else:
|
||||
return ''
|
||||
|
||||
|
||||
def get_stream_app_label():
|
||||
return get_cmd_arg('spring.cloud.dataflow.stream.app.label')
|
||||
|
||||
|
||||
def get_stream_name():
|
||||
return get_cmd_arg('spring.cloud.dataflow.stream.name')
|
||||
|
||||
|
||||
def get_channel_topic(channel_name):
|
||||
"""
|
||||
For given channel name returns the message broker destinations (e.g. Kafka topics or RabbitMQ exchanges).
|
||||
|
||||
We adopt the Spring Cloud Stream using the following format:
|
||||
spring.cloud.stream.bindings.<channelName>.destination=<value>.
|
||||
The <channelName> represents the name of the channel being configured (for example, input or output).
|
||||
|
||||
:param channel_name: logical channel name as defined in the application.
|
||||
:return: The target destination of a channel on the bound middleware (for example, the RabbitMQ exchange or Kafka
|
||||
topic). If the channel is bound as a consumer, it could be bound to multiple destinations, and the
|
||||
destination names can be specified as comma-separated String values.
|
||||
"""
|
||||
return get_cmd_arg('spring.cloud.stream.bindings.{}.destination'.format(channel_name))
|
||||
|
||||
|
||||
def get_kafka_brokers():
|
||||
return os.getenv('SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKERS', '')
|
||||
|
||||
|
||||
def get_kafka_zk_nodes():
|
||||
return os.getenv('SPRING_CLOUD_STREAM_KAFKA_BINDER_ZK_NODES', '')
|
||||
|
||||
|
||||
def get_application_guid():
|
||||
return os.getenv('SPRING_CLOUD_APPLICATION_GUID', '')
|
||||
|
||||
|
||||
def get_application_group():
|
||||
return os.getenv('SPRING_CLOUD_APPLICATION_GROUP', '')
|
||||
|
||||
|
||||
def get_env_info():
|
||||
props = ' stream-name={}\n app-name={}\n app-guid={}\n app-group={}\n kafka-brokers={}\n ' \
|
||||
'kafka-zk={}\n'.format(get_stream_name(), get_stream_app_label(), get_application_guid(),
|
||||
get_application_group(), get_kafka_brokers(), get_kafka_zk_nodes())
|
||||
channels = ' Inputs:\n orders={}\n Outputs: \n hot.drink={}\n cold.drink={}\n'.format(
|
||||
get_channel_topic('orders'), get_channel_topic('hot.drink'), get_channel_topic('cold.drink'))
|
||||
args = '\n '.join(sys.argv)
|
||||
envs = ''
|
||||
# envs = '\n '.join(list(map(lambda k: '{}={}'.format(k, os.environ[k]), os.environ)))
|
||||
return 'Properties\n{0}\nChannels\n{1}\nArguments\n {2}\n\nEnvironment\n {3}'.format(
|
||||
props, channels, args, envs)
|
||||
Reference in New Issue
Block a user