response - elastic beanstalk cron run twice -
i have 1 application on elastic beanstalk , cron jobs it.
the code of setting cron is
container_commands: 01_some_cron_job: command: "echo '*/5 * * * * wget -o - -q -t 1 http://site.com/cronscript/' | crontab" leader_only: true
this script calls mail sender. , i'm receive 2 message per time.
code of http://site.com/cronscript/
looks (php code)
require_once('ses.php'); $ses = new simpleemailservice(email_shortkey, email_longkey); $m = new simpleemailservicemessage(); $m->addto('user@domain.com'); $m->setfrom('response_service@domain.com'); $m->setsubject('test message'); $m->setmessagefromstring('', 'message content'); $send_emails=($ses->sendemail($m));
when call http://site.com/cronscript/
browser's address bar, receive 1 message want.
i believe what's happening first time deploy app, autoscaling picks 1 instance leader , new cron job created on instance. next time deploy app, autoscaling picks instance leader. end same cron job on 2 instances.
so basic test ssh instances , check crontab
contents crontab -l
you can avoid duplicate cron jobs removing old cron jobs on instance regardless whether leader or not.
container_commands: 00_remove_old_cron_jobs: command: "crontab -r || exit 0" 01_some_cron_job: command: "echo '*/5 * * * * wget -o - -q -t 1 http://example.com/cronscript/' | crontab" leader_only: true
as mentioned in running cron in elastic beanstalk auto-scaling environment: || exit 0
mandatory because if there no crontab in machine crontab -r
command return status code > 0 (an error). elastic beanstalk stop deploy process if 1 of container_commands fail.
although never experienced situation when crontab
not found on elastic beanstalk instance.
you can run /opt/elasticbeanstalk/bin/leader-test.sh
test whether leader instance or not.
hope helps.
Comments
Post a Comment