evanshabsove
evanshabsove

Reputation: 167

Rails class method in cron job not working in elastic beanstalk

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!

Upvotes: 1

Views: 331

Answers (1)

evanshabsove
evanshabsove

Reputation: 167

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

Upvotes: 1

Related Questions