kser.entry
— Entries¶
-
class
kser.entry.
Entrypoint
(uuid=None, params=None, result=None)¶ An entrypoint is the code which will be registred in the controller to handle execution.
param str uuid: An unique identifier. param dict params: Entrypoint parameters param cdumay_result.Result result: previous task result -
REQUIRED_FIELDS
¶ Tuple or list of keys required by the entrypoint.
-
check_required_params
()¶ Perform a self test. It can be used to check params received, states… By default, this method check the presence of each item stored in
kser.entry.Entrypoint.REQUIRED_FIELDS
in the kser.entry.Entrypoint.params dictionnary.
-
execute
(result=None)¶ The main method used to launch the entrypoint execution. This method is execption safe. To execute an entrypoint without catching execption uses
kser.entry.Entrypoint.unsafe_execute()
.Parameters: result (cdumay_result.Result) – Previous task result Returns: The execution result Return type: cdumay_result.Result
-
classmethod
from_Message
(kmsg)¶ Initialize the entrypoint from a
kser.schemas.Message
Parameters: kmsg (kser.schemas.Message) – A message received from Kafka. Returns: The entrypoint Return type: kser.entry.Entrypoint
-
log
(message, level=logging.INFO, *args, **kwargs)¶ Adds entrypoint information to the message and sends the result to logging.log.
Parameters: - message (str) – message content
- level (int) – Logging Level
- args (list) – Arguments which are merged into msg using the string formatting operator.
- kwargs (dict) – Keyword arguments.
-
onerror
(result)¶ Trigger call on execution error.
Parameters: result (cdumay_result.Result) – Current execution result that led to the error. Returns: Return back the result Return type: cdumay_result.Result
-
onsuccess
(result)¶ Trigger call on execution success.
Parameters: result (cdumay_result.Result) – Current execution result that led to the success. Returns: Return back the result Return type: cdumay_result.Result
-
postinit
()¶ Trigger call on execution post initialization.
Parameters: result (cdumay_result.Result) – Current execution result that led to the success. Returns: Return back the result Return type: cdumay_result.Result
-
postrun
(result)¶ Trigger call on execution post run. This trigger is called regardless of execution result.
Parameters: result (cdumay_result.Result) – Current execution result. Returns: Return back the result Return type: cdumay_result.Result
-
prerun
()¶ Trigger call before the execution.
-
run
()¶ The entrypoint body intended to be overwrite.
-
to_Message
(result=None)¶ Serialize an entrypoint into a
kser.schemas.Message
.Parameters: result (cdumay_result.Result) – Execution result. Returns: Return a message. Return type: kser.schemas.Message
-
unsafe_execute
(result=None)¶ Unlike
kser.entry.Entrypoint.execute()
this method launch the entrypoint execution without catching execption.Parameters: result (cdumay_result.Result) – Previous task result Returns: The execution result Return type: cdumay_result.Result
-
Example usage¶
Let’s define a basic entrypoint:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import logging
from kser.entry import Entrypoint
from cdumay_result import Result
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(message)s"
)
class Hello(Entrypoint):
REQUIRED_FIELDS = ['name']
def run(self):
return Result(
uuid=self.uuid, stdout="Hello {name} !".format_map(self.params)
)
|
Execution result:
>>> Hello(params=dict(name="Cedric")).execute()
2018-02-21 18:26:46,762 DEBUG Hello.PreRun: __main__.Hello[d455cba6-b329-4d2d-a4e5-1fc2a0ff2781]
2018-02-21 18:26:46,762 DEBUG Hello.Run: __main__.Hello[d455cba6-b329-4d2d-a4e5-1fc2a0ff2781]
2018-02-21 18:26:46,762 DEBUG Hello.PostRun: __main__.Hello[d455cba6-b329-4d2d-a4e5-1fc2a0ff2781]
2018-02-21 18:26:46,763 INFO Hello.Success: __main__.Hello[d455cba6-b329-4d2d-a4e5-1fc2a0ff2781]: Hello Cedric !
Has we can see there is a required parameter name. Let’s see what’s happen if we didn’t set it:
>>> Hello().execute()
2018-02-21 18:35:47,493 DEBUG Hello.PreRun: __main__.Hello[f581fb61-0de1-489c-a0df-2c03ce1d35b4]
2018-02-21 18:35:47,495 ERROR Hello.Failed: __main__.Hello[f581fb61-0de1-489c-a0df-2c03ce1d35b4]: Missing parameter: name
What’s happen if we uses kser.entry.Entrypoint.unsafe_execute()
instead of kser.entry.Entrypoint.execute()
:
>>> Hello().unsafe_execute()
2018-02-21 18:39:23,522 DEBUG Hello.PreRun: __main__.Hello[6aa38be5-cd82-441b-8853-318545a053ad]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/kser/src/kser/entry.py", line 220, in unsafe_execute
self._prerun()
File "/opt/kser/src/kser/entry.py", line 147, in _prerun
self.check_required_params()
File "/opt/kser/src/kser/entry.py", line 54, in check_required_params
raise ValidationError("Missing parameter: {}".format(param))
cdumay_rest_client.exceptions.ValidationError: Error 400: Missing parameter: name (extra={})
See also
- cdumay-result
- A basic lib to serialize exception results.
- cdumay-rest-client
- A basic REST client library.