Operation Wrappers

The purpose of this Symfony integration is to help you using operations and operation runners in an easy way. By using the AOP features provided by the JMSAopBundle you can wrap a Symfony service in an operation runner by simply using a tag or a YAML configuration.

Important

You need to first install JMSAopBundle in order to be able to use this AOP integration.

By default this feature is not activated so you need to activate it manually:

1
2
3
tolerance:
    aop:
        enabled: true

Using a tag

Let’s say now that you’ve a service for this YourService object that contains methods that are a bit risky and needs to be wrapped into an operation runner:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
namespace App;

class YourService
{
    public function getSomething()
    {
        // This method needs to be in an operation runner because it's
        // doing something risky such as an API call.
    }
}

Once you’ve that, you can use the tolerance.operation_wrapper tag to wrap the different calls to some of your service’s methods inside an operation runner.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="app.your_service" class="App\YourService">
            <tag name="tolerance.operation_wrapper"
                 methods="getSomething"
                 runner="tolerance.operation_runner.default" />
        </service>
    </services>
</container>

The tag have 2 configuration options:

  • methods: a comma separated names of the methods you want to proxy
  • runner: the service name of the operation runner to use

And that’s all, your calls to the method getSomething of your service will be wrapper inside a callback operation and run with the operation runner operation_runner.service_name.

Using YAML

You can wrap some methods of a given class into a given operation runner. The following example shows how simple it can be to simply get metrics from some API calls for instance.

All the calls to the methods requestSomething and findSomethingElse to a service with the class HttpThirdPartyClient will be proxied through the operation runner tolerance.operation_runners.3rdparty. This metrics operation runner created in YAML will record the success and failure of the operations to a metric publisher.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
tolerance:
aop:
    enabled: true

    wrappers:
        - class: "Acme\Your\HttpThirdPartyClient"
          methods:
              - requestSomething
              - findSomethingElse
          runner: tolerance.operation_runners.3rdparty

operation_runners:
    default:
        callback: ~

    3rdparty:
        success_failure_metrics:
            publisher: tolerance.metrics.publisher.statsd
            namespace: 3rdparty.outgoing.requests