Add the polyglot python-task sample

This commit is contained in:
Christian Tzolov
2019-05-07 09:47:19 +02:00
parent f51a6957ff
commit 108299b07e
7 changed files with 161 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
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)
return d[name][0]
def get_db_url():
"""Computes sqlalchemy connection URL
Uses the s.d.username, s.d.password and s.d.url properties to compute the sqlalchemy url.
This provides access to the SCDF internal DB and tables such as TASK_EXECUTION.
Returns:
sqlalchemy compatible URL compatible with the target DB.
"""
username = get_cmd_arg('spring.datasource.username')
password = get_cmd_arg('spring.datasource.password')
jdbc_url = get_cmd_arg('spring.datasource.url')
return str(jdbc_url) \
.replace('jdbc:', '') \
.replace('sqlserver:', 'mssql+pyodbc:') \
.replace('//', '//{username}:{password}@'.format(username=username, password=password))
def get_task_id():
"""Task ID as handled inside SCDF.
When launching tasks SCDF provides the spring.cloud.task.executionid as command line argument.
Returns:
The task id as handled inside SCDF.
"""
return get_cmd_arg('spring.cloud.task.executionid')
def get_task_name():
return get_cmd_arg('spring.cloud.task.name')

View File

@@ -0,0 +1,40 @@
from sqlalchemy import create_engine
from sqlalchemy.sql import text
import datetime
class TaskStatus:
"""Helper class to help manage Task's status in the SCDF DB. """
def __init__(self, task_id, jdbc_url):
self.task_id = task_id
self.engine = create_engine(jdbc_url)
self.connection = self.engine.connect()
def running(self):
"""Set the TASK_EXECUTION's START_TIME """
now = datetime.datetime.now()
start_task_statement = text(
"UPDATE TASK_EXECUTION SET START_TIME=:start_time, EXIT_CODE=null, LAST_UPDATED=:last_updated "
"WHERE TASK_EXECUTION_ID=:task_id")
self.connection.execute(start_task_statement, start_time=now, last_updated=now, task_id=self.task_id)
def completed(self):
"""Set the TASK_EXECUTION's END_TIME, EXIST_CODE=0 and EXIST_MESSAGE/ERROR_MESSAGE must be null """
now = datetime.datetime.now()
complete_task_statement = text(
"UPDATE TASK_EXECUTION SET END_TIME=:end_time, EXIT_CODE=0, EXIT_MESSAGE=null, ERROR_MESSAGE=null, "
"LAST_UPDATED=:last_updated WHERE TASK_EXECUTION_ID=:task_id")
self.connection.execute(complete_task_statement, end_time=now, last_updated=now, task_id=self.task_id)
def failed(self, exit_code, exit_message, error_message=''):
"""Set the TASK_EXECUTION's END_TIME, EXIST_CODE is the error code and EXIST_MESSAGE/ERROR_MESSAGE describe
the error """
now = datetime.datetime.now()
complete_task_statement = text(
"UPDATE TASK_EXECUTION SET END_TIME=:end_time, EXIT_CODE=:exit_code, EXIT_MESSAGE=:exit_message, "
" ERROR_MESSAGE=:error_message, LAST_UPDATED=:last_updated "
"WHERE TASK_EXECUTION_ID=:task_id")
self.connection.execute(complete_task_statement, end_time=now, exit_code=exit_code,
exit_message=exit_message, error_message=error_message, last_updated=now,
task_id=self.task_id)