How can I ensure scheduled tasks aren't duplicated in a highly available setup

I'm in the process of creating a highly available Sugar setup

Web servers are basically only running Sugar, everything else is separated out (database, session handling, upload storage and elasticsearch).

The thing I'm struggling most with is cron/scheduled tasks.

I'm guessing that cron should only run on one instance, to prevent duplication of scheduled tasks (I'm guessing this is necessary, please correct me if I'm wrong, I struggle to find documentation on this), however I'm struggling to find a way to do so in an automated manner.

Our deployment is automated and we're treating our instances as disposable, so I'm looking for a way to either:

  • Run cron on only one instance
    • I can probably do this through checking for a "cron" tag on the instances in the autoscale group -> if the tag doesn't exist, then enable cron on instance and apply the "cron" tag
      • This seems like a messy solution and I'm reluctant to use it unless there's no other way
  • Run cron on all instances, but lock tasks to one instance
    • I previously tried cronlock for this, but it caused really weird behaviour/lockups in Sugar.

Ideally I'd like to avoid having a separate cron server

Would really appreciate some input/guidance!

  • So I've implemented a solution for this which works, but I'm not satisfied with it so still looking for a better solution. Just in case it helps anyone else, the way I've implemented this (specific to AWS) is:

    # Check if there is already an instance with the tag SugarCron - if not, then enable cron and apply tag
    if aws ec2 describe-tags --filters Name=tag:SugarCron,Values= --region <your-region-here> | grep SugarCron
    then
    echo "Found an instance with the SugarCron tag, not enabling cron"
    else
    echo "No instance found with SugarCron tag - enabling cron and applying tag"
    <enable cron and restart>
    aws ec2 create-tags --resources "$INSTANCE_ID" --tags Key=SugarCron,Value= --region <your-region-here>
    fi

    Definitely not satisfied with this, so other options greatly appreciated!