Git Product home page Git Product logo

event-machine-skeleton's Introduction

event-machine-skeleton

Dockerized skeleton for prooph software Event Machine

Installation

Please make sure you have installed Docker and Docker Compose.

$ docker run --rm -it -v $(pwd):/app prooph/composer:7.1 create-project proophsoftware/event-machine-skeleton <your_project_name>
$ cd <your_project_name>
$ sudo chown $(id -u -n):$(id -g -n) . -R
$ docker-compose up -d
$ docker-compose run php php scripts/create_event_stream.php

Tutorial

https://proophsoftware.github.io/event-machine/tutorial/

Demo

We've prepared a demo branch that contains a small service called BuildingMgmt. It will show you the basics of event machine and the skeleton structure. To run the demo you have to clone the skeleton instead of composer create-project so that your local copy is still connected to the github repo.

*Note: Event Machine is very flexible in the way how you organize your code. The skeleton just gives an example of a possible structure. The default way is to use static aggregate methods as pure functions. However, it is also possible to use stateful OOP aggregates. Take a look at the tutorial for more information.

$ git clone https://github.com/proophsoftware/event-machine-skeleton.git prooph-building-mgmt
$ cd prooph-building-mgmt
$ git checkout demo
$ docker run --rm -it -v $(pwd):/app prooph/composer:7.1 install
$ docker-compose up -d
$ docker-compose run php php scripts/create_event_stream.php

Head over to http://localhost:8080 to check if the containers are up and running. You should see a "It works" message.

Database

The skeleton uses a single Postgres database for both write and read model. The write model is event sourced and writes all events to prooph/event-store. The read model is created by projections (see src/Api/Projection) and is also stored in the Postgres DB. Read model tables have the prefix em_ds_ and end with a version number which is by default 0_1_0.

You can connect to the Postgres DB using following credentials (listed also in app.env):

PDO_DSN=pgsql:host=postgres port=5432 dbname=event_machine
PDO_USER=postgres
PDO_PWD=

Note: The DB runs insight a docker container. Use localhost as host name if you want to connect from your host system!

RabbitMQ

The skeleton uses RabbitMQ as a message broker with a preconfigured exchange called ui-exchange and a corresponding queue called ui-queue. You can open the Rabbit Mgmt UI in the browser: http://localhost:8081 and login with user: prooph and password: prooph.

The skeleton also contains a demo JS client which connects to a websocket and consumes messages from the ui-queue. Open http://localhost:8080/ws.html in your browser and forward events on the queue with $eventMachine->on(Event::MY_EVENT, UiExchange::class). Check src/Api/Listener for an example.

Unit and Integration Tests

We've prepared a BaseTestCase located in tests. Extend your test cases from that class to get access to some very useful test helpers. Check the tutorial for a detailed explanation.

You can run the tests using docker:

docker-compose run php php vendor/bin/phpunit

Troubleshooting

With the command docker-compose ps you can list the running containers. This should look like the following list:

                    Name                                   Command               State                             Ports                           
---------------------------------------------------------------------------------------------------------------------------------------------------
proophbuildingmgmt_event_machine_projection_1   docker-php-entrypoint php  ...   Up                                                                
proophbuildingmgmt_nginx_1                      nginx -g daemon off;             Up      0.0.0.0:443->443/tcp, 0.0.0.0:8080->80/tcp                
proophbuildingmgmt_php_1                        docker-php-entrypoint php-fpm    Up      9000/tcp                                                  
proophbuildingmgmt_postgres_1                   docker-entrypoint.sh postgres    Up      0.0.0.0:5432->5432/tcp                                    
proophbuildingmgmt_rabbit_1                     docker-entrypoint.sh rabbi ...   Up      0.0.0.0:8081->15671/tcp, 15672/tcp,                       
                                                                                         0.0.0.0:15691->15691/tcp, 25672/tcp, 4369/tcp, 5671/tcp,  
                                                                                         5672/tcp 

Make sure that all required ports are available on your machine. If not you can modify port mapping in the docker-compose.yml.

Have you tried turning it off and on again?

If something does not work as expected try to restart the containers first:

$ docker-compose down
$ docker-compose up -d

Projection reset

Event machine uses a single projection process (read more about prooph projections in the prooph docs). You can register your own projections in event machine which are all handled by the one background process that is started automatically with the script bin/event_machine_projection.php. Also see docker-compose.yml. The projection process runs in its own docker container which is restarted by docker in case of a failure. The projection process dies from time to time to catch up with your latest code changes.

If you recognize that your read models are not up-to-date or you need to reset the read model you can use this command:

$ docker-compose run php php bin/reset.php

If you still have trouble try a step by step approach:

$ docker-compose stop event_machine_projection
$ docker-compose run php php bin/reset.php
$ docker-compose up -d

You can also check the projection log with:

$ docker-compose logs -f event_machine_projection

Swagger UI is not updated

When you add new commands or queries in event machine the Swagger UI will not automatically reread the schema from the backend. Simply reload the UI or press Explore button.

Batteries Included

You know the headline from Docker, right? The Event Machine skeleton follows the same principle. It ships with a default set up so that you can start without messing around with configuration and such. The default set up is likely not what you want to use in production. The skeleton can be and should be adapted.

Focus of the skeleton is to provide an easy to use development environment, hence it uses default settings of Postgres and RabbitMQ containers. Make sure to secure the containers before you deploy them anywhere! You should build and use your own docker containers in production anyway. And if you cannot or don't want to use Docker then provide the needed infrastructure the way you prefer and just point event machine to it by adjusting configuration.

Powered by prooph software

prooph software

Event Machine is maintained by the prooph software team. The source code of Event Machine is open sourced along with an API documentation and a getting started demo. Prooph software offers commercial support and workshops for Event Machine as well as for the prooph components.

If you are interested in this offer or need project support please get in touch.

event-machine-skeleton's People

Contributors

camuthig avatar codeliner avatar martin-schilling avatar sandrokeil avatar shochdoerfer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

event-machine-skeleton's Issues

Hot reload of projections

Find a way to restart projection containers (or php process insight container) on code changes, so that latest changes are also available within the projections.

Default projection of aggregate state

Provide a default projection that is triggered each time an event occurs.
The projection takes _aggregate_type and _aggregate_id from event metadata and uses the new EventMachine::loadAggregateState method to get current state of related aggregate.
State is json_encoded (add hint in README that state should be json_encodeable) by the projection.
Aggregate type is used for mongo collection name (filter not allowed chars) and aggregateId is of course used as document id. The projection can simply perform an upsert.

Class 'AMQPConnection' not found

Hi,

thanks for this wonderfull guide. I head up with this exception while following it.

I did check image used in docker-compose prooph/php:7.2-fpm and it seems that it should be installed, still when i go inside the container and try to check the extension with php -i | grep amqp i get nothing.

Any idea on what is happening would be appreciated?

thanks in advance for your time,
David

event_machine_projection keeps failing: call to a member function run() on null

@codeliner was trying to create prototype suggested here: proophsoftware/es-emergency-call#8

But I'm having issues running the docker image; on particular, the event_machine_projection service keeps failing to start. The logs show the following:

event_machine_projection_1  | Fatal error: Uncaught Error: Call to a member function run() on null in /app/vendor/proophsoftware/event-machine/src/Projecting/ProjectionRunner.php:93
event_machine_projection_1  | Stack trace:
event_machine_projection_1  | #0 /app/vendor/proophsoftware/event-machine/src/EventMachine.php(617): Prooph\EventMachine\Projecting\ProjectionRunner->run(false, NULL)
event_machine_projection_1  | #1 /app/bin/event_machine_projection.php(21): Prooph\EventMachine\EventMachine->runProjections(false)
event_machine_projection_1  | #2 {main}
event_machine_projection_1  |   thrown in /app/vendor/proophsoftware/event-machine/src/Projecting/ProjectionRunner.php on line 93

I've checked the code, and seems like EventMachine.php is trying to create a projection runner, but the projection manager in the service container is null.

$this->container->get(self::SERVICE_ID_PROJECTION_MANAGER)

Any ideas why this could be happening? Have you heard this issue before?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.