Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background page publishing #2067

Merged
merged 2 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alchemy_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |gem|
actionmailer
actionpack
actionview
activejob
activemodel
activerecord
activesupport
Expand Down
11 changes: 11 additions & 0 deletions app/jobs/alchemy/base_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Alchemy
class BaseJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end
end
11 changes: 11 additions & 0 deletions app/jobs/alchemy/publish_page_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Alchemy
class PublishPageJob < BaseJob
queue_as :default

def perform(page, public_on:)
Alchemy::Page::Publisher.new(page).publish!(public_on: public_on)
end
end
end
4 changes: 2 additions & 2 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ def copy_children_to(new_parent)
# The +published_at+ attribute is used as +cache_key+.
#
def publish!(current_time = Time.current)
update_columns(published_at: current_time)
Publisher.new(self).publish!(public_on: current_time)
update(published_at: current_time)
PublishPageJob.perform_later(self, public_on: current_time)
end

# Sets the public_on date on the published version
Expand Down
6 changes: 3 additions & 3 deletions spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative 'boot'
require_relative "boot"

require "rails"
# Pick the frameworks you want:
Expand All @@ -9,7 +9,7 @@
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
# require "active_job/railtie"
require "active_job/railtie"
# require "action_cable/engine"
# require "rails/test_unit/railtie"
require "sprockets/railtie"
Expand All @@ -23,7 +23,7 @@ module Dummy
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
if config.respond_to?(:load_defaults)
config.load_defaults ENV['RAILS_VERSION'] || 6.0
config.load_defaults ENV["RAILS_VERSION"] || 6.0
end

# Settings in config/environments/* take precedence over those specified here.
Expand Down
8 changes: 5 additions & 3 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

config.cache_classes = true

# Do not eager load code on boot. This avoids loading your whole application
Expand All @@ -16,11 +16,11 @@
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
"Cache-Control" => "public, max-age=#{1.hour.to_i}",
}

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store

Expand All @@ -42,4 +42,6 @@

# Raises error for missing translations.
# config.action_view.raise_on_missing_translations = true

config.active_job.queue_adapter = :test
end
23 changes: 23 additions & 0 deletions spec/jobs/alchemy/publish_page_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::PublishPageJob, type: :job do
describe "#perfom_later" do
let(:page) { build_stubbed(:alchemy_page) }
let(:public_on) { Time.current }

it "enqueues job" do
expect {
described_class.perform_later(page, public_on: public_on)
}.to have_enqueued_job
end

it "calls the page publisher" do
expect_any_instance_of(Alchemy::Page::Publisher).to receive(:publish!).with(
public_on: public_on,
)
described_class.new.perform(page, public_on: public_on)
end
end
end
7 changes: 4 additions & 3 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1413,9 +1413,10 @@ class AnotherUrlPathClass; end
allow(Time).to receive(:current).and_return(current_time)
end

it "calls the page publisher" do
expect_any_instance_of(Alchemy::Page::Publisher).to receive(:publish!).with(public_on: current_time)
page.publish!
it "enqueues publish page job" do
expect {
page.publish!
}.to have_enqueued_job(Alchemy::PublishPageJob)
end

it "sets published_at" do
Expand Down