Skip to content

Commit

Permalink
Merge pull request #62 from nestauk/perform-now
Browse files Browse the repository at this point in the history
Enqueue the jobs now
  • Loading branch information
cdccollins authored Dec 19, 2024
2 parents 3242000 + 6c17f75 commit e9fdd55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
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_later(users.to_a)
SendBulkMessageJob.perform_now(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_later(users.to_a)
SendBulkMessageJob.perform_now(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_later(users.to_a)
SendBulkMessageJob.perform_now(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_later(users.to_a)
SendBulkMessageJob.perform_now(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_later(user)
RestartMessagesJob.perform_now(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_later(message)
SendCustomMessageJob.perform_now(message)
user.update(nudged_at: Time.now)
end
end
Expand Down
30 changes: 18 additions & 12 deletions test/lib/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: "morning")

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

assert_enqueued_with(job: RestartMessagesJob) do
Rake::Task["scheduler:restart_users"].execute
end
stub_successful_twilio_call(
"Welcome back to Tiny Happy People! Text 'stop' to unsubscribe at any time.",
user
)

Rake::Task["scheduler:restart_users"].execute

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

assert_enqueued_with(job: SendCustomMessageJob) do
Rake::Task["scheduler:check_for_disengaged_users"].execute
end
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_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 @@ -127,7 +133,7 @@ class SchedulerTest < ActiveSupport::TestCase
create(:content)
create(:user, timing: "morning")

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

0 comments on commit e9fdd55

Please sign in to comment.