1

I have a class method that I need to run every 15 min, I have the cron job

0,15,30,45 * * * * /bin/bash -l -c 'cd /var/app/current && sudo /opt/rubies/ruby-2.3.5/bin/bundle exec /opt/rubies/ruby-2.3.5/bin/rails runner -e production '\''Structure.check_parking'\'' >> /var/app/current/log/cron_log 2>&1'

Running in my elastic beanstsalk environment, but I keep getting an error of

/opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/lib/mysql2/client.rb:87:in `connect': Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

I am using an RDS server in my production environment so it is strange that it's trying to connect to a local mysql server.

The rails app is connected to the database and I can do all normal functionality on it but when I try to run this class method I am getting this error.

My guess is that it's not running in production, but I'm really not sure, my database.yml file looks like

development:
    adapter: mysql2
    database: bddatabase
    encoding: utf8
    username: bduser
    password: dbpass
    host: 127.0.0.1
    port: 3306

production:
    adapter: mysql2
    encoding: utf8
    database: <%= ENV['RDS_DB_NAME'] %>
    username: <%= ENV['RDS_USERNAME'] %>
    password: <%= ENV['RDS_PASSWORD'] %>
    host: <%= ENV['RDS_HOSTNAME'] %>
    port: <%= ENV['RDS_PORT'] %>

Been trying to figure this out for a while now, any help is really appreciated!

evanshabsove
  • 157
  • 1
  • 14

1 Answers1

1

I figured it out, turns out there was a problem with my cron task it's self. I think how I was referencing bundle and rails the way I did it wasn't pointing at my app which was causing the error. Changing the command to

0,15,30,45 * * * * /bin/bash -l -c 'cd /var/app/current && bundle exec rails runner -e production 'Structure.check_parking' >> /var/app/current/log/cron_log 2>&1'

Did the trick

evanshabsove
  • 157
  • 1
  • 14
  • Can you please tell me how you set up the cron task in the first place? – akshay_kashain Mar 22 '18 at 18:50
  • Is this a rails specific question? Or just a how to set up cron jobs? – evanshabsove Mar 23 '18 at 19:36
  • Sorry, should have been more specific. I was more interested to know how you set up cron jobs in Elastic Beanstalk – akshay_kashain Mar 26 '18 at 15:06
  • Gotcha, I followed this https://stackoverflow.com/questions/14077095/aws-elastic-beanstalk-running-a-cronjob mostly. Ideally you should have a separate server that just runs cronjob's but for us at the given moment we only have a single instance. The issue with cronjobs in eb is that when a new instance gets made the cron job will now run on both. There's ways to get around this but ideally you should just have a separate instance. – evanshabsove Mar 26 '18 at 15:38