Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module RecurringJob
class SendAccountResetNotificationsController < BaseController
def create
count = AccountReset::GrantRequestsAndSendEmails.new.call
analytics.track_event(Analytics::ACCOUNT_RESET, event: :notifications, count: count)
render plain: 'ok'
render(
plain: 'This endpoint has been removed in favor of idp-jobs.',
status: :gone,
)
end

private
Expand Down
9 changes: 9 additions & 0 deletions app/services/account_reset/grant_requests_and_send_emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def call
).order('requested_at ASC').each do |arr|
notifications_sent += 1 if grant_request_and_send_email(arr)
end

# TODO: rewrite analytics so that we can generate events even from
# background jobs where we have no request or user objects
# analytics.track_event(Analytics::ACCOUNT_RESET,
# event: :notifications, count: notifications_sent)

Rails.logger.info("Sent #{notifications_sent} account_reset notifications")

notifications_sent
end

Expand All @@ -26,6 +34,7 @@ def sql_query_for_users_eligible_to_delete_their_accounts
def grant_request_and_send_email(arr)
user = arr.user
return false unless AccountReset::GrantRequest.new(user).call

arr = arr.reload
user.confirmed_email_addresses.each do |email_address|
UserMailer.account_reset_granted(email_address, arr).deliver_later
Expand Down
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require_relative '../lib/upaya_log_formatter'

Bundler.require(*Rails.groups)

Expand All @@ -25,6 +26,9 @@ class Application < Rails::Application
event.payload.except(:params, :headers)
end

# Use a custom log formatter to get timestamp
config.log_formatter = Upaya::UpayaLogFormatter.new

require 'headers_filter'
config.middleware.insert_before 0, HeadersFilter
require 'utf8_sanitizer'
Expand Down
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
config.lograge.ignore_actions = ['Users::SessionsController#active']
config.lograge.formatter = Lograge::Formatters::Json.new

# Override log formatter
config.log_formatter = Upaya::DevelopmentUpayaLogFormatter.new

# Bullet gem config
config.after_initialize do
Bullet.enable = true
Expand Down
15 changes: 12 additions & 3 deletions config/initializers/job_configurations.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# rubocop:disable Metrics/LineLength
# Daily GPO letter mailings
JobRunner::Runner.configurations << JobRunner::JobConfiguration.new(
name: 'Send GPO letter',
interval: 24 * 60 * 60,
timeout: 300,
callback: -> { UspsConfirmationUploader.new.run unless HolidayService.observed_holiday?(Time.zone.today) },
callback: lambda {
UspsConfirmationUploader.new.run unless HolidayService.observed_holiday?(Time.zone.today)
},
)

# Send account deletion confirmation notifications
JobRunner::Runner.configurations << JobRunner::JobConfiguration.new(
name: 'Account reset notice',
interval: 5 * 60, # 5 minutes
timeout: 4 * 60,
callback: -> { AccountReset::GrantRequestsAndSendEmails.new.call },
)
# rubocop:enable Metrics/LineLength
41 changes: 0 additions & 41 deletions lib/lambdas/account_reset_lambda.rb

This file was deleted.

35 changes: 35 additions & 0 deletions lib/upaya_log_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Upaya
class UpayaLogFormatter < ::Logger::Formatter
# This method is invoked when a log event occurs
def call(severity, timestamp, progname, msg)
# If message looks like JSON, print it directly. This is a hack to avoid
# needing to change the analytics ETL Lambdas that parse the pageview
# JSON logs.
# If the Analytics ETL lambda is no longer in use or has had more
# sophisticated parsing added, then this could be removed.
if msg.is_a?(String) && msg.start_with?('{') && msg.end_with?('}')
"#{msg}\n"
else
# Otherwise, use default Ruby log format
super
end
end
end

class DevelopmentUpayaLogFormatter < UpayaLogFormatter
# This method is invoked when a log event occurs
def call(severity, timestamp, progname, msg)
# If message contains terminal escapes, print it directly. This is useful
# in development because rails dev logs contain SQL queries with ANSI
# terminal escapes that should be printed as-is without timestamps.
if msg.is_a?(String) && msg.include?("\u001b[")
"#{msg}\n"
else
# Otherwise, see parent
super
end
end
end
end
29 changes: 29 additions & 0 deletions spec/config/initializers/job_configurations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rails_helper'

describe JobRunner::Runner do
describe '.configurations' do
it 'has the GPO letter job' do
job = JobRunner::Runner.configurations.find { |c| c.name == 'Send GPO letter' }
expect(job).to be_instance_of(JobRunner::JobConfiguration)
expect(job.interval).to eq 24 * 60 * 60

stub = instance_double(UspsConfirmationUploader)
expect(UspsConfirmationUploader).to receive(:new).and_return(stub)
expect(stub).to receive(:run).and_return('the GPO test worked')

expect(job.callback.call).to eq 'the GPO test worked'
end

it 'runs the account reset job' do
job = JobRunner::Runner.configurations.find { |c| c.name == 'Account reset notice' }
expect(job).to be_instance_of(JobRunner::JobConfiguration)
expect(job.interval).to eq 300

service = instance_double(AccountReset::GrantRequestsAndSendEmails)
expect(AccountReset::GrantRequestsAndSendEmails).to receive(:new).and_return(service)
expect(service).to receive(:call).and_return('the reset test worked')

expect(job.callback.call).to eq 'the reset test worked'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

describe RecurringJob::SendAccountResetNotificationsController do
describe '#create' do
it_behaves_like 'a recurring job controller', Figaro.env.account_reset_auth_token

context 'with a good auth token' do
before do
request.headers['X-API-AUTH-TOKEN'] = Figaro.env.account_reset_auth_token
end

it 'grants account reset requests and sends emails' do
# controller is disabled
it 'grants account reset requests and sends emails', skip: true do
service = instance_double(AccountReset::GrantRequestsAndSendEmails)
allow(AccountReset::GrantRequestsAndSendEmails).to receive(:new).and_return(service)
allow(service).to receive(:call).and_return(7)

stub_analytics
expect(@analytics).to receive(:track_event).
with(Analytics::ACCOUNT_RESET, event: :notifications, count: 7)
post :create
end

it 'returns 410 Gone to indicate the endpoint is deprecated' do
post :create
expect(response).to have_http_status(:gone)
end
end
end
Expand Down
20 changes: 0 additions & 20 deletions spec/lib/lambdas/account_reset_lambda_spec.rb

This file was deleted.

38 changes: 38 additions & 0 deletions spec/lib/upaya_log_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'
require 'upaya_log_formatter'

RSpec.describe Upaya::UpayaLogFormatter do
describe '.call' do
it 'prints expected standard messages' do
now = Time.utc(2019, 1, 2, 3, 4, 5)
expect(Upaya::UpayaLogFormatter.new.call('INFO', now, 'progname', 'hello')).to eq(
"I, [2019-01-02T03:04:05.000000 ##{Process.pid}] INFO -- progname: hello\n",
)
end

it 'prints JSON-like messages as-is' do
expect(
Upaya::UpayaLogFormatter.new.call('INFO', Time.zone.now, 'progname', '{"hello"}'),
).to eq('{"hello"}' + "\n")
end
end
end

describe Upaya::DevelopmentUpayaLogFormatter do
describe '.call' do
it 'prints ANSI escaped messages as-is' do
now = Time.utc(2019, 1, 2, 3, 4, 5)
msg = "\e[1;31mhello\e[m"
expect(Upaya::DevelopmentUpayaLogFormatter.new.call('INFO', now, 'progname', msg)).to eq(
msg + "\n",
)
end

it 'prints expected messages otherwise' do
now = Time.utc(2019, 1, 2, 3, 4, 5)
expect(Upaya::DevelopmentUpayaLogFormatter.new.call('INFO', now, 'progname', 'hello')).to eq(
"I, [2019-01-02T03:04:05.000000 ##{Process.pid}] INFO -- progname: hello\n",
)
end
end
end