Skip to content

Commit

Permalink
Set perform later
Browse files Browse the repository at this point in the history
  • Loading branch information
cdccollins committed Dec 19, 2024
1 parent 4b68232 commit 7d20fe9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@
config.action_controller.raise_on_missing_callback_actions = true

config.assets.css_compressor = nil

config.active_job.queue_adapter = :test
end
12 changes: 6 additions & 6 deletions lib/tasks/scheduler.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace :scheduler do
(next unless Date.today.wday == ENV.fetch("WEEKLY_MESSAGE_DAY").to_i) if ENV.fetch("SET_WEEKLY") == "true"

User.contactable.wants_morning_message.find_in_batches do |users|
SendBulkMessageJob.perform_now(users.to_a)
SendBulkMessageJob.perform_later(users.to_a)
end
end

Expand All @@ -13,7 +13,7 @@ namespace :scheduler do
(next unless Date.today.wday == ENV.fetch("WEEKLY_MESSAGE_DAY").to_i) if ENV.fetch("SET_WEEKLY") == "true"

User.contactable.wants_afternoon_message.find_in_batches do |users|
SendBulkMessageJob.perform_now(users.to_a)
SendBulkMessageJob.perform_later(users.to_a)
end
end

Expand All @@ -22,7 +22,7 @@ namespace :scheduler do
(next unless Date.today.wday == ENV.fetch("WEEKLY_MESSAGE_DAY").to_i) if ENV.fetch("SET_WEEKLY") == "true"

User.contactable.wants_evening_message.find_in_batches do |users|
SendBulkMessageJob.perform_now(users.to_a)
SendBulkMessageJob.perform_later(users.to_a)
end
end

Expand All @@ -31,15 +31,15 @@ namespace :scheduler do
(next unless Date.today.wday == ENV.fetch("WEEKLY_MESSAGE_DAY").to_i) if ENV.fetch("SET_WEEKLY") == "true"

User.contactable.no_preference_message.find_in_batches do |users|
SendBulkMessageJob.perform_now(users.to_a)
SendBulkMessageJob.perform_later(users.to_a)
end
end

desc "Restart users who paused"
task restart_users: :environment do
User.opted_out.where("restart_at < ?", Time.now).each do |user|
user.update(contactable: true, restart_at: nil)
RestartMessagesJob.perform_now(user)
RestartMessagesJob.perform_later(user)
end
end

Expand All @@ -49,7 +49,7 @@ namespace :scheduler do

User.contactable.not_nudged.not_clicked_last_two_messages.each do |user|
message = Message.create(user:, body: "You've not interacted with any videos lately. Want to continue receiving them? You can text 'PAUSE' for a break, 'ADJUST' for different content, or 'STOP' to stop them entirely.")
SendCustomMessageJob.perform_now(message)
SendCustomMessageJob.perform_later(message)
user.update(nudged_at: Time.now)
end
end
Expand Down
32 changes: 13 additions & 19 deletions test/lib/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "rake"

class SchedulerTest < ActiveSupport::TestCase
# include ActiveJob::QueueAdapters::DelayedJobAdapter
include ActiveJob::TestHelper

setup do
ENV["WEEKLY_MESSAGE_DAY"] = "1"
Expand All @@ -17,7 +17,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: "morning")

assert_enqueued_with(job: SendMessageJob) do
assert_enqueued_with(job: SendBulkMessageJob) do
Rake::Task["scheduler:send_morning_message"].execute
end
end
Expand All @@ -26,7 +26,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: "afternoon")

assert_enqueued_with(job: SendMessageJob) do
assert_enqueued_with(job: SendBulkMessageJob) do
Rake::Task["scheduler:send_afternoon_message"].execute
end
end
Expand All @@ -35,7 +35,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: "evening")

assert_enqueued_with(job: SendMessageJob) do
assert_enqueued_with(job: SendBulkMessageJob) do
Rake::Task["scheduler:send_evening_message"].execute
end
end
Expand All @@ -44,7 +44,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: "no_preference")

assert_enqueued_with(job: SendMessageJob) do
assert_enqueued_with(job: SendBulkMessageJob) do
Rake::Task["scheduler:send_no_timing_preference_message"].execute
end
end
Expand All @@ -53,7 +53,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: nil)

assert_enqueued_with(job: SendMessageJob) do
assert_enqueued_with(job: SendBulkMessageJob) do
Rake::Task["scheduler:send_no_timing_preference_message"].execute
end
end
Expand All @@ -71,12 +71,9 @@ class SchedulerTest < ActiveSupport::TestCase
user2 = create(:user, contactable: false, restart_at: Time.now + 1.day)
user3 = create(:user, contactable: true)

stub_successful_twilio_call(
"Welcome back to Tiny Happy People! Text 'stop' to unsubscribe at any time.",
user
)

Rake::Task["scheduler:restart_users"].execute
assert_enqueued_with(job: RestartMessagesJob) do
Rake::Task["scheduler:restart_users"].execute
end

user.reload
assert user.contactable
Expand Down Expand Up @@ -105,12 +102,9 @@ class SchedulerTest < ActiveSupport::TestCase
create(:message, user: user3, clicked_at: nil, content:)
create(:message, user: user3, clicked_at: nil, content:)

stub_successful_twilio_call(
"You've not interacted with any videos lately. Want to continue receiving them? You can text 'PAUSE' for a break, 'ADJUST' for different content, or 'STOP' to stop them entirely.",
user1
)

Rake::Task["scheduler:check_for_disengaged_users"].execute
assert_enqueued_with(job: SendCustomMessageJob) do
Rake::Task["scheduler:check_for_disengaged_users"].execute
end

assert_equal 1, Message.where(body: "You've not interacted with any videos lately. Want to continue receiving them? You can text 'PAUSE' for a break, 'ADJUST' for different content, or 'STOP' to stop them entirely.").count
end
Expand All @@ -133,7 +127,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: "morning")

assert_enqueued_with(job: SendMessageJob) do
assert_enqueued_with(job: SendBulkMessageJob) do
Rake::Task["scheduler:send_morning_message"].execute
end
end
Expand Down

0 comments on commit 7d20fe9

Please sign in to comment.