Browse Source

ADD sync repo updated at time

(cherry picked from commit 8c6df359a5)
tags/v3.0.1
Jasder moshenglv 5 years ago
parent
commit
8c8d9f64c4
6 changed files with 54 additions and 12 deletions
  1. +25
    -0
      app/jobs/sync_repo_update_time_job.rb
  2. +5
    -0
      app/models/project.rb
  3. +13
    -12
      app/services/gitea/repository/get_by_id_service.rb
  4. +5
    -0
      config/initializers/sidekiq.rb
  5. +1
    -0
      config/routes.rb
  6. +5
    -0
      config/schedule.yml

+ 25
- 0
app/jobs/sync_repo_update_time_job.rb View File

@@ -0,0 +1,25 @@
class SyncRepoUpdateTimeJob < ApplicationJob
queue_as :default

def perform(*args)
# Do something later
Project.forge.find_each do |project|
update_repo_time!(project)
end
end

private
def gitea_repo_updated_at(project)
admin = User.where(admin: true).select(:id, :gitea_token, :gitea_uid).last

return nil if project.gpid.blank?

result = Gitea::Repository::GetByIdService.call(project.gpid, admin.gitea_token)

result[:status] === :success ? result[:body]['updated_at'] : nil
end

def update_repo_time!(project)
project.set_updated_on gitea_repo_updated_at(project)
end
end

+ 5
- 0
app/models/project.rb View File

@@ -279,4 +279,9 @@ class Project < ApplicationRecord
ps.increment!(:mirror_projects_count) unless ps.blank?
end
def set_updated_on(time)
return if time.blank?
update_column(:updated_on, time)
end
end

+ 13
- 12
app/services/gitea/repository/get_by_id_service.rb View File

@@ -1,31 +1,32 @@
class Gitea::Repository::GetByIdService < Gitea::ClientService
attr_reader :owner, :repo_id
attr_reader :token, :id

def initialize(owner, repo_id)
@owner = owner
@repo_id = repo_id
def initialize(id, token=nil)
@token = token
@id = id
end

def call
response = get(url, params)
render_result(response)

status, message, body = render_response(response)
json_format(status, message, body)
end

private
def params
Hash.new.merge(token: owner.gitea_token)
Hash.new.merge(token: token)
end

def url
"/repositories/#{repo_id}".freeze
"/repositories/#{id}".freeze
end

def render_result(response)
case response.status
when 200
JSON.parse(response.body)
def json_format(status, message, body)
case status
when 200 then success(body)
else
nil
error(message, status)
end
end
end

+ 5
- 0
config/initializers/sidekiq.rb View File

@@ -3,6 +3,11 @@ sidekiq_url = redis_config["url"]
Sidekiq.configure_server do |config|
config.redis = { url: sidekiq_url }
schedule_file = "config/schedule.yml"
if File.exists?(schedule_file)
Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
end
end
Sidekiq.configure_client do |config|


+ 1
- 0
config/routes.rb View File

@@ -1,6 +1,7 @@
Rails.application.routes.draw do
require 'sidekiq/web'
require 'sidekiq/cron/web'
require 'admin_constraint'
# mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new


+ 5
- 0
config/schedule.yml View File

@@ -0,0 +1,5 @@
sync_gitea_repo_updated_at:
# second minute hour day month date
cron: "0 0 24 * *"
class: "SyncRepoUpdateTimeJob"
queue: default

Loading…
Cancel
Save