php-runtime/runtime

[Bref] What's the correct configuration for Console app?

vhenzl opened this issue · 3 comments

I'm trying to migrate my Symfony app with Bref to use Runtime, and I can't get the Lambda function with Symfony Console to work correctly. I also get the same behaviour for Nyholm/bref-runtime-demo.

By "work correctly", I mean that if I execute a command with vendor/bin/bref cli, I can't see any output in my console. The command seems to be executed correctly, its output is logged in CloudWatch, but I can't see the output on my machine.

The readme in this project implicates that console function should keep using bref:layer.console layer:

# pluggins and other options omitted
provider:
  environment:
    APP_RUNTIME: Runtime\Bref\Runtime

functions:
  console:
    handler: bin/console
    layers:
      - ${bref:layer.php-80}
      - ${bref:layer.console}

The demo project suggests that console functions should use bref-extra:symfony-runtime-php-80 layer:

# pluggins and other options omitted
provider:
  environment:
    APP_RUNTIME: Runtime\Bref\Runtime

functions:
  console:
    handler: bin/console
    layers:
      - ${bref:layer.php-80}
      - ${bref-extra:symfony-runtime-php-80}

Either way, the function seems to be executed correctly, but there is no local output.

I was able to display the function's output locally with the following config by forcing it to use GenericRuntime.

# pluggins and other options omitted
provider:
  environment:
    APP_RUNTIME: Runtime\Bref\Runtime

functions:
  console:
    environment:
      APP_RUNTIME: Symfony\Component\Runtime\GenericRuntime
    handler: bin/console
    layers:
      - ${bref:layer.php-80}
      - ${bref:layer.console}

With this setup, the function is executed correctly, its output is logged to CloudWatch, and I can see the output locally in my console. In other words, it works exactly in the same way as the "old" bin/console without Runtime did. But I'm not sure this is the correct approach.

It's probably fine for a console function to write only to stderr if it's a cron, but for functions executed remotely with vendor/bin/bref cli (e.g. DB migrations), their output should be transferred back to the client.

Am I missing something?

The 2nd example is correct. Maybe the readme should be improved.

You have indeed found a bug! Look here:
https://github.com/php-runtime/bref/blob/main/src/ConsoleApplicationRunner.php#L37

We run the command and store the output in a buffer. Then we fetch() the buffer to echo out to CouldWatch. However, when we run fetch() we also clear the buffer. So later when we get the result for your console, the buffer is empty.

Could you submit a PR to fix this issue?

Thanks!

Sure, I can have a look.

Thank you for your PRs.

I've released 0.1.1: https://github.com/php-runtime/bref/releases/tag/0.1.1