Anyone have a good method for notifying someone if segments stop building?

Your software
My Mautic version is: 4.X
My PHP version is: 7.4
My Database type and version is: N/A

We have been experiencing issues with segment filters losing values after minor point upgrades. This stops the cron job in its tracks and only some of our segments get updated.

What are you doing to proactively notify someone when this (or other Mautic errors) happen? Is there some magic you can add to a cron command to notify an admin when it errors?

I love that out of date segments get a red exclamation mark now, but I’d rather not have to wait until a member of our team logs in and happens to navigate to the segment tab to know that some of our segments aren’t updating.

Hi Steve,
basically we take all cron job output, filter all “known good”, e.g.

<console command> |egrep -v  "^$|contact\(s\)|Triggering scheduled events|Triggering events|total event|<further known good>" 

and send the rest to some monitored email address.
Add in crontab, above your actual jobs:

MAILTO=<desired email address>

If you have logfile monitoring you may as well send output to syslog through

`|tr '\n' ' ' |logger -t mautic_cron`

Trouble is that our commands do not have nice structured return codes, properly use STDERR or other things (that I know of)…

Greets
Ekke

2 Likes

I guess we could notify admins like we do if Webhooks start to fail?

Hey Steve

You could do it with a bash script like this:

#!/bin/bash
set -xu

MAUTIC_DOCROOT=$(realpath $(pwd)/../public_html/)
PHP_BIN="/usr/bin/php"
MAUTIC_CLI="$PHP_BIN $MAUTIC_DOCROOT/bin/console"

# change to mautic docroot
cd $MAUTIC_DOCROOT

# get db credentials
HOST=$(grep db_host app/config/local.php |cut -d"'" -f4)
DATABASE=$(grep db_name app/config/local.php |cut -d"'" -f4)
USER=$(grep db_user app/config/local.php |cut -d"'" -f4)
PASSWORD=$(grep db_password app/config/local.php |cut -d"'" -f4)

function trap_error {

   exit_code=$?
    if [ $exit_code -ne 0 ]; then
        echo "Error during segment command ... "
        
        # HERE YOU CAN CALL A COMMAND TO NOTIFY YOU 
        # dshofdhf
    fi


}
trap trap_error EXIT

while read -a row
do   

   # call the segment rebuild per id and timeout after 5,5 hours 
   /usr/bin/timeout --preserve-status 20000  $MAUTIC_CLI mautic:segments:update --batch-limit=200 --list-id=${row} -q -n

done < <(echo 'select id from lead_lists where is_published=1' | mysql --host=${HOST} --user=${USER} --password=${PASSWORD} --batch -s --database=$DATABASE)

(the script expects to be in a dir scripts and goes one level up and change into public_html. replace MAUTIC_DOCROOT with your mautic document root)

Script explanation:

  • We change into the mautic document root and get the db credentials from the mautic config

  • We then use the db credentials to get every published segment and rebuild it

  • We use the timeout command to prevent an endless running command. We preserve the status of the command to get the exit code.

( See: https://linuxize.com/post/timeout-command-in-linux/ )

  • In case of problems the function trap_error get called. You could put any command here to send a notitfication to you.

Other Benefits of this script:
The other segments are still rebuild even if one of the segment rebuilds fails!

Regarding the notfication in the bash script:

We regulary use this service to monitor cron jobs (you could also use the open source version to self host it)

Please see how to use them with bash scripts:

With https://healthchecks.io/ you would simply send the notification abount the problem like this

Notify Healthchecks.io about the error

curl -m 10 --retry 5 https://hc-ping.com/your-uuid-here/fail

and configure in https://healthchecks.io/ the notification method (we use signal and email)

Greetings
Sebastian

2 Likes