From d10735d0a832722f5ad297baaef7c694d4ecd450 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 15 Apr 2022 15:16:57 +0200 Subject: [PATCH] Persist Execution Environment when running code --- app/controllers/submissions_controller.rb | 3 ++- app/models/execution_environment.rb | 1 + app/models/submission.rb | 7 +++++-- app/models/testrun.rb | 1 + app/models/testrun_execution_environment.rb | 6 ++++++ ...20415125948_add_testrun_execution_environment.rb | 12 ++++++++++++ db/schema.rb | 13 ++++++++++++- 7 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 app/models/testrun_execution_environment.rb create mode 100644 db/migrate/20220415125948_add_testrun_execution_environment.rb diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 83fefdb4..18997160 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -293,7 +293,7 @@ class SubmissionsController < ApplicationController # save the output of this "run" as a "testrun" (scoring runs are saved in submission.rb) def save_run_output - Testrun.create( + testrun = Testrun.create( file: @file, cause: 'run', submission: @submission, @@ -301,6 +301,7 @@ class SubmissionsController < ApplicationController container_execution_time: @container_execution_time, waiting_for_container_time: @waiting_for_container_time ) + TestrunExecutionEnvironment.create(testrun: testrun, execution_environment: @submission.used_execution_environment) end def send_hints(tubesock, errors) diff --git a/app/models/execution_environment.rb b/app/models/execution_environment.rb index 91c62898..a3bee8d6 100644 --- a/app/models/execution_environment.rb +++ b/app/models/execution_environment.rb @@ -16,6 +16,7 @@ class ExecutionEnvironment < ApplicationRecord has_many :exercises belongs_to :file_type has_many :error_templates + belongs_to :testrun_execution_environment, optional: true, dependent: :destroy scope :with_exercises, -> { where('id IN (SELECT execution_environment_id FROM exercises)') } diff --git a/app/models/submission.rb b/app/models/submission.rb index ead754cd..2150b0c5 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -46,6 +46,8 @@ class Submission < ApplicationRecord validates :cause, inclusion: {in: CAUSES} + attr_reader :used_execution_environment + # after_save :trigger_working_times_action_cable def build_files_hash(files, attribute) @@ -195,8 +197,8 @@ class Submission < ApplicationRecord def prepared_runner request_time = Time.zone.now begin - execution_environment = AwsStudy.get_execution_environment(user, exercise) - runner = Runner.for(user, execution_environment) + @used_execution_environment = AwsStudy.get_execution_environment(user, exercise) + runner = Runner.for(user, @used_execution_environment) files = collect_files files.reject!(&:teacher_defined_assessment?) if cause == 'run' Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Copying files to Runner #{runner.id} for #{user_type} #{user_id} and Submission #{id}." } @@ -254,6 +256,7 @@ class Submission < ApplicationRecord container_execution_time: output[:container_execution_time], waiting_for_container_time: output[:waiting_for_container_time] ) + TestrunExecutionEnvironment.create(testrun: testrun, execution_environment: @used_execution_environment) filename = file.filepath diff --git a/app/models/testrun.rb b/app/models/testrun.rb index e8903e61..126a1499 100644 --- a/app/models/testrun.rb +++ b/app/models/testrun.rb @@ -3,4 +3,5 @@ class Testrun < ApplicationRecord belongs_to :file, class_name: 'CodeOcean::File', optional: true belongs_to :submission + belongs_to :testrun_execution_environment, optional: true, dependent: :destroy end diff --git a/app/models/testrun_execution_environment.rb b/app/models/testrun_execution_environment.rb new file mode 100644 index 00000000..70270018 --- /dev/null +++ b/app/models/testrun_execution_environment.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class TestrunExecutionEnvironment < ApplicationRecord + belongs_to :testrun + belongs_to :execution_environment +end diff --git a/db/migrate/20220415125948_add_testrun_execution_environment.rb b/db/migrate/20220415125948_add_testrun_execution_environment.rb new file mode 100644 index 00000000..b33de7a4 --- /dev/null +++ b/db/migrate/20220415125948_add_testrun_execution_environment.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class AddTestrunExecutionEnvironment < ActiveRecord::Migration[6.1] + def change + create_table :testrun_execution_environments do |t| + t.belongs_to :testrun, foreign_key: true, null: false, index: true + t.belongs_to :execution_environment, foreign_key: true, null: false, index: {name: 'index_testrun_execution_environments'} + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8a1b089a..ea8791a6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_11_18_185051) do +ActiveRecord::Schema.define(version: 2022_04_15_125948) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -461,6 +461,15 @@ ActiveRecord::Schema.define(version: 2021_11_18_185051) do t.datetime "updated_at" end + create_table "testrun_execution_environments", force: :cascade do |t| + t.bigint "testrun_id", null: false + t.bigint "execution_environment_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["execution_environment_id"], name: "index_testrun_execution_environments" + t.index ["testrun_id"], name: "index_testrun_execution_environments_on_testrun_id" + end + create_table "testruns", id: :serial, force: :cascade do |t| t.boolean "passed" t.text "output" @@ -536,6 +545,8 @@ ActiveRecord::Schema.define(version: 2021_11_18_185051) do add_foreign_key "exercise_tips", "tips" add_foreign_key "remote_evaluation_mappings", "study_groups" add_foreign_key "submissions", "study_groups" + add_foreign_key "testrun_execution_environments", "execution_environments" + add_foreign_key "testrun_execution_environments", "testruns" add_foreign_key "tips", "file_types" add_foreign_key "user_exercise_feedbacks", "submissions" end