Last active
November 7, 2024 21:39
-
-
Save ivanaugustobd/47285ef9878925f134df849e13441a42 to your computer and use it in GitHub Desktop.
Magento 2 queue (background jobs) quick setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Usage: ./setup-queue.sh | |
# (at magento's root dir) | |
# | |
# This will update your app/etc/env.php with the config needed | |
# to process magento queued messages (background jobs) using mysql + cron, | |
# executing them alongside your regular bin/magento cron:run scheduling | |
# Backup the env.php file to app/etc/env.php.bkp | |
if [ ! -e app/etc/env.php.bkp ]; then | |
cp app/etc/env.php{,.bkp} | |
fi | |
# Ensure magerun2 is available | |
if [ ! -e bin/n98-magerun2 ]; then | |
echo 'Installing magerun2 to bin/n98-magerun2..' | |
curl https://files.magerun.net/n98-magerun2.phar --output bin/n98-magerun2 | |
chmod +x bin/n98-magerun2 | |
fi | |
# Delete existent queue config, if set | |
echo 'Deleting old queue config...' | |
bin/n98-magerun2 config:env:delete queue | |
bin/n98-magerun2 config:env:delete cron_consumers_runner | |
# Setup new queue config | |
echo 'Setting up new queue config...' | |
bin/n98-magerun2 config:env:set queue.consumers_wait_for_messages 0 | |
bin/n98-magerun2 config:env:set cron_consumers_runner.cron_run 1 | |
bin/n98-magerun2 config:env:set cron_consumers_runner.max_messages 100 | |
# Setup consumers | |
echo 'Setting up consumers...' | |
mapfile -t CONSUMERS < <(bin/magento queue:consumers:list | grep -v async.operations.all | sort) | |
for I in "${!CONSUMERS[@]}"; do | |
bin/n98-magerun2 config:env:set "cron_consumers_runner.consumers.$I" "${CONSUMERS[$I]}" | |
done | |
# Clean cache | |
echo 'Cleaning cache...' | |
bin/magento cache:clean | |
# Warm up the config cache | |
echo 'Warming up cache...' | |
bin/n98-magerun2 sys:check > /dev/null & | |
echo 'Done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DISCLAIMER for Mac users: I doubt this is compatible with macOS utterly deprecated bash, so I recommend running it inside your docker stack (Warden, Docker Magento, ECE Tools, etc) php container instead