| @@ -9,8 +9,9 @@ module ApplicationCable | |||||
| private | private | ||||
| def find_verified_user | def find_verified_user | ||||
| puts "############### cookies.signed[:signed_user_id]: #{cookies.signed[:user_id]}" | |||||
| puts "############### cookies.signed[:user_id]: #{cookies.signed[:user_id]}" | |||||
| if current_user = User.find_by(id: cookies.signed[:user_id]) | if current_user = User.find_by(id: cookies.signed[:user_id]) | ||||
| puts "############### find_verified_user success! ###############" | |||||
| current_user | current_user | ||||
| else | else | ||||
| reject_unauthorized_connection | reject_unauthorized_connection | ||||
| @@ -1,7 +1,6 @@ | |||||
| class MirrorProjectChannel < ApplicationCable::Channel | class MirrorProjectChannel < ApplicationCable::Channel | ||||
| def subscribed | def subscribed | ||||
| Rails.logger.info "################### channel params: #{params}" | Rails.logger.info "################### channel params: #{params}" | ||||
| # @project = Project.find_by_identifier params[:id] | |||||
| stream_from "channel_room_#{params[:id]}" | stream_from "channel_room_#{params[:id]}" | ||||
| end | end | ||||
| @@ -0,0 +1,29 @@ | |||||
| class BroadcastMirrorRepoMsgJob < ApplicationJob | |||||
| queue_as :default | |||||
| def perform(repo_id) | |||||
| puts "############ BroadcastMirrorRepoMsgJob start ############ #{repo_id}" | |||||
| repo = Repository.find_by(id: repo_id) | |||||
| return if repo.blank? | |||||
| project = repo.project | |||||
| json_data = { | |||||
| mirror_status: repo.mirror_status, | |||||
| mirror_num: repo.mirror_num, | |||||
| mirror_url: repo.mirror_url, | |||||
| first_sync: repo.first_sync?, | |||||
| identifier: repo.identifier, | |||||
| name: project.name, | |||||
| id: project.id, | |||||
| type: project.numerical_for_project_type | |||||
| } | |||||
| puts "############ broadcast start.......... " | |||||
| puts "############ broadcast channel_name: channel_room_#{project.id}" | |||||
| puts "############ broadcast project data: #{json_data} " | |||||
| cable_result = ActionCable.server.broadcast "channel_room_#{project.id}", project: json_data | |||||
| puts "############ broadcast result: #{cable_result == 1 ? 'successed' : 'failed'} " | |||||
| end | |||||
| end | |||||
| @@ -10,24 +10,8 @@ class MigrateRemoteRepositoryJob < ApplicationJob | |||||
| gitea_repository = Gitea::Repository::MigrateService.new(token, params).call | gitea_repository = Gitea::Repository::MigrateService.new(token, params).call | ||||
| if gitea_repository | if gitea_repository | ||||
| repo&.project&.update_columns(gpid: gitea_repository["id"]) | repo&.project&.update_columns(gpid: gitea_repository["id"]) | ||||
| repo&.mirror&.update_columns(status: Mirror.statuses[:succeeded]) | |||||
| project = repo.project | |||||
| json_data = { | |||||
| mirror_status: repo.mirror_status, | |||||
| mirror_num: repo.mirror_num, | |||||
| mirror_url: repo.mirror_url, | |||||
| first_sync: repo.first_sync?, | |||||
| identifier: repo.identifier, | |||||
| name: project.name, | |||||
| id: project.id, | |||||
| type: project.numerical_for_project_type | |||||
| } | |||||
| puts "############ broadcast start.......... ############" | |||||
| cable_result = ActionCable.server.broadcast "channel_room_#{repo.identifier}", project: json_data | |||||
| puts "############ room_channel_#{repo.identifier} result : #{cable_result}" | |||||
| repo&.mirror&.succeeded! | |||||
| puts "############ mirror status: #{repo.mirror.status} ############" | |||||
| end | end | ||||
| end | end | ||||
| end | end | ||||
| @@ -4,8 +4,9 @@ class Mirror < ApplicationRecord | |||||
| # 0: 同步镜像成功;1: 正在同步镜像;2: 同步失败; 默认值为0 | # 0: 同步镜像成功;1: 正在同步镜像;2: 同步失败; 默认值为0 | ||||
| enum status: { succeeded: 0, waiting: 1, failed: 2 } | enum status: { succeeded: 0, waiting: 1, failed: 2 } | ||||
| belongs_to :repository, foreign_key: :repo_id | |||||
| after_update :websocket_boardcast, if: :saved_change_to_status? | |||||
| belongs_to :repository, foreign_key: :repo_id | |||||
| def set_status!(status=Mirror.statuses[:succeeded]) | def set_status!(status=Mirror.statuses[:succeeded]) | ||||
| update_column(:status, status) | update_column(:status, status) | ||||
| @@ -14,4 +15,9 @@ class Mirror < ApplicationRecord | |||||
| def numerical_for_status | def numerical_for_status | ||||
| self.class.name.constantize.statuses["#{self.status}"] | self.class.name.constantize.statuses["#{self.status}"] | ||||
| end | end | ||||
| private | |||||
| def websocket_boardcast | |||||
| BroadcastMirrorRepoMsgJob.perform_later(self.repository.id) | |||||
| end | |||||
| end | end | ||||