discourse/lib/tasks/cdn.rake

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-26 00:42:20 +08:00
# cdn related tasks
2013-02-06 03:16:51 +08:00
#
desc "pre-stage assets on cdn"
task "assets:prestage" => :environment do |t|
require "net/https"
require "uri"
2013-02-26 00:42:20 +08:00
2013-03-20 09:12:19 +08:00
def get_assets(path)
Dir
.glob("#{Rails.root}/public/assets/#{path}*")
2013-03-20 09:12:19 +08:00
.map { |f| "/assets/#{path}#{f.split("/")[-1]}" if f =~ /[a-f0-9]{16}\.(css|js)$/ }
.compact
end
2013-02-06 03:16:51 +08:00
# pre-stage css/js only for now
assets = get_assets("locales/") + get_assets("")
2013-04-11 19:31:56 +08:00
puts "pre staging: #{assets.join(" ")}"
2013-03-20 09:12:19 +08:00
# makes testing simpler leaving this here
2021-10-27 16:39:28 +08:00
config = YAML.safe_load(File.open("#{Rails.root}/config/cdn.yml"))
2013-03-20 09:12:19 +08:00
2013-02-06 03:16:51 +08:00
start = Time.now
2013-02-26 00:42:20 +08:00
uri = URI.parse("https://client.cdn77.com/api/prefetch")
2013-02-06 03:16:51 +08:00
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
failed_assets = []
2013-02-06 03:16:51 +08:00
request = Net::HTTP::Post.new(uri.request_uri)
assets.each do |asset|
request.set_form_data(
"id" => config["id"],
"login" => config["login"],
"passwd" => config["password"],
"json" => { "prefetch_paths" => asset }.to_json,
)
2013-02-06 03:16:51 +08:00
response = http.request(request)
json = JSON.parse(response.body)
failed_assets.push(asset) if json["status"] != "ok"
end
2017-07-28 09:20:09 +08:00
if failed_assets.length > 0
raise "Failed to pre-stage #{failed_assets.length}/#{assets.length} files"
2013-02-06 03:16:51 +08:00
end
puts "Done (took: #{((Time.now - start) * 1000.0).to_i}ms)"
end