mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
new image screenshot system for #194
This commit is contained in:
parent
06778925e0
commit
df917f3791
3 changed files with 93 additions and 60 deletions
4
Gemfile
4
Gemfile
|
@ -16,7 +16,7 @@ gem 'google-api-client', require: 'google/api_client'
|
||||||
gem 'tilt'
|
gem 'tilt'
|
||||||
gem 'erubis'
|
gem 'erubis'
|
||||||
gem 'stripe', '1.15.0' #, source: 'https://code.stripe.com/'
|
gem 'stripe', '1.15.0' #, source: 'https://code.stripe.com/'
|
||||||
gem 'screencap', '~> 0.1.4'
|
#gem 'screencap', '~> 0.1.4'
|
||||||
gem 'cocaine'
|
gem 'cocaine'
|
||||||
gem 'zipruby'
|
gem 'zipruby'
|
||||||
gem 'sass', require: nil
|
gem 'sass', require: nil
|
||||||
|
@ -38,7 +38,7 @@ platform :mri, :rbx do
|
||||||
gem 'pg'
|
gem 'pg'
|
||||||
gem 'sequel_pg', require: nil
|
gem 'sequel_pg', require: nil
|
||||||
gem 'hiredis'
|
gem 'hiredis'
|
||||||
gem 'rainbows', require: nil
|
gem 'posix-spawn'
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
gem 'pry'
|
gem 'pry'
|
||||||
|
|
56
files/phantomjs_screenshot.js
Normal file
56
files/phantomjs_screenshot.js
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
var page = require('webpage').create()
|
||||||
|
var system = require('system')
|
||||||
|
|
||||||
|
var maxTimeout = 15000
|
||||||
|
|
||||||
|
if (system.args.length === 1) {
|
||||||
|
console.log('required args: <siteURL> <outputFilePath>');
|
||||||
|
phantom.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
var address = system.args[1]
|
||||||
|
var outputPath = system.args[2]
|
||||||
|
|
||||||
|
page.viewportSize = { width: 1280, height: 960 }
|
||||||
|
page.clipRect = { top: 0, left: 0, width: 1280, height: 960}
|
||||||
|
|
||||||
|
/*
|
||||||
|
In development, not working yet.
|
||||||
|
|
||||||
|
page.settings.scriptTimeout = 1000
|
||||||
|
|
||||||
|
page.onLongRunningScript = function() {
|
||||||
|
page.stopJavaScript()
|
||||||
|
phantom.exit(4)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
var t = Date.now()
|
||||||
|
|
||||||
|
console.log('Loading ' + address)
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
console.log('timeout')
|
||||||
|
phantom.exit(62)
|
||||||
|
}, maxTimeout)
|
||||||
|
|
||||||
|
page.settings.resourceTimeout = maxTimeout
|
||||||
|
|
||||||
|
page.onResourceTimeout = function(e) {
|
||||||
|
console.log(e.errorCode)
|
||||||
|
console.log(e.errorString)
|
||||||
|
console.log(e.url)
|
||||||
|
phantom.exit(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
page.open(address, function(status) {
|
||||||
|
if(status !== 'success') {
|
||||||
|
console.log('failed')
|
||||||
|
phantom.exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
page.render(outputPath)
|
||||||
|
console.log('Loading time ' + (Date.now() - t) + ' msec');
|
||||||
|
phantom.exit()
|
||||||
|
})
|
||||||
|
|
|
@ -4,32 +4,9 @@ require 'securerandom'
|
||||||
require 'thread'
|
require 'thread'
|
||||||
require 'open3'
|
require 'open3'
|
||||||
|
|
||||||
# Don't judge - Ruby handling of timeouts is a joke..
|
|
||||||
module Phantomjs
|
|
||||||
def self.run(*args, &block)
|
|
||||||
pid = nil
|
|
||||||
stdin, stdout, stderr, wait_thr = nil
|
|
||||||
begin
|
|
||||||
Timeout::timeout(30) do
|
|
||||||
stdin, stdout, stderr, wait_thr = Open3.popen3(path, *args)
|
|
||||||
pid = wait_thr.pid
|
|
||||||
wait_thr.join
|
|
||||||
return stdout.read
|
|
||||||
end
|
|
||||||
# :nocov:
|
|
||||||
rescue Timeout::Error
|
|
||||||
stdin.close
|
|
||||||
stdout.close
|
|
||||||
stderr.close
|
|
||||||
Process.kill 'QUIT', pid
|
|
||||||
raise Timeout::Error
|
|
||||||
end
|
|
||||||
# :nocov:
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ScreenshotWorker
|
class ScreenshotWorker
|
||||||
SCREENSHOTS_PATH = Site::SCREENSHOTS_ROOT
|
SCREENSHOTS_PATH = Site::SCREENSHOTS_ROOT
|
||||||
|
HARD_TIMEOUT = 20.freeze
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
sidekiq_options queue: :screenshots, retry: 3, backtrace: true
|
sidekiq_options queue: :screenshots, retry: 3, backtrace: true
|
||||||
|
|
||||||
|
@ -39,42 +16,43 @@ class ScreenshotWorker
|
||||||
screenshot.close
|
screenshot.close
|
||||||
screenshot_output_path = screenshot.path+'.png'
|
screenshot_output_path = screenshot.path+'.png'
|
||||||
|
|
||||||
begin
|
line = Cocaine::CommandLine.new(
|
||||||
f = Screencap::Fetcher.new("http://#{username}.neocities.org#{path}")
|
"timeout #{HARD_TIMEOUT} phantomjs #{File.join DIR_ROOT, 'files', 'phantomjs_screenshot.js'}", ":url :output",
|
||||||
f.fetch(
|
expected_outcodes: [0]
|
||||||
output: screenshot_output_path,
|
)
|
||||||
width: 1280,
|
|
||||||
height: 960,
|
|
||||||
maxRenderWait: 25000,
|
|
||||||
cutoffWait: 30000
|
|
||||||
)
|
|
||||||
rescue Timeout::Error
|
|
||||||
# :nocov:
|
|
||||||
puts "#{username}/#{path} is timing out, discontinuing"
|
|
||||||
site = Site[username: username]
|
|
||||||
site.is_crashing = true
|
|
||||||
site.save_changes validate: false
|
|
||||||
|
|
||||||
# Don't enable until we know it works well.
|
begin
|
||||||
|
output = line.run(
|
||||||
|
url: "http://#{username}.neocities.org#{path}",
|
||||||
|
output: screenshot_output_path
|
||||||
|
)
|
||||||
|
rescue Cocaine::ExitStatusError => e
|
||||||
|
if e.message && e.message.match(/returned 124/)
|
||||||
|
puts "#{username}/#{path} is timing out, discontinuing"
|
||||||
|
site = Site[username: username]
|
||||||
|
site.is_crashing = true
|
||||||
|
site.save_changes validate: false
|
||||||
|
return true
|
||||||
=begin
|
=begin
|
||||||
if site.email
|
if site.email
|
||||||
EmailWorker.perform_async({
|
EmailWorker.perform_async({
|
||||||
from: 'web@neocities.org',
|
from: 'web@neocities.org',
|
||||||
to: site.email,
|
to: site.email,
|
||||||
subject: "[NeoCities] The web page \"#{path}\" on your site (#{username}.neocities.org) is slow",
|
subject: "[NeoCities] The web page \"#{path}\" on your site (#{username}.neocities.org) is slow",
|
||||||
body: "Hi there! This is an automated email to inform you that we're having issues loading your site to take a "+
|
body: "Hi there! This is an automated email to inform you that we're having issues loading your site to take a "+
|
||||||
"screenshot. It is possible that this is an error specific to our screenshot program, but it is much more "+
|
"screenshot. It is possible that this is an error specific to our screenshot program, but it is much more "+
|
||||||
"likely that your site is too slow to be used with browsers. We don't want Neocities sites crashing browsers, "+
|
"likely that your site is too slow to be used with browsers. We don't want Neocities sites crashing browsers, "+
|
||||||
"so we're taking steps to inform you and see if you can resolve the issue. "+
|
"so we're taking steps to inform you and see if you can resolve the issue. "+
|
||||||
"We may have to de-list your web site from being viewable in our browse page if it is not resolved shortly. "+
|
"We may have to de-list your web site from being viewable in our browse page if it is not resolved shortly. "+
|
||||||
"We will review the site manually before taking this step, so don't worry if your site is fine and we made "+
|
"We will review the site manually before taking this step, so don't worry if your site is fine and we made "+
|
||||||
"a mistake."+
|
"a mistake."+
|
||||||
"\n\nOur best,\n- Neocities"
|
"\n\nOur best,\n- Neocities"
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
=end
|
=end
|
||||||
return
|
else
|
||||||
# :nocov:
|
raise
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
img_list = Magick::ImageList.new
|
img_list = Magick::ImageList.new
|
||||||
|
@ -99,7 +77,6 @@ class ScreenshotWorker
|
||||||
else
|
else
|
||||||
new_img = img.scale width, height
|
new_img = img.scale width, height
|
||||||
end
|
end
|
||||||
|
|
||||||
new_img.write(File.join(user_screenshots_path, "#{path}.#{res}.jpg")) {
|
new_img.write(File.join(user_screenshots_path, "#{path}.#{res}.jpg")) {
|
||||||
self.quality = 90
|
self.quality = 90
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue