Skip to content

Commit

Permalink
Merge pull request #2 from FluffyDiscord/#autoregister-cache
Browse files Browse the repository at this point in the history
Autoregister cache adapters using RoadRunner config
  • Loading branch information
FluffyDiscord authored Mar 13, 2024
2 parents 186e6a3 + 38a7818 commit 1ba1856
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/phpspec.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/roadrunner-bundle.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 51 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,33 @@ composer require fluffydiscord/roadrunner-symfony-bundle

## Usage

Define the environment variable `APP_RUNTIME` in `.rr.yaml`
Define the environment variable `APP_RUNTIME` in `.rr.yaml` and set up `rpc` plugin:

```
// .rr.yaml
`.rr.yaml`
```yaml
server:
...
env:
APP_RUNTIME: FluffyDiscord\RoadRunnerBundle\Runtime\Runtime

rpc:
listen: tcp://127.0.0.1:6001
```
Don't forget to add the `RR_RPC` to your `.env`:

```dotenv
RR_RPC=tcp://127.0.0.1:6001
```

## Configuration
fluffy_discord_road_runner.yaml

`fluffy_discord_road_runner.yaml`
```yaml
fluffy_discord_road_runner:
# https://docs.roadrunner.dev/http/http
http:
# Optional
# -----------
# This decides when to boot the Symfony kernel.
#
# false (default) - before first request (worker takes some time to be ready, but app has consistent response times)
Expand All @@ -34,10 +46,38 @@ fluffy_discord_road_runner:
# take a lot of time or just boot up using only a few "emergency" workers
# and then use dynamic worker scaling as described here https://docs.roadrunner.dev/php-worker/scaling
lazy_boot: false

# https://docs.roadrunner.dev/plugins/centrifuge
centrifugo:
# Optional
# -----------
# See http section
lazy_boot: false
# https://docs.roadrunner.dev/key-value/overview-kv
kv:
# Optional
# -----------
# If true (default), bundle will automatically register all "kv" adapters in your .rr.yaml.
# Registered services have alias "cache.adapter.rr_kv.NAME"
auto_register: true
# Optional
# -----------
# Which serializer should be used.
# By default, "IgbinarySerializer" will be used if "igbinary" php extension
# is installed (recommended), otherwise "DefaultSerializer".
# You are free to create your own serializer, it needs to implement
# Spiral\RoadRunner\KeyValue\Serializer\SerializerInterface
serializer: null
# Optional
# -----------
# Specify relative path from "kernel.project_dir" to a keypair file
# for end-to-end encryption. "sodium" php extension is required.
# https://docs.roadrunner.dev/key-value/overview-kv#end-to-end-value-encryption
keypair_path: bin/keypair.key
```


Expand Down Expand Up @@ -75,7 +115,11 @@ class MyController

## Sentry

Built in support for [Sentry](https://packagist.org/packages/sentry/sentry-symfony). Just install it as you normally do.
Built in support for [Sentry](https://packagist.org/packages/sentry/sentry-symfony). Just install & configure it as you normally do.

```shell
composer require sentry/sentry-symfony
```

## Centrifugo (websockets)

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"sentry/sentry-symfony": "^4.13",
"phpunit/phpunit": "^10.5",
"symfony/mime": "^7.0",
"roadrunner-php/centrifugo": "^2.0"
"roadrunner-php/centrifugo": "^2.0",
"spiral/roadrunner-kv": "^4.0"
}
}
140 changes: 138 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/Cache/KVCacheAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace FluffyDiscord\RoadRunnerBundle\Cache;

use FluffyDiscord\RoadRunnerBundle\Exception\SodiumKeypairException;
use FluffyDiscord\RoadRunnerBundle\Exception\SodiumNotEnabledException;
use Spiral\Goridge\RPC\RPCInterface;
use Spiral\RoadRunner\KeyValue\Factory;
use Spiral\RoadRunner\KeyValue\Serializer\DefaultSerializer;
use Spiral\RoadRunner\KeyValue\Serializer\IgbinarySerializer;
use Spiral\RoadRunner\KeyValue\Serializer\SodiumSerializer;
use Symfony\Component\Cache\Adapter\Psr16Adapter;

class KVCacheAdapter extends Psr16Adapter
{
public static function create(
string $namespace,
RPCInterface $rpc,
string $name,
string $projectDir,
?string $serializerClass,
?string $keypairPath,
): self
{
$serializer = null;
if ($serializerClass === null && function_exists("igbinary_serialize")) {
$serializer = new IgbinarySerializer();
}

if ($serializerClass !== null) {
if (!class_exists($serializerClass)) {
throw new \LogicException(sprintf('Serializer class "%s" does not exist', $serializerClass));
}

$serializer = new $serializerClass();
}

$factory = new Factory(
$rpc,
$serializer ?? new DefaultSerializer(),
);

if ($keypairPath !== null) {
$keypairPath = "{$projectDir}/{$keypairPath}";
if (!file_exists($keypairPath)) {
throw new SodiumKeypairException(sprintf('Unable to find keypair at: %s', $keypairPath));
}

$keypairContent = @file_get_contents($keypairPath);
if ($keypairContent === false) {
throw new SodiumKeypairException("Unable to read keypair content. Check it's permissions.");
}

try {
$sodiumSerializer = new SodiumSerializer(
$factory->getSerializer(),
$keypairContent,
);
} catch (\LogicException $logicException) {
throw new SodiumNotEnabledException($logicException->getMessage(), previous: $logicException);
}

$factory = $factory->withSerializer($sodiumSerializer);
}

return new self($factory->select($name), $namespace);
}
}
7 changes: 7 additions & 0 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public function getConfigTreeBuilder(): TreeBuilder

$builder->getRootNode()
->children()
->arrayNode("kv")
->children()
->booleanNode("auto_register")->defaultTrue()->end()
->scalarNode("serializer")->defaultNull()->end()
->scalarNode("keypair_path")->defaultNull()->end()
->end()
->end()
->arrayNode("http")
->children()
->booleanNode("lazy_boot")->defaultFalse()->end()
Expand Down
Loading

0 comments on commit 1ba1856

Please sign in to comment.