diff --git a/app/jobs/sync_trustie_job.rb b/app/jobs/sync_trustie_job.rb index 8d92696eb..33df5b529 100644 --- a/app/jobs/sync_trustie_job.rb +++ b/app/jobs/sync_trustie_job.rb @@ -6,24 +6,21 @@ class SyncTrustieJob < ApplicationJob def perform(type, count) Rails.logger.info("#######_________response__sync__start__#########") - configs_content = Rails.application.config_for(:configuration) - token = configs_content["sync_token"] - token_url = configs_content["sync_url"] - url = "#{token_url}/api/v1/homes/sync_count" + token = EduSetting.get('trustie_api_token') + api_host = EduSetting.get('trustie_api_url') + + url = "#{api_host}/api/v1/homes/sync_count" sync_json = { "token": token, "type": type, "number": count } uri = URI.parse(url) - http = Net::HTTP.new(uri.hostname, uri.port) - if token_url.include?("https://") - http.use_ssl = true + if api_host + http = Net::HTTP.new(uri.hostname, uri.port) + http.send_request('PUT', uri.path, sync_json.to_json, {'Content-Type' => 'application/json'}) + Rails.logger.info("#######_________response__sync__end_____#########") end - - - response = http.send_request('PUT', uri.path, sync_json.to_json, {'Content-Type' => 'application/json'}) - Rails.logger.info("#######_________response__sync__end_____#########{response.body}") end -end \ No newline at end of file +end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index ded4119cd..888c65423 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -12,4 +12,8 @@ class ApplicationRecord < ActiveRecord::Base def display_extra_data(key) _extra_data&.[](key) end + + def allow_sync_to_trustie? + Rails.env.production? && EduSetting.get('host_name') == 'https://www.educoder.net' + end end diff --git a/app/models/project.rb b/app/models/project.rb index c884935d8..2e0a8be64 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -8,7 +8,9 @@ class Project < ApplicationRecord has_many :issues has_many :user_grades, dependent: :destroy - after_create :sync_project_trustie #同步到trustie + after_create do + SyncTrustieJob.perform_later("project", 1) if allow_sync_to_trustie? + end #同步到trustie # 创建者 def creator @@ -23,7 +25,4 @@ class Project < ApplicationRecord members.exists?(user_id: user.id) end - def sync_project_trustie - SyncTrustieJob.perform_later("project", 1) - end end diff --git a/app/models/school.rb b/app/models/school.rb index 1608377a6..10bbd4e76 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -19,7 +19,9 @@ class School < ApplicationRecord has_many :apply_add_departments, dependent: :destroy has_many :user_extensions, dependent: :nullify - after_create :sync_school_trustie #同步到trustie + after_create do + SyncTrustieJob.perform_later("school", 1) if allow_sync_to_trustie? #同步到trustie + end # 学校管理员 def manager?(user) @@ -41,8 +43,4 @@ class School < ApplicationRecord def manage_permission?(user) manager?(user) || major_manager?(user) || course_manager?(user) end - - def sync_school_trustie - SyncTrustieJob.perform_later("school", 1) - end end diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 744d486e3..103c8b68f 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -75,7 +75,11 @@ class Shixun < ApplicationRecord scope :field_for_recommend, lambda{ select([:id, :name, :identifier, :myshixuns_count]) } scope :find_by_ids,lambda{|k| where(id:k)} - after_create :send_tiding, :sync_shixun_trustie #同步到trustie + after_create :send_tiding + #同步到trustie + after_create do + SyncTrustieJob.perform_later("practical_training_project", 1) if allow_sync_to_trustie? + end # REDO:  def propaedeutics @@ -290,10 +294,6 @@ class Shixun < ApplicationRecord subjects.where(hidden: 0).uniq end - def sync_shixun_trustie - SyncTrustieJob.perform_later("practical_training_project", 1) - end - private def send_tiding diff --git a/app/models/user.rb b/app/models/user.rb index 806abc98f..4d13727db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -156,7 +156,9 @@ class User < ApplicationRecord delegate :gender, :department_id, :school_id, :location, :location_city, :technical_title, to: :user_extension, allow_nil: true before_save :update_hashed_password - after_create :sync_user_trustie #同步到trustie + after_create do + SyncTrustieJob.perform_later("user", 1) if allow_sync_to_trustie? + end # # validations @@ -314,10 +316,6 @@ class User < ApplicationRecord shixun.shixun_members.exists?(role: 2, user_id: id) end - def sync_user_trustie - SyncTrustieJob.perform_later("user", 1) - end - # TPI的创建者 def creator_of_game?(game) id == game.user_id diff --git a/db/migrate/20191029065244_init_trustie_config.rb b/db/migrate/20191029065244_init_trustie_config.rb new file mode 100644 index 000000000..24ec7a245 --- /dev/null +++ b/db/migrate/20191029065244_init_trustie_config.rb @@ -0,0 +1,8 @@ +class InitTrustieConfig < ActiveRecord::Migration[5.2] + def change + hash = {"trustie_api_token" => "", "trustie_api_url" => ""} + hash.each { |key, value| + EduSetting.find_or_create_by(name: key, value: value) + } + end +end