Worker Configuration Guide

Workers are an essential feature for improving the efficiency and reliability of the NextGenAiAssistant module. Instead of relying on frequent cron job executions, workers keep scripts running continuously in the background. This ensures real-time processing of tasks, minimizes delays, and optimizes the handling of high-volume operations.


Why Use Workers?

  • Enhanced Performance: Continuous execution ensures tasks are processed in real-time without waiting for the next scheduled cron job.
  • Scalability: Ideal for stores handling large volumes of data or running frequent AI-related operations.
  • Reliability: Reduces the risk of missing tasks, especially during peak workloads.
  • Recommended Setup: Workers are the recommended method for running the module efficiently, especially when processing a large number of operations.

Setting Up a Worker

You can configure a worker for the NextGenAiAssistant module using different approaches. Below, we provide step-by-step guides for Supervisor, systemd, and Screen. We also show how to consolidate multiple Supervisor worker definitions into a single file for simplicity.


1. Setting Up Workers with Supervisor (Recommended)

Supervisor is a process management tool that ensures your worker scripts run continuously and restart automatically if they fail.

Step 1: Install Supervisor

Run the following command to install Supervisor on your server:

sudo apt-get install supervisor

Step 2: Create a Configuration File

You can define all workers in a single configuration file for simplicity and better organization. This approach keeps all worker settings in one place, making it easier to manage and maintain.

An example Supervisor configuration file is included with the module and can be found in the directory: examples/supervisor/conf.d/ai_assistant_workers.conf. This file provides a ready-to-use setup for managing the workers.

Note: Make sure to update the file paths in the configuration to match the location of your PrestaShop installation.

sudo nano /etc/supervisor/conf.d/ai_assistant_workers.conf

Step 3: Add Multiple Worker Definitions

Here’s how to define all three workers in one configuration file:

[program:ai_assistant_worker_create_threads]
command=php /path/to/prestashop/bin/console ai-assistant:openai:create-new-threads --worker
autostart=true
autorestart=true
stderr_logfile=/var/log/ai_assistant_worker_create_threads.err.log
stdout_logfile=/var/log/ai_assistant_worker_create_threads.out.log

[program:ai_assistant_worker_process_queue]
command=php /path/to/prestashop/bin/console ai-assistant:process-queue --worker
autostart=true
autorestart=true stderr_logfile=/var/log/ai_assistant_worker_process_queue.err.log stdout_logfile=/var/log/ai_assistant_worker_process_queue.out.log

[program:ai_assistant_worker_process_messages]
command=php /path/to/prestashop/bin/console ai-assistant:openai:process-thread-messages --worker
autostart=true
autorestart=true stderr_logfile=/var/log/ai_assistant_worker_process_messages.err.log stdout_logfile=/var/log/ai_assistant_worker_process_messages.out.log

Step 4: Apply the Configuration

Apply the changes and start all workers with a single command:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start all

Step 5: Monitor Worker Logs

Check the logs to verify the workers are running smoothly:

tail -f /var/log/ai_assistant_worker_create_threads.out.log
tail -f /var/log/ai_assistant_worker_process_queue.out.log
tail -f /var/log/ai_assistant_worker_process_messages.out.log

2. Setting Up Workers with systemd

If you prefer to use systemd instead of Supervisor, you can create separate service files for each worker. While this method doesn’t support consolidating definitions into one file like Supervisor does, it’s still a robust solution.

Step 1: Create Service Files

Create a systemd service file for each worker. Example for Create Threads:

sudo nano /etc/systemd/system/ai_assistant_worker_create_threads.service

Add the following content:

[Unit]
Description=AI Assistant Worker - Create Threads
After=network.target

[Service]
ExecStart=/usr/bin/php /path/to/prestashop/bin/console ai-assistant:openai:create-new-threads --worker
Restart=always
User=www-data
Group=www-data
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=ai_assistant_worker_create_threads

[Install]
WantedBy=multi-user.target

Repeat this for the other workers.

Step 2: Enable and Start the Workers

sudo systemctl enable ai_assistant_worker_create_threads
sudo systemctl start ai_assistant_worker_create_threads

sudo systemctl enable ai_assistant_worker_process_queue
sudo systemctl start ai_assistant_worker_process_queue

sudo systemctl enable ai_assistant_worker_process_messages
sudo systemctl start ai_assistant_worker_process_messages

3. Setting Up Workers with Screen

If you prefer a simpler setup without installing additional tools, you can use the screen command to keep the workers running in the background.

Step 1: Install Screen

Ensure screen is installed:

sudo apt-get install screen

Step 2: Start a New Screen Session

screen -S ai_assistant_worker_create_threads
php /path/to/prestashop/bin/console ai-assistant:openai:create-new-threads --worker

Repeat the same for other workers:

screen -S ai_assistant_worker_process_queue
php /path/to/prestashop/bin/console ai-assistant:process-queue --worker
screen -S ai_assistant_worker_process_messages
php /path/to/prestashop/bin/console ai-assistant:openai:process-thread-messages --worker

Step 3: Detach the Session

Press Ctrl+A, then D to detach the session and leave the workers running.

Step 4: Reattach the Session

To return to a specific session:

screen -r ai_assistant_worker_create_threads

Conclusion

Using workers significantly enhances the performance of the NextGenAiAssistant module. Consolidating worker definitions in a single Supervisor file simplifies configuration management, while alternatives like systemd and Screen provide flexibility for different server setups.

For additional support, refer to our documentation or contact our support team.

How can we help?