ADD action cable for migrate project

This commit is contained in:
Jasder 2020-07-02 15:49:22 +08:00
parent ba79b8f4e6
commit c1a5c390f7
14 changed files with 100 additions and 11 deletions

View File

@ -0,0 +1,13 @@
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);

View File

@ -0,0 +1,13 @@
App.mirror_project = App.cable.subscriptions.create("MirrorProjectChannel", {
connected: function() {
// Called when the subscription is ready for use on the server
},
disconnected: function() {
// Called when the subscription has been terminated by the server
},
received: function(data) {
// Called when there's incoming data on the websocket for this channel
}
});

View File

@ -1,4 +1,20 @@
module ApplicationCable module ApplicationCable
class Connection < ActionCable::Connection::Base class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', current_user.id
end
private
def find_verified_user
puts "############### cookies.signed[:signed_user_id]: #{cookies.signed[:user_id]}"
if current_user = User.find_by(id: cookies.signed[:user_id])
current_user
else
reject_unauthorized_connection
end
end
end end
end end

View File

@ -0,0 +1,12 @@
class MirrorProjectChannel < ApplicationCable::Channel
def subscribed
Rails.logger.info "################### channel params: #{params}"
# @project = Project.find_by_identifier params[:id]
stream_from "channel_room_#{params[:id]}"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
Rails.logger.info "################### unsubscribed ################### "
end
end

View File

@ -269,7 +269,9 @@ class AccountsController < ApplicationController
cookie_options = cookie_options.merge(domain: edu_setting('cookie_domain')) cookie_options = cookie_options.merge(domain: edu_setting('cookie_domain'))
end end
cookies[autologin_cookie_name] = cookie_options cookies[autologin_cookie_name] = cookie_options
logger.info("cookies is #{cookies}") cookies.signed[:user_id] ||= user.id
logger.info("cookies is #{cookies} ======> #{cookies.signed[:user_id]} =====> #{cookies[autologin_cookie_name]}")
end end
def logout def logout

View File

@ -342,7 +342,9 @@ class ApplicationController < ActionController::Base
elsif params[:debug] == 'student' elsif params[:debug] == 'student'
User.current = User.find 8686 User.current = User.find 8686
elsif params[:debug] == 'admin' elsif params[:debug] == 'admin'
User.current = User.find 1 user = User.find 1
User.current = user
cookies.signed[:user_id] = user.id
end end
end end
# User.current = User.find 81403 # User.current = User.find 81403
@ -386,7 +388,6 @@ class ApplicationController < ActionController::Base
else else
User.current User.current
end end
# User.current
end end
## 默认输出json ## 默认输出json

View File

@ -24,8 +24,10 @@ module LoginHelper
unless cookies[autologin_cookie_name].present? unless cookies[autologin_cookie_name].present?
cookies[autologin_cookie_name] = cookie_options cookies[autologin_cookie_name] = cookie_options
end end
# for action cable
cookies.signed[:user_id] ||= user.id
Rails.logger.info("cookies is #{cookies}") Rails.logger.info("cookies is #{cookies} ======> #{cookies.signed[:user_id]}")
end end
def successful_authentication(user) def successful_authentication(user)

View File

@ -2,14 +2,32 @@ class MigrateRemoteRepositoryJob < ApplicationJob
queue_as :default queue_as :default
def perform(repo_id, token, params) def perform(repo_id, token, params)
puts "############ perform: repo_id: #{repo_id}, token: #{token}, params: #{params}}"
repo = Repository.find_by(id: repo_id) repo = Repository.find_by(id: repo_id)
return if repo.blank? return if repo.blank?
puts "############ MigrateRemoteRepositoryJob starting ... ############"
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]) 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}"
end end
end end
end end

View File

@ -29,7 +29,6 @@ class Gitea::Repository::Entries::ListService < Gitea::ClientService
when 200 when 200
body body
else else
Rails.logger.info("########__________has_error_______##########{body['message']}")
[] []
end end
end end

2
bin/cable Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
bundle exec puma -p 28080 cable/config.ru

4
cable/config.ru Normal file
View File

@ -0,0 +1,4 @@
require_relative '../config/environment'
Rails.application.eager_load!
run ActionCable.server

View File

@ -29,6 +29,9 @@ module Educoderplus
# job # job
config.active_job.queue_adapter = :sidekiq config.active_job.queue_adapter = :sidekiq
# disable actioncable development nend true
# config.action_cable.disable_request_forgery_protection = true
config.middleware.use OmniAuth::Builder do config.middleware.use OmniAuth::Builder do
provider :cas, url: 'https://urp.tfswufe.edu.cn/cas' provider :cas, url: 'https://urp.tfswufe.edu.cn/cas'
end end

View File

@ -1,10 +1,12 @@
development: development:
adapter: async adapter: redis
url: redis://localhost:6379
test: test:
adapter: async adapter: redis
url: redis://localhost:6379
production: production:
adapter: redis adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379" } %>
channel_prefix: educoderplus_production channel_prefix: forgeplus_production

View File

@ -2,10 +2,12 @@ Rails.application.routes.draw do
require 'sidekiq/web' require 'sidekiq/web'
require 'admin_constraint' require 'admin_constraint'
# mount Sidekiq::Web => '/sidekiq'
mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new
# Serve websocket cable requests in-process
mount ActionCable.server => '/cable'
get 'attachments/download/:id', to: 'attachments#show' get 'attachments/download/:id', to: 'attachments#show'
get 'attachments/download/:id/:filename', to: 'attachments#show' get 'attachments/download/:id/:filename', to: 'attachments#show'
get 'auth/qq/callback', to: 'oauth/qq#create' get 'auth/qq/callback', to: 'oauth/qq#create'