Package: camunda-external-task-client-python3
camunda-external-task-client-python3This repository contains Camunda External Task Client written in Python3.Implement yourBPMN Service Taskin Python3.Python >= 3.7 is requiredInstallingAdd following line torequirements.txtof your Python project.git+https://github.com/trustfactors/camunda-external-task-client-python3.git/#egg=camunda-external-task-client-python3Or use pip to install as shown below:pip install camunda-external-task-client-python3Running Camunda with DockerTo run the examples provided inexamplesfolder, you need to have Camunda running locally or somewhere.To run Camunda locally with Postgres DB as backend, you can usedocker-compose.ymlfile.$> docker-compose -f docker-compose.yml upAuth Basic ExamplesTo run the examples with Auth Basic provided inexamples/examples_auth_basicfolder, you need to have Camunda with AuthBasic, running locally or somewhere.To run Camunda with AuthBasic locally with Postgres DB as backend, you can usedocker-compose-auth.ymlfile.$> docker-compose -f docker-compose-auth.yml upUsageMake sure to haveCamundarunning.Create a simple process model with an External Service Task and define the topic as 'topicName'.Deploy the process to the Camunda BPM engine.In your Python code:importtimefromcamunda.external_task.external_taskimportExternalTask,TaskResultfromcamunda.external_task.external_task_workerimportExternalTaskWorker# configuration for the Clientdefault_config={"maxTasks":1,"lockDuration":10000,"asyncResponseTimeout":5000,"retries":3,"retryTimeout":5000,"sleepSeconds":30}defhandle_task(task:ExternalTask)->TaskResult:"""This task handler you need to implement with your business logic.After completion of business logic call either task.complete() or task.failure() or task.bpmn_error()to report status of task to Camunda"""# add your business logic here# ...# mark task either complete/failure/bpmnError based on outcome of your business logicfailure,bpmn_error=random_true(),random_true()# this code simulate random failureiffailure:# this marks task as failed in Camundareturntask.failure(error_message="task failed",error_details="failed task details",max_retries=3,retry_timeout=5000)elifbpmn_error:returntask.bpmn_error(error_code="BPMN_ERROR_CODE",error_message="BPMN Error occurred",variables={"var1":"value1","success":False})# pass any output variables you may want to send to Camunda as dictionary to complete()returntask.complete({"var1":1,"var2":"value"})defrandom_true():current_milli_time=int(round(time.time()*1000))returncurrent_milli_time%2==0if__name__=='__main__':ExternalTaskWorker(worker_id="1",config=default_config).subscribe("topicName",handle_task)About External TasksExternal Tasks are service tasks whose execution differs particularly from the execution of other service tasks (e.g. Human Tasks).
The execution works in a way that units of work are polled from the engine before being completed.camunda-external-task-client-pythonallows you to create easily such client in Python3.FeaturesStart processCamunda provides functionality to start a process instance for a given process definition.To start a process instance, we can usestart_process()fromengine_client.pyYou can find a usage examplehere.client=EngineClient()resp_json=client.start_process(process_key="PARALLEL_STEPS_EXAMPLE",variables={"intVar":"1","strVar":"hello"},tenant_id="6172cdf0-7b32-4460-9da0-ded5107aa977",business_key=str(uuid.uuid1()))Fetch and LockExternalTaskWorker(worker_id="1").subscribe("topicName", handle_task)starts long polling of the Camunda engine for external tasks.Polling tasks from the engine works by performing a fetch & lock operation of tasks that have subscriptions. It then calls the handler function passed tosubscribe()function. i.e.handle_taskin above example.Long Polling is done periodically based on theasyncResponseTimeoutconfiguration. Read more aboutLong Polling.Completefromcamunda.external_task.external_taskimportExternalTask,TaskResultfromcamunda.external_task.external_task_workerimportExternalTaskWorkerdefhandle_task(task:ExternalTask)->TaskResult:# add your business logic here# Complete the task# pass any output variables you may want to send to Camunda as dictionary to complete()returntask.complete({"var1":1,"var2":"value"})ExternalTaskWorker(worker_id="1").subscribe("topicName",handle_task)Handle Failurefromcamunda.external_task.external_taskimportExternalTask,TaskResultfromcamunda.external_task.external_task_workerimportExternalTaskWorkerdefhandle_task(task:ExternalTask)->TaskResult:# add your business logic here# Handle task Failurereturntask.failure(error_message="task failed",error_details="failed task details",max_retries=3,retry_timeout=5000)# This client/worker uses max_retries if no retries are previously set in the task# if retries are previously set then it just decrements that count by one before reporting failure to Camunda# when retries are zero, Camunda creates an incident which then manually needs to be looked into on Camunda CockpitExternalTaskWorker(worker_id="1").subscribe("topicName",handle_task)Handle BPMN Errorfromcamunda.external_task.external_taskimportExternalTask,TaskResultfromcamunda.external_task.external_task_workerimportExternalTaskWorkerdefhandle_task(task:ExternalTask)->TaskResult:# add your business logic here# Handle a BPMN Failurereturntask.bpmn_error(error_code="BPMN_ERROR",error_message="BPMN error occurred")ExternalTaskWorker(worker_id="1").subscribe("topicName",handle_task)Access Process Variablesfromcamunda.external_task.external_taskimportExternalTask,TaskResultfromcamunda.external_task.external_task_workerimportExternalTaskWorkerdefhandle_task(task:ExternalTask)->TaskResult:# add your business logic here# get the process variable 'score'score=task.get_variable("score")ifint(score)>=100:returntask.complete(...)else:returntask.failure(...)ExternalTaskWorker().subscribe("topicName",handle_task)Correlate messageCamunda provides functionality to send a message event to a running process instance.You can read more about the message events here:https://docs.camunda.org/manual/7.13/reference/bpmn20/events/message-events/In our to send a message event to a process instance, a new function calledcorrelate_message()is added toengine_client.pyWe can correlate the message by:process_instance_idtenant_idbusiness_keyprocess_variablesYou can find a usage examplehere.client=EngineClient()resp_json=client.correlate_message("CANCEL_MESSAGE",business_key="b4a6f392-12ab-11eb-80ef-acde48001122")AuthBasic UsageTo create an EngineClient with AuthBasic simpleclient=EngineClient(config={"auth_basic":{"username":"demo","password":"demo"}})resp_json=client.start_process(process_key="PARALLEL_STEPS_EXAMPLE",variables={"intVar":"1","strVar":"hello"},tenant_id="6172cdf0-7b32-4460-9da0-ded5107aa977",business_key=str(uuid.uuid1()))To create an ExternalTaskWorker with AuthBasic simplefromcamunda.external_task.external_taskimportExternalTask,TaskResultfromcamunda.external_task.external_task_workerimportExternalTaskWorkerconfig={"auth_basic":{"username":"demo","password":"demo"}}defhandle_task(task:ExternalTask)->TaskResult:# add your business logic here# Complete the task# pass any output variables you may want to send to Camunda as dictionary to complete()returntask.complete({"var1":1,"var2":"value"})ExternalTaskWorker(worker_id="1",config=config).subscribe("topicName",handle_task)LicenseThe source files in this repository are made available under theApache License Version 2.0.