diff --git a/Gemfile b/Gemfile index 27c7a18..ca8aaa6 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem "dotenv-rails", groups: %i[development test] gem "rails", "5.2.1" +gem "delayed_job_active_record" gem "dragonfly" gem "http" gem "jquery-rails" diff --git a/Gemfile.lock b/Gemfile.lock index 03ba032..c57953f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -65,6 +65,11 @@ GEM safe_yaml (~> 1.0.0) crass (1.0.4) debug_inspector (0.0.3) + delayed_job (4.1.5) + activesupport (>= 3.0, < 5.3) + delayed_job_active_record (4.1.3) + activerecord (>= 3.0, < 5.3) + delayed_job (>= 3.0, < 5) diff-lcs (1.3) domain_name (0.5.20180417) unf (>= 0.0.5, < 1.0.0) @@ -244,6 +249,7 @@ DEPENDENCIES better_errors binding_of_caller capybara + delayed_job_active_record dotenv-rails dragonfly http diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..f787d4e --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +web: bundle exec puma -C config/puma.rb +worker: bundle exec rake jobs:work diff --git a/bin/delayed_job b/bin/delayed_job new file mode 100755 index 0000000..edf1959 --- /dev/null +++ b/bin/delayed_job @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) +require 'delayed/command' +Delayed::Command.new(ARGV).daemonize diff --git a/config/application.rb b/config/application.rb index d7c3146..8069124 100644 --- a/config/application.rb +++ b/config/application.rb @@ -26,5 +26,7 @@ module MyImages # ] # config.i18n.default_locale = :de config.autoload_paths += %W(#{config.root}/lib) + + config.active_job.queue_adapter = :delayed_job end end diff --git a/config/puma.rb b/config/puma.rb new file mode 100644 index 0000000..7d3431e --- /dev/null +++ b/config/puma.rb @@ -0,0 +1,15 @@ +workers Integer(ENV["WEB_CONCURRENCY"] || 2) +threads_count = Integer(ENV["RAILS_MAX_THREADS"] || 5) +threads threads_count, threads_count + +preload_app! + +rackup DefaultRackup +port ENV["PORT"] || 3000 +environment ENV["RACK_ENV"] || "development" + +on_worker_boot do + # Worker specific setup for Rails 4.1+ + # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot + ActiveRecord::Base.establish_connection +end diff --git a/db/migrate/20180826163510_create_delayed_jobs.rb b/db/migrate/20180826163510_create_delayed_jobs.rb new file mode 100644 index 0000000..d10f240 --- /dev/null +++ b/db/migrate/20180826163510_create_delayed_jobs.rb @@ -0,0 +1,22 @@ +class CreateDelayedJobs < ActiveRecord::Migration[5.2] + def self.up + create_table :delayed_jobs, force: true do |table| + table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue + table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually. + table.text :handler, null: false # YAML-encoded string of the object that will do work + table.text :last_error # reason for last failure (See Note below) + table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future. + table.datetime :locked_at # Set when a client is working on this object + table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) + table.string :locked_by # Who is working on this object (if locked) + table.string :queue # The name of the queue this job is in + table.timestamps null: true + end + + add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority" + end + + def self.down + drop_table :delayed_jobs + end +end diff --git a/db/schema.rb b/db/schema.rb index 231143d..491eb0b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,26 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_08_25_150330) do +ActiveRecord::Schema.define(version: 2018_08_26_163510) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "delayed_jobs", force: :cascade do |t| + t.integer "priority", default: 0, null: false + t.integer "attempts", default: 0, null: false + t.text "handler", null: false + t.text "last_error" + t.datetime "run_at" + t.datetime "locked_at" + t.datetime "failed_at" + t.string "locked_by" + t.string "queue" + t.datetime "created_at" + t.datetime "updated_at" + t.index ["priority", "run_at"], name: "delayed_jobs_priority" + end + create_table "images", force: :cascade do |t| t.integer "user_id" t.string "image_uid"