Browse Source

ADD action cable for migrate project

tags/v1
Jasder 6 years ago
parent
commit
c1a5c390f7
14 changed files with 100 additions and 11 deletions
  1. +13
    -0
      app/assets/javascripts/cable.js
  2. +13
    -0
      app/assets/javascripts/channels/mirror_project.js
  3. +16
    -0
      app/channels/application_cable/connection.rb
  4. +12
    -0
      app/channels/mirror_project_channel.rb
  5. +3
    -1
      app/controllers/accounts_controller.rb
  6. +3
    -2
      app/controllers/application_controller.rb
  7. +3
    -1
      app/controllers/concerns/login_helper.rb
  8. +19
    -1
      app/jobs/migrate_remote_repository_job.rb
  9. +0
    -1
      app/services/gitea/repository/entries/list_service.rb
  10. +2
    -0
      bin/cable
  11. +4
    -0
      cable/config.ru
  12. +3
    -0
      config/application.rb
  13. +6
    -4
      config/cable.yml
  14. +3
    -1
      config/routes.rb

+ 13
- 0
app/assets/javascripts/cable.js 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);

+ 13
- 0
app/assets/javascripts/channels/mirror_project.js 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
}
});

+ 16
- 0
app/channels/application_cable/connection.rb View File

@@ -1,4 +1,20 @@
module ApplicationCable
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

+ 12
- 0
app/channels/mirror_project_channel.rb 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

+ 3
- 1
app/controllers/accounts_controller.rb View File

@@ -269,7 +269,9 @@ class AccountsController < ApplicationController
cookie_options = cookie_options.merge(domain: edu_setting('cookie_domain'))
end
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
def logout


+ 3
- 2
app/controllers/application_controller.rb View File

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

## 默认输出json


+ 3
- 1
app/controllers/concerns/login_helper.rb View File

@@ -24,8 +24,10 @@ module LoginHelper
unless cookies[autologin_cookie_name].present?
cookies[autologin_cookie_name] = cookie_options
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

def successful_authentication(user)


+ 19
- 1
app/jobs/migrate_remote_repository_job.rb View File

@@ -2,14 +2,32 @@ class MigrateRemoteRepositoryJob < ApplicationJob
queue_as :default

def perform(repo_id, token, params)
puts "############ perform: repo_id: #{repo_id}, token: #{token}, params: #{params}}"
repo = Repository.find_by(id: repo_id)
return if repo.blank?

puts "############ MigrateRemoteRepositoryJob starting ... ############"

gitea_repository = Gitea::Repository::MigrateService.new(token, params).call
if gitea_repository
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}"
end
end
end

+ 0
- 1
app/services/gitea/repository/entries/list_service.rb View File

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


+ 2
- 0
bin/cable View File

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

+ 4
- 0
cable/config.ru View File

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

run ActionCable.server

+ 3
- 0
config/application.rb View File

@@ -29,6 +29,9 @@ module Educoderplus
# job
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
provider :cas, url: 'https://urp.tfswufe.edu.cn/cas'
end


+ 6
- 4
config/cable.yml View File

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

+ 3
- 1
config/routes.rb View File

@@ -2,10 +2,12 @@ Rails.application.routes.draw do
require 'sidekiq/web'
require 'admin_constraint'
# mount Sidekiq::Web => '/sidekiq'
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/:filename', to: 'attachments#show'
get 'auth/qq/callback', to: 'oauth/qq#create'


Loading…
Cancel
Save