ADD sync repo updated at time

(cherry picked from commit 8c6df359a5)
This commit is contained in:
Jasder 2021-01-08 18:07:48 +08:00 committed by moshenglv
parent f4dc82f7d5
commit 8c8d9f64c4
6 changed files with 54 additions and 12 deletions

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

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

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

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|

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
config/schedule.yml Normal file
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