Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a statement_timeout for the db for *only* web queries in Rails?

Is it possible to set a statement_timeout for the db (Postgres) for only web queries in Ruby on Rails (vs sidekiq workers/jobs)? Reason being Heroku web queries timeout after 30 seconds so it makes sense to kill queries from Puma after that. But I want sidekiq db queries to be able to execute for much longer?

I already tried doing something like this in database.yml

  primary:
    <<: *default
    url: <%= ENV['DATABASE_URL']) %>
    variables:
      statement_timeout: <%= ENV["STATEMENT_TIMEOUT"] || "0" %>

Then doing this in my Procfile

web: STATEMENT_TIMEOUT=30s bundle exec puma

But it didn't work. The statement timeout would get reset back to 0 after a few requests.

like image 896
concept47 Avatar asked Oct 20 '19 06:10

concept47


1 Answers

It might've been a syntax issue; I wrote mine out in ms and it works fine.

web: STATEMENT_TIMEOUT=30000 bundle exec puma

I flipped it to provide an override for my workers, and then defaulted to 27 sec otherwise:

# database.yml
<% statement_timeout = ENV['STATEMENT_TIMEOUT'] || '27000' %>

production:
  ...
  variables:
    statement_timeout: <%= statement_timeout %>

and in my Procfile:

web: bundle exec puma -C config/puma.rb
worker: STATEMENT_TIMEOUT=600000 rake jobs:work
like image 99
Andrew Smith Avatar answered Oct 22 '22 12:10

Andrew Smith