Setting up the Events API¶
Configure the consumer worker¶
The command to launch the consumer is:
1$ /path/to/php /path/to/your/pim/bin/console messenger:consume webhook --env=prod
Logs¶
The consumer writes logs to the standard output.
It’s your responsibility to choose where to write the logs.
For example, to write in the file /tmp/consumer.log
:
1$ /path/to/php /path/to/your/pim/bin/console messenger:consume webhook --env=prod >/tmp/consumer.log 2>&1
Do note that you should ensure the log rotation as well.
Option #1 - Supervisor¶
It’s strongly recommended to use a Process Control System to launch the consumer in production. This is not useful in development though.
In this documentation, we will describe how to configure the Process Control System supervisor, to run the consumer. These instructions are valid for Debian 9 and Ubuntu 16.04.
Installing supervisor¶
Install supervisor:
1$ apt update
2$ apt install supervisor
For the other platforms, you can follow the install section of the official documentation.
Configuring supervisor¶
Create a file in the configuration directory of supervisor /etc/supervisor/conf.d
.
1[program:akeneo_events_api_consumer]
2command=/path/to/php /path/to/your/pim/bin/console messenger:consume webhook --env=prod
3autostart=false
4autorestart=true
5stderr_logfile=/var/log/akeneo_events_api_consumer.err.log
6stdout_logfile=/var/log/akeneo_events_api_consumer.out.log
7user=my_user
The user my_user
should be the same as the user to run PHP-FPM.
Then, bring the changes into effect:
1$ supervisorctl reread
2$ supervisorctl update
Launch the daemon¶
1$ supervisorctl start akeneo_events_api_consumer
Option #2 - systemd¶
If you prefer, you can use systemd
, which will also allow to have logs management and auto restart in case of failure.
Configuration files¶
Create /etc/systemd/system/akeneo_events_api_consumer@.service
:
1[Unit]
2Description=Akeneo PIM Events API consumer
3Requires=apache2.service
4
5[Service]
6User=akeneo
7Environment=APP_ENV=prod
8WorkingDirectory=/path/to/home/user/.systemd
9ExecStart=/path/to/akeneo/bin/console messenger:consume webhook
10Restart=always
11
12[Install]
13WantedBy=multi-user.target
Manage the services¶
1# use * if you want the operation to apply on all services.
2systemctl [start|stop|restart|status] akeneo_events_api_consumer
3# check the logs in real time
4journalctl --unit=akeneo_events_api_consumer -f
Manage services by non-root users¶
sytemctl
is not useable by non-privileged users, if you want to allow a user akeneo
:
1apt install sudo
2visudo
You can then type in the following lines, depending on what commands you want to allow.
1akeneo ALL=(root) NOPASSWD: /bin/systemctl start akeneo_events_api_consumer
2akeneo ALL=(root) NOPASSWD: /bin/systemctl stop akeneo_events_api_consumer
3akeneo ALL=(root) NOPASSWD: /bin/systemctl status akeneo_events_api_consumer
4akeneo ALL=(root) NOPASSWD: /bin/systemctl restart akeneo_events_api_consumer
5akeneo ALL=(root) NOPASSWD: /bin/systemctl reload akeneo_events_api_consumer
Configure the purge message command¶
When using the event api feature, a message is created and stored into the database each time an event is raised. If you are massive product creator or updater, you may drastically increase your database volume!
Fortunately, Akeneo PIM provides a simple command to purge this messenger table:
1 bin/console akeneo:messenger:doctrine:purge-messages <table-name> <queue-name>
2
3<table-name> and <queue-name> must match with the Doctrine transport configuration for Symfony messenger.
The default configuration looks like:
1 bin/console akeneo:messenger:doctrine:purge-messages messenger_messages default
Note
You can change <table-name> and <queue-name> in the messenger.yml
configuration file.
See https://symfony.com/doc/4.4/messenger.html#doctrine-transport for further information.
Tip
Don’t forget to add –env=prod to avoid memory leaks in dev environment (the default environment for commands)
You can also change the retention time of a message in the queue:
1bin/console akeneo:messenger:doctrine:purge-messages <table-name> <queue-name> --retention-time[=RETENTION-TIME]
For example, with the option –retention-time 3600, the command will remove all messages that are older than one hour (3600 seconds). By default, RETENTION-TIME is equal to 7200 seconds (two hours).
Scheduling the purge¶
To run a command periodically, you can use a cron:
First, you need to install it (example in debian/ubuntu based distributions):
1apt-get install cron
Then, you can edit your crontab:
1crontab -e
You can now add a new line at the end of the opened file:
10 */2 * * * /home/akeneo/pim/bin/console akeneo:messenger:doctrine:purge-messages messenger_messages default --env=prod
With this cron configuration a purge of the messages older than 2 hours, will be launched every two hours.
Found a typo or a hole in the documentation and feel like contributing?
Join us on Github!