Skip to content

Commit

Permalink
fix: change load of tii eula and feature to use file cache
Browse files Browse the repository at this point in the history
  • Loading branch information
macite committed Mar 21, 2024
1 parent a53a998 commit d17c5d6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
15 changes: 12 additions & 3 deletions app/helpers/turn_it_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ def self.load_config(config)
# Launch the tii background jobs
def self.launch_tii(with_webhooks: true)
TiiRegisterWebHookJob.perform_async if with_webhooks

check_and_update_features
check_and_update_eula
load_tii_features
load_tii_eula
end

# Check if the features are up to date, and update if required
Expand All @@ -51,6 +50,11 @@ def self.check_and_update_features
feature_job.perform if feature_job.update_required?
end

def self.load_tii_features
feature_job = TiiActionFetchFeaturesEnabled.last || TiiActionFetchFeaturesEnabled.create
feature_job.fetch_features_enabled
end

# A global error indicates that tii is not configured correctly or a change in the
# environment requires that the configuration is updated
def self.global_error
Expand Down Expand Up @@ -139,6 +143,11 @@ def self.check_and_update_eula
eula_job.perform if eula_job.update_required? # Update if needed
end

def self.load_tii_eula
eula_job = TiiActionFetchEula.last || TiiActionFetchEula.create
eula_job.fetch_eula_version
end

# Return the url used for webhook callbacks
def self.webhook_url
"#{Doubtfire::Application.config.institution[:host_url]}api/tii_hook"
Expand Down
3 changes: 2 additions & 1 deletion app/models/turn_it_in/tii_action_fetch_eula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def eula_yaml_path
def load_eula_yaml
require 'yaml' # Built in, no gem required
YAML::load_file(eula_yaml_path, permitted_classes: [Time, DateTime, TCAClient::EulaVersion]) if File.exist?(eula_yaml_path) # Load
rescue
rescue StandardError
nil
end

Expand Down Expand Up @@ -81,6 +81,7 @@ def fetch_eula_version
if data && data['eula'] && data['expire'] && data['expire'] > DateTime.now
# update cache
Rails.cache.write('tii.eula_version', data['eula'], expires_in: 48.hours)
fetch_eula_html(data['eula'].version)
true
else
fetch_eula_from_tii
Expand Down
30 changes: 14 additions & 16 deletions app/models/turn_it_in/tii_action_fetch_features_enabled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,25 @@ def update_required?
last_feature_check < DateTime.now - 1.day
end

def fetch_features_enabled
# Attempt to load the feature from file
data = load_feature_yaml
features = if data && data['features'] && data['expire'] && data['expire'] > DateTime.now
data['features']
else
fetch_features_enabled_from_tii
end
# update cache
Rails.cache.write('tii.features_enabled', features, expires_in: 48.hours)
features
end

private

def run
features = fetch_features_enabled
if features.present?
self.complete = true

# update cache
Rails.cache.write('tii.features_enabled', features, expires_in: 48.hours)
else
retry_request
end
Expand All @@ -71,7 +81,7 @@ def feature_yaml_path
def load_feature_yaml
require 'yaml' # Built in, no gem required
YAML::load_file(feature_yaml_path, permitted_classes: [DateTime, Time, TCAClient::FeaturesEnabled, TCAClient::FeaturesSimilarity, TCAClient::FeaturesViewerModes, TCAClient::FeaturesGenerationSettings, TCAClient::FeaturesSimilarityViewSettings, TCAClient::FeaturesTenant]) if File.exist?(feature_yaml_path) # Load
rescue
rescue StandardError
nil
end

Expand All @@ -80,18 +90,6 @@ def save_feature_yaml(data)
File.write feature_yaml_path, data.to_yaml
end

def fetch_features_enabled
# Attempt to load the feature from file
data = load_feature_yaml
if data && data['features'] && data['expire'] && data['expire'] > DateTime.now
# update cache
Rails.cache.write('tii.feature_version', data['features'], expires_in: 48.hours)
true
else
fetch_features_enabled_from_tii
end
end

# Connect to tii to get the features enabled for this institution
def fetch_features_enabled_from_tii
exec_tca_call 'fetch TII features enabled' do
Expand Down

0 comments on commit d17c5d6

Please sign in to comment.