]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Added a job runner for mailouts
authorAidan Cornelius-Bell <[email protected]>
Sun, 13 Oct 2024 04:19:30 +0000 (14:49 +1030)
committerAidan Cornelius-Bell <[email protected]>
Sun, 13 Oct 2024 04:19:30 +0000 (14:49 +1030)
app/controllers/job_runner_controller.rb [new file with mode: 0644]
app/helpers/job_runner_helper.rb [new file with mode: 0644]
app/jobs/weekly_bookmarks_digest_job.rb
app/views/digest_mailer/weekly_bookmarks_digest.html.erb
app/views/job_runner/index.html.erb [new file with mode: 0644]
app/views/job_runner/run.html.erb [new file with mode: 0644]
config/routes.rb
test/controllers/job_runner_controller_test.rb [new file with mode: 0644]

diff --git a/app/controllers/job_runner_controller.rb b/app/controllers/job_runner_controller.rb
new file mode 100644 (file)
index 0000000..1975f3a
--- /dev/null
@@ -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 (file)
index 0000000..85b2738
--- /dev/null
@@ -0,0 +1,2 @@
+module JobRunnerHelper
+end
index f66422937dae5a429a45da9bab31b75bfa2a3460..87e7f747a880d83ddf202fbff43856bf8f731d7e 100644 (file)
@@ -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
index 38997d35094f7fee51b648626e875c83a4c11e79..03fc0bab734ca1a5f8db9bf0b30ab5cabf450a72 100644 (file)
@@ -15,4 +15,5 @@
   <% end %>
 </ul>
 
-<p>Have a great weekend!</p>
\ No newline at end of file
+<p>Have a great weekend,<br>
+Aidan.</p>
\ 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 (file)
index 0000000..95ad961
--- /dev/null
@@ -0,0 +1,26 @@
+<div class="container">
+       <h1>Rails jobs</h1>
+       <%= link_to "Back home...", root_url %>
+</div>
+
+<div class="post">
+       <div class="container">
+               <h3>Send a weekly bookmark digest:</h3>
+               
+               
+               <%= form_tag job_runner_path, method: :post do %>
+                 <div>
+                       <%= label_tag :job, "Select Job:" %>
+                       <%= select_tag :job, options_for_select([['Weekly Bookmarks Digest', 'weekly_bookmarks_digest']]) %>
+                 </div>
+               
+                 <div>
+                       <%= 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)" %>
+                 </div>
+               
+                 <%= submit_tag "Run Job" %>
+               <% end %>
+               
+       </div>
+</div>
\ 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 (file)
index 0000000..dd7245e
--- /dev/null
@@ -0,0 +1,2 @@
+<h1>JobRunner#run</h1>
+<p>Find me in app/views/job_runner/run.html.erb</p>
index e126c357f49d9a8947b2065e90e04e63fdf15849..9df6e47fc16393c4c18f1ad27e335c2786bc5f2b 100644 (file)
@@ -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 (file)
index 0000000..c39d3d7
--- /dev/null
@@ -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