diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-01-11 16:58:14 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-01-11 16:58:14 +0900 |
commit | 8bf1a437668427a9da918ae53b39e01b1d505dd1 (patch) | |
tree | a89c3d45b88ebaead0deb8246883c625301133d0 | |
parent | 4499881cb5e61879ca6dd758c445804486d58637 (diff) | |
download | poe-8bf1a437668427a9da918ae53b39e01b1d505dd1.tar.gz |
improve output style
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 6 | ||||
-rw-r--r-- | app/controllers/results_controller.rb | 2 | ||||
-rw-r--r-- | app/models/compiler.rb | 21 | ||||
-rw-r--r-- | app/models/result.rb | 23 | ||||
-rw-r--r-- | app/models/snippet.rb | 7 | ||||
-rw-r--r-- | app/views/results/_result.html.haml | 2 | ||||
-rw-r--r-- | db/migrate/20160111053610_add_error_to_result.rb | 5 | ||||
-rw-r--r-- | db/migrate/20160111074143_add_truncated_to_result.rb | 5 | ||||
-rw-r--r-- | db/schema.rb | 16 |
10 files changed, 69 insertions, 19 deletions
@@ -16,6 +16,7 @@ gem "bootstrap-sass" gem "codemirror-rails" gem "plum" +gem "thin" group :development, :test do gem "byebug" diff --git a/Gemfile.lock b/Gemfile.lock index 4adfe9c..5378c0f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,6 +86,7 @@ GEM coffee-script-source (1.10.0) concurrent-ruby (1.0.0) connection_pool (2.2.0) + daemons (1.2.3) debug_inspector (0.0.2) em-hiredis (0.3.0) eventmachine (~> 1.0) @@ -178,6 +179,10 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + thin (1.6.2) + daemons (>= 1.0.9) + eventmachine (>= 1.0.0) + rack (>= 1.0.0) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.2) @@ -214,6 +219,7 @@ DEPENDENCIES sinatra sprockets sprockets-es6 + thin uglifier (>= 1.3.0) web-console (~> 3.0) diff --git a/app/controllers/results_controller.rb b/app/controllers/results_controller.rb index 60ade5a..d5066f9 100644 --- a/app/controllers/results_controller.rb +++ b/app/controllers/results_controller.rb @@ -10,7 +10,7 @@ class ResultsController < ApplicationController end def show - render text: render_to_string(partial: "results/result", locals: { result: @result, compiler: @result.compiler }), content_type: "text/plain" + render plain: render_to_string(partial: "results/result", locals: { result: @result, compiler: @result.compiler }) end private diff --git a/app/models/compiler.rb b/app/models/compiler.rb index 0c27054..2d819e5 100644 --- a/app/models/compiler.rb +++ b/app/models/compiler.rb @@ -25,25 +25,34 @@ class Compiler < ApplicationRecord sf.write(snippet.code) sf.fsync of = Tempfile.open + ef = Tempfile.open pid = spawn(Rails.root.join("sandbox/safe_runner").to_s, baseroot, env_overlay, sf.path, *Shellwords.split(command_line), in: :close, # TODO out: of, - err: STDERR) + err: ef) _, pst = Process.waitpid2(pid) + ef.rewind + err = ef.read.force_encoding(Encoding::BINARY) if pst.signaled? || pst.exitstatus > 0 result = :errored status = -1 + Logger.error(err) + err = nil else - result = :success - status = 0 + rx, status = err.slice!(0, 8).unpack("ii") + result = rx == 0 ? :success : :failed end of.rewind - r.update!(output: of.read, + output = of.read(65535) + r.update!(output: output, + truncated: !of.eof?, status: status, - result: result) - rescue + result: result, + error: err) + rescue => e r.update!(status: -1, result: :errored) + raise e ensure sf.close if sf of.close if of diff --git a/app/models/result.rb b/app/models/result.rb index 1bbdb29..7ce874e 100644 --- a/app/models/result.rb +++ b/app/models/result.rb @@ -15,20 +15,37 @@ class Result < ApplicationRecord ret = [] while orig.bytesize > 0 fd, len = orig.slice!(0, 5).unpack("CV") - raise "output is too short" if !len || orig.bytesize < len + if !truncated? && (!len || orig.bytesize < len) + raise "output is too short" + end ret << [fd, orig.slice!(0, len)] end ret end def formatted_output - parse_output.inject("".b) { |s, (fd, c)| + s = "".b + last_c = nil + parse_output.each { |fd, c| if fd == 1 s << CGI.escapeHTML(c) else s << "<span style='color: red'>" << CGI.escapeHTML(c) << "</span>" end - }.html_safe + last_c = c + } + + if truncated? + s << "<span style='color: white; background-color: black'>#truncated#</span>" + elsif last_c && last_c[-1] != "\n" + s << "<span style='color: white; background-color: black'>%</span>" + end + + if error.present? + s << "<span style='color: #6666ff; font-style: bold'>" << CGI.escapeHTML(error) << "</span>" + end + + s.html_safe end def self.prepare_execution(compiler, snippet) diff --git a/app/models/snippet.rb b/app/models/snippet.rb index a55b732..e3d4c63 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -1,7 +1,7 @@ class Snippet < ApplicationRecord has_many :results - validates :code, length: { maximum: 65535, tokenizer: :bytes.to_proc } + validates :code_bytes, length: { maximum: 65535 } def compilers Compiler.where(language: language) @@ -11,4 +11,9 @@ class Snippet < ApplicationRecord r = results.map { |r| [r.compiler_id, r] }.to_h compilers.map { |c| [r[c.id], c] } end + + private + def code_bytes + code.bytes + end end diff --git a/app/views/results/_result.html.haml b/app/views/results/_result.html.haml index 0f4c8cd..6cb837f 100644 --- a/app/views/results/_result.html.haml +++ b/app/views/results/_result.html.haml @@ -7,7 +7,7 @@ %div Status: #{result.status} - else - .result-item.panel.panel-default{"data-compiler-id": compiler.id, "data-id": result.id, "data-status": (result ? "running" : "notran")} + .result-item.panel.panel-default{"data-compiler-id": compiler.id, "data-id": result && result.id, "data-status": (result ? "running" : "notran")} .panel-heading= compiler .panel-body %p running.... diff --git a/db/migrate/20160111053610_add_error_to_result.rb b/db/migrate/20160111053610_add_error_to_result.rb new file mode 100644 index 0000000..26af807 --- /dev/null +++ b/db/migrate/20160111053610_add_error_to_result.rb @@ -0,0 +1,5 @@ +class AddErrorToResult < ActiveRecord::Migration[5.0] + def change + add_column :results, :error, :string, null: true + end +end diff --git a/db/migrate/20160111074143_add_truncated_to_result.rb b/db/migrate/20160111074143_add_truncated_to_result.rb new file mode 100644 index 0000000..4f172ff --- /dev/null +++ b/db/migrate/20160111074143_add_truncated_to_result.rb @@ -0,0 +1,5 @@ +class AddTruncatedToResult < ActiveRecord::Migration[5.0] + def change + add_column :results, :truncated, :boolean, null: false, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index f60b7ab..d84c0ee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160101121151) do +ActiveRecord::Schema.define(version: 20160111074143) do create_table "compilers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t| t.string "language", limit: 64, null: false @@ -23,12 +23,14 @@ ActiveRecord::Schema.define(version: 20160101121151) do end create_table "results", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t| - t.integer "snippet_id", null: false - t.integer "compiler_id", null: false - t.binary "output", limit: 65535, null: false - t.datetime "created_at", null: false - t.integer "result", null: false - t.integer "status", null: false + t.integer "snippet_id", null: false + t.integer "compiler_id", null: false + t.binary "output", limit: 65535, null: false + t.datetime "created_at", null: false + t.integer "result", null: false + t.integer "status", null: false + t.string "error" + t.boolean "truncated", default: false, null: false t.index ["compiler_id"], name: "index_results_on_compiler_id", using: :btree t.index ["snippet_id", "compiler_id"], name: "index_results_on_snippet_id_and_compiler_id", unique: true, using: :btree t.index ["snippet_id"], name: "index_results_on_snippet_id", using: :btree |