ADD sync repo updated at time

This commit is contained in:
Jasder 2021-01-08 18:07:48 +08:00
parent 8f24f65820
commit 8c6df359a5
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? ps.increment!(:mirror_projects_count) unless ps.blank?
end end
def set_updated_on(time)
return if time.blank?
update_column(:updated_on, time)
end
end end

View File

@ -1,31 +1,32 @@
class Gitea::Repository::GetByIdService < Gitea::ClientService class Gitea::Repository::GetByIdService < Gitea::ClientService
attr_reader :owner, :repo_id attr_reader :token, :id
def initialize(owner, repo_id) def initialize(id, token=nil)
@owner = owner @token = token
@repo_id = repo_id @id = id
end end
def call def call
response = get(url, params) response = get(url, params)
render_result(response)
status, message, body = render_response(response)
json_format(status, message, body)
end end
private private
def params def params
Hash.new.merge(token: owner.gitea_token) Hash.new.merge(token: token)
end end
def url def url
"/repositories/#{repo_id}".freeze "/repositories/#{id}".freeze
end end
def render_result(response) def json_format(status, message, body)
case response.status case status
when 200 when 200 then success(body)
JSON.parse(response.body)
else else
nil error(message, status)
end end
end end
end end

View File

@ -3,6 +3,11 @@ sidekiq_url = redis_config["url"]
Sidekiq.configure_server do |config| Sidekiq.configure_server do |config|
config.redis = { url: sidekiq_url } 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 end
Sidekiq.configure_client do |config| Sidekiq.configure_client do |config|

View File

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