diff --git a/app/helpers/turn_it_in.rb b/app/helpers/turn_it_in.rb index 702331097..57aefc0f1 100644 --- a/app/helpers/turn_it_in.rb +++ b/app/helpers/turn_it_in.rb @@ -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 @@ -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 @@ -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" diff --git a/app/models/turn_it_in/tii_action_fetch_eula.rb b/app/models/turn_it_in/tii_action_fetch_eula.rb index 14ade96ce..7b16a983c 100644 --- a/app/models/turn_it_in/tii_action_fetch_eula.rb +++ b/app/models/turn_it_in/tii_action_fetch_eula.rb @@ -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 @@ -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 diff --git a/app/models/turn_it_in/tii_action_fetch_features_enabled.rb b/app/models/turn_it_in/tii_action_fetch_features_enabled.rb index db0c266bd..e9dcc94e2 100644 --- a/app/models/turn_it_in/tii_action_fetch_features_enabled.rb +++ b/app/models/turn_it_in/tii_action_fetch_features_enabled.rb @@ -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 @@ -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 @@ -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