Skip to content

Commit

Permalink
Created scheduled background job
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-npq committed Jun 23, 2023
1 parent d21d5f5 commit 30a3c03
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ruby File.read(".ruby-version").chomp
gem "activerecord-session_store"
gem "bootsnap", ">= 1.1.0", require: false
gem "canonical-rails"
gem "daemons"
gem "delayed_job", "~> 4.1"
gem "delayed_job_active_record"
gem "devise", "~> 4.9"
Expand Down Expand Up @@ -37,6 +38,7 @@ gem "sentry-rails"
gem "sentry-ruby"
gem "stimulus-rails"
gem "webpacker"
gem "whenever"

gem "net-imap", require: false
gem "net-pop", require: false
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ GEM
capybara (>= 1.0, < 4)
launchy
childprocess (4.1.0)
chronic (0.10.2)
coderay (1.1.3)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.2.2)
crack (0.4.5)
rexml
crass (1.0.6)
daemons (1.4.1)
date (3.3.3)
deep_merge (1.2.1)
delayed_job (4.1.11)
Expand Down Expand Up @@ -500,6 +502,8 @@ GEM
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
with_model (2.1.6)
activerecord (>= 5.2)
xpath (3.2.0)
Expand All @@ -521,6 +525,7 @@ DEPENDENCIES
canonical-rails
capybara
capybara-screenshot
daemons
delayed_job (~> 4.1)
delayed_job_active_record
devise (~> 4.9)
Expand Down Expand Up @@ -573,6 +578,7 @@ DEPENDENCIES
webdrivers
webmock
webpacker
whenever
with_model

RUBY VERSION
Expand Down
28 changes: 28 additions & 0 deletions app/jobs/receive_lead_provider_approval_status_from_ecf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class ReceiveLeadProviderApprovalStatusFromEcf < ApplicationJob
queue_as :default
def perform
begin
uri = URI.parse("#{ENV['ECF_APP_BASE_URL']}/api/v3/npq-applications/send_lead_provider_approval_status_to_npq")
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Bearer #{ENV['ECF_APP_BEARER_TOKEN']}"
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end
if response.is_a?(Net::HTTPSuccess)
response_data = JSON.parse(response.body)
filtered_applications = Application.where.not(ecf_id: nil)
response_data["data"].map do |status_data|
retrieved_id = status_data["attributes"]["id"]
retrieved_status = status_data["attributes"]["lead_provider_approval_status"]
application = filtered_applications.find_by(ecf_id: retrieved_id)
application.update!(lead_provider_approval_status: retrieved_status) if application.present?
end
else
response_message = response.message
raise "Failed to retrieve lead provider approval status: #{response_message}"
end
rescue StandardError => e
Rails.logger.error "An error occurred during lead provider approval status retrieval: #{e.message}"
end
end
end
3 changes: 3 additions & 0 deletions app/models/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def new_headteacher?
def school
School.find_by(urn: school_urn)
end
<<<<<<< HEAD

def receive_lead_provider_approval_status_from_ecf
uri = URI.parse("#{ENV['ECF_APP_BASE_URL']}/api/v3/npq-applications/send_lead_provider_approval_status_to_npq")
Expand All @@ -61,4 +62,6 @@ def receive_lead_provider_approval_status_from_ecf
rescue StandardError => e
Rails.logger.error "An error occurred during lead provider approval status retrieval: #{e.message}"
end
=======
>>>>>>> 35f455f8 (Created scheduled background job)
end
3 changes: 3 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
every :day, at: "12pm" do
runner "ReceiveLeadProviderApprovalStatusFromEcf.perform_now"
end

0 comments on commit 30a3c03

Please sign in to comment.