Reference

Core

class tamarco.core.microservice.Microservice[source]

Main class of a microservice. This class is responsible for controlling the lifecycle of the microservice, also builds and provides the necessary elements that a resource needs to work.

The resources of a microservice should be declared in this class. The microservice automatically takes the ownership of all the declared resources.

async post_start()[source]

Post start stage of lifecycle. This method can be overwritten by the user to add some logic in the start.

async post_stop()[source]

Post stop stage of the lifecycle. This method can be overwritten by the user to add some logic to the shut down.

async pre_start()[source]

Pre start stage of lifecycle. This method can be overwritten by the user to add some logic in the start.

run()[source]

Run a microservice. It initializes the main event loop of asyncio, so this function only are going to end when the microservice ends its live cycle.

async start()[source]

Start stage of lifecycle. This method can be overwritten by the user to add some logic in the start.

async stop()[source]

Stop stage of the lifecycle. This method can be overwritten by the user to add some logic to the shut down. This method should close all the I/O operations opened by the resources.

async stop_gracefully()[source]

Stop the microservice gracefully. Shut down the microservice. If after 30 seconds the microservice is not closed gracefully it forces a exit.

class tamarco.core.microservice.MicroserviceContext[source]

“This class is used to use tamarco resources without using a full microservice, for example a script.

tamarco.core.microservice.task(name_or_fn)[source]

Decorator to convert a method of a microservice in a asyncio task. The task is started and stopped when the microservice starts and stops respectively.

Parameters

name_or_fn – Name of the task or function. If function the task name is the declared name of the function.

tamarco.core.microservice.task_timer(interval=1000, one_shot=False, autostart=False) → Union[collections.abc.Callable, Coroutine][source]

Decorator to declare a task that should repeated in time intervals.

Examples

>>> @task_timer()
>>> async def execute(*arg,**kwargs)
>>>     print('tick')
>>> @task_timer(interval=1000, oneshot=True, autostart=True)
>>> async def execute(*args,**kwargs)
>>>     print('tick')
Parameters
  • interval (int) – Interval in milliseconds when the task is repeated.

  • one_shot (bool) – Only runs the task once.

  • autostart (bool) – Task is automatically initialized with the microservice.

tamarco.core.microservice.thread(name_or_fn)[source]

Decorator to convert a method of a microservice in a thread. The thread is started and stopped when the microservice starts and stops respectively.

Parameters

name_or_fn – Name of the thread or function. If function the thread name is the declared name of the function.

Logging

class tamarco.core.logging.logging.Logging[source]

Class that handles the configuration of the standard logging of python using the microservice settings.

configure_settings(settings)[source]

Sets the settings object (a SettingsView(f”{ROOT_SETTINGS}.logging”)).

Parameters

settings (SettingsInterface) – Settings object that have the logging settings.

static describe_dynamic_settings()[source]

Describe all the class dynamic settings.

Returns

Settings and their description.

Return type

dict

static describe_static_settings()[source]

Describe all the settings as a dictionary keys and their values are a setting short description. These settings are the static settings needed by the class.

Returns

Settings and their description.

Return type

dict

async start(loggers, microservice_name, deploy_name, loop)[source]

Configure the standard python logging, adding handlers and loggers that uses that handlers.

Parameters
  • loggers (list) – Names of the loggers you want to configure.

  • microservice_name (str) – Name of the microservice that will use the logging.

  • deploy_name (str) – Deploy name.

  • loop – asyncio event loop.

Patterns

class tamarco.core.patterns.Singleton[source]

Singleton pattern implementation.

This pattern restricts the instantiation of a class to one object.

class tamarco.core.patterns.Proxy(obj)[source]

Proxy pattern to be used as a pointer. When the value of _obj changes, the reference to the proxy remains.

static make_method(name)[source]

Create a new method to getting the value of the attribute name.

Parameters

name (string) – Attribute name.

Returns

New __getattribute__ method to get a value from the _obj object.

Return type

function

class tamarco.core.patterns.Flyweight(name, bases, dct)[source]

Metaclass that implements the Flyweight pattern.

It is like a Singleton but only for the instances with the same key. The key is first parameter that you pass to the class when you create the object.

This class is conceived for the internal use of the Tamarco metrics library.

Example:

>>> class Metric(metaclass=Flyweight):
>>>     def __init__(self, metric_id):
>>>         self.metric_id = metric_id
>>>
>>> http_requests_1 = Metric('http_requests')
>>> http_requests_2 = Metric('http_requests')
>>>
>>> http_requests_1 == http_requests_2
True
class tamarco.core.patterns.FlyweightWithLabels(name, bases, dct)[source]

Metaclass that extends the pattern of the Flyweight pattern with labels.

This class is conceived for the internal use of the Tamarco metrics library.

Example:

>>> class Metric(metaclass=FlyweightWithLabels):
>>>     def __init__(self, metric_id, labels=None):
>>>         self.metric_id = metric_id
>>>         self.labels = labels if labels else {}
>>>
>>> requests_http_get_1 = Metric('request', labels={'protocol': 'http', 'method': 'get'})
>>> requests_http_post_1 = Metric('request', labels={'protocol': 'http', 'method': 'post'})
>>>
>>> requests_http_get_2 = Metric('request', labels={'protocol': 'http', 'method': 'get'})
>>> requests_http_post_2 = Metric('request', labels={'protocol': 'http', 'method': 'post'})
>>>
>>> requests_http_get_1 == requests_http_get_2
True
>>> requests_http_post_1 == requests_http_post_2
True

Settings

exception tamarco.core.settings.settings.SettingNotFound(key)[source]
class tamarco.core.settings.settings.Settings[source]

Core settings class, here is the unique True of settings all the settings values are cached by this class in his internal_backend, all of the other settings are views of the data that this class holds.

The external backend is where the settings should be originally loaded, the internal backend acts as cache to avoid making many requests to the external backend.

async bind(loop)[source]

Binds the settings to one event loop.

Parameters

loop – Main asyncio event loop.

async cancel_watch_tasks()[source]

Cancel all the pending watcher tasks of the settings in the etcd backend.

async delete(key)[source]

Delete a setting.

Parameters

key (str) – Path to the setting.

async get(key, default=<class 'tamarco.core.settings.backends.interface._EmptyArg'>)[source]

Get a setting value for a key.

Parameters
  • key (str) – Path to the setting.

  • default – Default value in the case that it doesn’t exists.

Raises

SettingNotFound – The setting can’t be resolved and it hasn’t default value.

Returns

Setting value.

async get_external(key, default=<class 'tamarco.core.settings.backends.interface._EmptyArg'>)[source]

Get the setting from the external backend updating the internal one with the value of the external.

Parameters
  • key (str) – Path to the setting.

  • default – Default value in case that the setting doesn’t exists in the external backend.

Returns

Setting value.

register_promised_setting(key, promised_setting)[source]

Register a SettingProxy to be resolved when the settings are loaded.

Parameters
  • key (str) – setting key to register.

  • promised_setting – setting proxy to register.

async set(key, value)[source]

Set a setting value.

Parameters
  • key (str) – Path to the setting.

  • value – Value to be set in the setting key.

async start()[source]

Start the settings. First loads the settings from the external settings backend (etcd or yaml file) once the internal and external settings backends are ready, the promised settings (when_loaded_settings) are resolved and the proxies start to holds the settings values.

async stop()[source]

Perform all the needed tasks in order to stop the Settings.

update_internal(dict_settings)[source]

Update the internal cache with new settings.

Parameters

dict_settings (dict) – Settings to add to the internal backend.

async update_internal_settings(key, value)[source]

Update an specific internal setting.

Parameters
  • key (str) – Path to the setting.

  • value – Setting value.

async watch(key, callback)[source]

Schedule a callback for when a setting is changed in the etcd backend.

Parameters
  • key (str) – Path to the setting.

  • callback – function or coroutine to be called when the setting changes, it should have with two input arguments, one for the setting path and other for the setting value.

async watch_and_update(key)[source]

Watch one specific settings and maintain it updated in the internal settings.

Parameters

key (str) – Path to the setting.

exception tamarco.core.settings.settings.SettingsNotLoadedYet[source]
class tamarco.core.settings.settings.SettingsView(settings, prefix, microservice_name=None)[source]

View/chroot/jail/box of main settings class. Used in the resources to provide them with their subset of settings.

async cancel_watch_tasks()[source]

Cancel all the pending watcher tasks of the settings in the etcd backend.

async delete(key, raw=False)[source]

Delete a setting.

Parameters
  • key (str) – Path to the setting.

  • raw – If True no prefix is used so is not a view.

async get(key, default=<class 'tamarco.core.settings.backends.interface._EmptyArg'>, raw=False)[source]

Get setting.

Parameters
  • key (str) – Path to the setting.

  • default – Default value in case that the setting doesn’t exists in the external backend.

  • raw – if True no prefix is used so is not a view.

async set(key, value, raw=False)[source]

Set a setting value.

Parameters
  • key (str) – Path to the setting.

  • default – Default value in the case that it doesn’t exists.

  • raw – If True no prefix is used so is not a view.

Returns

Setting value.

async update_internal_settings(key, value)[source]

Update internal settings.

Parameters
  • key (str) – Path to the setting.

  • value – Setting value.

async watch(key, callback, raw=False)[source]

Schedule a callback for when a setting is changed in the etcd backend.

Parameters
  • key (str) – Path to the setting.

  • callback – Callback to run whenever the key changes.

  • raw – If True no prefix is used so is not a view.

Resources

class tamarco.resources.bases.BaseResource[source]

Define the basic interface of a resource. All the tamarco resources should inherit from this class.

Resource start call chain:
  1. bind

  2. configure_settings

  3. pre_start

  4. start

  5. post_start

Resource stop call chain:
  1. stop

  2. post_stop

async bind(microservice, name)[source]

Build method, the microservice binds all its resources. Microservice starts and stops the resources.

Parameters
  • microservice (Microservice) – Microservice instance managing the resource.

  • name (str) – Name of the resource instance in the microservice class.

async configure_settings(settings)[source]

Build method, the microservice provides the settings class of each resource. The resource should read the settings via this object.

Parameters

settings (SettingsView) – Settings view of the resource.

async post_start()[source]

Post start stage of the resource lifecycle.

async post_stop()[source]

Post stop stage of the resource lifecycle.

async pre_start()[source]

Pre start stage of the resource lifecycle.

async start()[source]

Start stage of the resource lifecycle.

async status() → dict[source]

Return information about the state of the resource.

async stop()[source]

Stop stage of the resource lifecycle.

class tamarco.resources.bases.DatabaseResource(*args, **kwargs)[source]
async start(clean_database=False, register_scripts=True)[source]

Start stage of the resource lifecycle.

async status()[source]

Return information about the state of the resource.

async stop()[source]

Stop stage of the resource lifecycle.

class tamarco.resources.bases.IOResource(inputs: List = None, outputs: List = None)[source]

Extended resource that manages I/O streams, like Kafka and AMQP.

add_input(input_to_add)[source]

Add one input.

Parameters

input_to_add (InputBase) – Input to add.

add_output(output)[source]

Add one output.

Parameters

output (OutputBase) – Output to add.