Skip to content

Commit

Permalink
added job to prerender all public templates. added logic to always us…
Browse files Browse the repository at this point in the history
…e activestorage copy of public plans
  • Loading branch information
briri committed Jun 24, 2024
1 parent b3347b1 commit eb35d5f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
6 changes: 3 additions & 3 deletions app/controllers/plan_exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def show
@formatting = export_params[:formatting]
@formatting = @plan.settings(:export)&.formatting if @formatting.nil?
@public_plan = false

@from_public_plans_page = export_params[:pub].to_s.downcase.strip == 'true'
elsif publicly_authorized?
skip_authorization
@show_coversheet = true
Expand Down Expand Up @@ -129,7 +129,7 @@ def show_pdf
begin
# If we have a copy of the PDF stored in ActiveStorage, just retrieve that one instead of generating it
redirect_to rails_blob_path(@plan.narrative, disposition: "attachment") and return if @plan.narrative.present? &&
current_user.nil?
@from_public_plans_page

html = render_to_string(partial: '/shared/export/plan')

Expand Down Expand Up @@ -218,7 +218,7 @@ def export_params
#
params.require(:export)
.permit(:form, :project_details, :section_headings, :question_text, :unanswered_questions,
:custom_sections, :research_outputs, :related_identifiers,
:custom_sections, :research_outputs, :related_identifiers, :pub,
formatting: [:font_face, :font_size, { margin: %i[top right bottom left] }])
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/branded/public_pages/_plans_results.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ul>
<div class="c-plan-template"><%= plan.template.title %></div>
<h3 class="c-plan-title">
<%= link_to plan.title, plan_export_path(plan, format: :pdf, export: { question_headings: true }), target: '_blank' %>
<%= link_to plan.title, plan_export_path(plan, format: :pdf, export: { question_headings: true, pub: true }), target: '_blank' %>
</h3>
<div class="c-plan-creator">
<% if plan.roles.first.user.present? %>
Expand Down
33 changes: 31 additions & 2 deletions lib/tasks/v5.rake
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace :v5 do
publisher_job_status = 'enqueued'
plan.save(touch: false)

PdfPublisherJob.perform_now(plan: plan)
PdfPublisherJob.perform_now(obj: plan)
pauser = pauser >= 11 ? 0 : (pauser += 1)
end
p "done"
Expand Down Expand Up @@ -215,7 +215,7 @@ namespace :v5 do
old_subscriptions.destroy_all

puts " registered #{plan.dmp_id}. Uploading narrative ..."
if PdfPublisherJob.perform_now(plan: plan)
if PdfPublisherJob.perform_now(obj: plan)
plan = plan.reload
puts " uploaded to #{plan.narrative_url}"
else
Expand Down Expand Up @@ -301,6 +301,35 @@ namespace :v5 do
end
end

# This will generate PDF copies of each published public template and store them in
# ActiveStorage for faster retrieval. This is being done because bad bots have been
# crawling our public pages and forcing the system to generate PDFs on the fly which
# bogs down the Rails workers
#
# As templates are published, the PDF will be regenerated and replace the existing one
# in ActiveStorage
desc 'Create PDF copies of publish public templates'
task build_template_pdfs: :environment do
pauser = 0
template_ids = Template.live(Template.families(Org.funder.pluck(:id)).pluck(:family_id))
.publicly_visible.pluck(:id) <<
Template.where(is_default: true).unarchived.published.pluck(:id)
templates = Template.includes(:org)
.where(id: templates.uniq.flatten)
.unarchived.published

templates.each do |tmplt|
next if tmplt.publisher_job_status == 'enqueued'

# Don't retrigger all of the callbacks when just changing the status of the publisher job!
tmplt.publisher_job_status = 'enqueued'
tmplt.save(touch: false)

p "Creating job to generate PDF for #{tmplt.id} - '#{tmplt.title}'"
PdfPublisherJob.perform_now(obj: tmplt)
pauser = pauser >= 11 ? 0 : (pauser += 1)
end
p "done"
end
end
# rubocop:enable Naming/VariableNumber

0 comments on commit eb35d5f

Please sign in to comment.