diff --git a/Gemfile b/Gemfile index 41dab4d..8018ed9 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ gem "postmark" group :development, :test do gem "guard-rspec", require: false + gem "foreman" gem "pry-byebug" end diff --git a/Gemfile.lock b/Gemfile.lock index 09f6281..e9a432b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -83,6 +83,8 @@ GEM roda-flow (~> 0.3) thor (~> 0.19) ffi (1.9.14) + foreman (0.82.0) + thor (~> 0.19.1) formatador (0.2.5) guard (2.14.0) formatador (>= 0.2.4) @@ -228,6 +230,7 @@ DEPENDENCIES dry-view dry-web (~> 0.5) dry-web-roda (~> 0.4) + foreman guard-rspec i18n inflecto @@ -254,4 +257,4 @@ RUBY VERSION ruby 2.3.1p112 BUNDLED WITH - 1.12.5 + 1.13.2 diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..2a101cb --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +web: bundle exec puma --port=$PORT +worker: bundle exec rake que:work diff --git a/Procfile.dev b/Procfile.dev new file mode 100644 index 0000000..5719825 --- /dev/null +++ b/Procfile.dev @@ -0,0 +1,3 @@ +web: bundle exec shotgun -p 3000 -o 0.0.0.0 config.ru +worker: bundle exec rake que:work +assets: npm start diff --git a/Rakefile b/Rakefile index dd18a05..de4fee7 100644 --- a/Rakefile +++ b/Rakefile @@ -100,3 +100,30 @@ namespace :assets do FileUtils.rm_rf("#{AppPrototype::Container.config.root}/public/assets") end end + +require "que" +namespace :que do + desc "Process Que's jobs using a worker pool" + task :work do + $stdout.puts "Starting Que's jobs..." + # # When changing how signals are caught, be sure to test the behavior with + # # the rake task in tasks/safe_shutdown.rb. + + stop = false + %w(INT TERM).each do |signal| + trap(signal) { stop = true } + end + + at_exit do + $stdout.puts "Finishing Que's current jobs before exiting..." + Que.worker_count = 0 + Que.mode = :off + $stdout.puts "Que's jobs finished, exiting..." + end + + loop do + sleep 0.01 + break if stop + end + end +end diff --git a/config/settings.example.yml b/config/settings.example.yml index 6dbc5e5..84db537 100644 --- a/config/settings.example.yml +++ b/config/settings.example.yml @@ -7,9 +7,13 @@ development: &base app_mailer_from_email: bugsnag_api_key: postmark_api_key: + que_worker_count: 1 + que_wake_interval: 0.1 test: <<: *base database_url: 'postgres://localhost/app_prototype_test' postmark_api_key: 'POSTMARK_API_TEST' + que_worker_count: 0 + que_wake_interval: 0.1 production: <<: *base diff --git a/system/app_prototype/settings.rb b/system/app_prototype/settings.rb index 6f8abbe..cd2a963 100644 --- a/system/app_prototype/settings.rb +++ b/system/app_prototype/settings.rb @@ -23,5 +23,8 @@ module Required setting :bugsnag_api_key, Types::String setting :postmark_api_key, Types::String + + setting :que_worker_count, Types::Form::Int.default(0) + setting :que_wake_interval, Types::Form::Float.default(0.1) end end diff --git a/system/boot/que.rb b/system/boot/que.rb index 11a936b..edab91b 100644 --- a/system/boot/que.rb +++ b/system/boot/que.rb @@ -7,6 +7,8 @@ container.register :connection, container["persistence.config"].gateways[:default].connection + Que.worker_count = container.settings.que_worker_count + Que.wake_interval = container.settings.que_wake_interval Que.logger = container[:logger] Que.mode = :sync if container.config.env == :test Que.connection = container["que.connection"]