From c05fbb2dd39f3473d7039e85d08003f2ccdf77b5 Mon Sep 17 00:00:00 2001 From: Aidan Cornelius-Bell Date: Sun, 13 Oct 2024 14:49:30 +1030 Subject: [PATCH] Added a job runner for mailouts --- app/controllers/job_runner_controller.rb | 18 +++++++++++++ app/helpers/job_runner_helper.rb | 2 ++ app/jobs/weekly_bookmarks_digest_job.rb | 8 ++++-- .../weekly_bookmarks_digest.html.erb | 3 ++- app/views/job_runner/index.html.erb | 26 +++++++++++++++++++ app/views/job_runner/run.html.erb | 2 ++ config/routes.rb | 2 ++ .../controllers/job_runner_controller_test.rb | 13 ++++++++++ 8 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 app/controllers/job_runner_controller.rb create mode 100644 app/helpers/job_runner_helper.rb create mode 100644 app/views/job_runner/index.html.erb create mode 100644 app/views/job_runner/run.html.erb create mode 100644 test/controllers/job_runner_controller_test.rb diff --git a/app/controllers/job_runner_controller.rb b/app/controllers/job_runner_controller.rb new file mode 100644 index 0000000..1975f3a --- /dev/null +++ b/app/controllers/job_runner_controller.rb @@ -0,0 +1,18 @@ +class JobRunnerController < ApplicationController + include AdminAuthenticatable + + def index + @users = User.all.order(:created_at) + end + + def run + if params[:job] == 'weekly_bookmarks_digest' + user = params[:user_id].present? ? User.find(params[:user_id]) : nil + WeeklyBookmarksDigestJob.perform_later(user) + flash[:notice] = user ? "Weekly bookmarks digest job queued for #{user.email}" : "Weekly bookmarks digest job queued for all users" + else + flash[:alert] = "Invalid job selection" + end + redirect_to job_runner_path + end +end diff --git a/app/helpers/job_runner_helper.rb b/app/helpers/job_runner_helper.rb new file mode 100644 index 0000000..85b2738 --- /dev/null +++ b/app/helpers/job_runner_helper.rb @@ -0,0 +1,2 @@ +module JobRunnerHelper +end diff --git a/app/jobs/weekly_bookmarks_digest_job.rb b/app/jobs/weekly_bookmarks_digest_job.rb index f664229..87e7f74 100644 --- a/app/jobs/weekly_bookmarks_digest_job.rb +++ b/app/jobs/weekly_bookmarks_digest_job.rb @@ -1,11 +1,15 @@ class WeeklyBookmarksDigestJob < ApplicationJob queue_as :default - def perform(*args) + def perform(user = nil) start_date = 1.week.ago.beginning_of_week end_date = Time.current.end_of_week bookmarks = Post.bookmarks.where(created_at: start_date..end_date) # or do User.find_each do |user| for everyone, but that'll break fastmail - DigestMailer.weekly_bookmarks_digest(User.first, bookmarks).deliver_now + if user.present? + DigestMailer.weekly_bookmarks_digest(user, bookmarks).deliver_now + else + DigestMailer.weekly_bookmarks_digest(User.first, bookmarks).deliver_now + end end end diff --git a/app/views/digest_mailer/weekly_bookmarks_digest.html.erb b/app/views/digest_mailer/weekly_bookmarks_digest.html.erb index 38997d3..03fc0ba 100644 --- a/app/views/digest_mailer/weekly_bookmarks_digest.html.erb +++ b/app/views/digest_mailer/weekly_bookmarks_digest.html.erb @@ -15,4 +15,5 @@ <% end %> -

Have a great weekend!

\ No newline at end of file +

Have a great weekend,
+Aidan.

\ No newline at end of file diff --git a/app/views/job_runner/index.html.erb b/app/views/job_runner/index.html.erb new file mode 100644 index 0000000..95ad961 --- /dev/null +++ b/app/views/job_runner/index.html.erb @@ -0,0 +1,26 @@ +
+

Rails jobs

+ <%= link_to "Back home...", root_url %> +
+ +
+
+

Send a weekly bookmark digest:

+ + + <%= form_tag job_runner_path, method: :post do %> +
+ <%= label_tag :job, "Select Job:" %> + <%= select_tag :job, options_for_select([['Weekly Bookmarks Digest', 'weekly_bookmarks_digest']]) %> +
+ +
+ <%= label_tag :user_id, "Select User (optional):" %> + <%= select_tag :user_id, options_from_collection_for_select(@users, :id, :email), prompt: "Select a user (defaults to Aidan)" %> +
+ + <%= submit_tag "Run Job" %> + <% end %> + +
+
\ No newline at end of file diff --git a/app/views/job_runner/run.html.erb b/app/views/job_runner/run.html.erb new file mode 100644 index 0000000..dd7245e --- /dev/null +++ b/app/views/job_runner/run.html.erb @@ -0,0 +1,2 @@ +

JobRunner#run

+

Find me in app/views/job_runner/run.html.erb

diff --git a/config/routes.rb b/config/routes.rb index e126c35..9df6e47 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,8 @@ Rails.application.routes.draw do resource :two_factor, only: [:new, :create, :destroy], controller: 'two_factor' do get 'backup_codes', on: :member end + get "job_runner", to: "job_runner#index" + post "job_runner", to: "job_runner#run" resources :subscriptions, only: [:new, :create, :index] resources :pages get 'importer', to: 'posts#importer' diff --git a/test/controllers/job_runner_controller_test.rb b/test/controllers/job_runner_controller_test.rb new file mode 100644 index 0000000..c39d3d7 --- /dev/null +++ b/test/controllers/job_runner_controller_test.rb @@ -0,0 +1,13 @@ +require "test_helper" + +class JobRunnerControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get job_runner_index_url + assert_response :success + end + + test "should get run" do + get job_runner_run_url + assert_response :success + end +end -- 2.39.5