Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Rails to 6.0 #146

Merged
merged 9 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ jobs:
bundler-cache: true
- run: bin/rails db:create db:schema:load
- run: bundle exec rspec

assets-precompile:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby:
- '2.7'
# TODO: Add 3.0 and 3.1 after upgrading Rails
name: Run assets:precompile with Ruby ${{ matrix.ruby }}
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bin/rails app:assets:precompile RAILS_ENV=production SECRET_KEY_BASE=dummy
8 changes: 4 additions & 4 deletions app/controllers/kuroko2/api/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class Kuroko2::Api::ApplicationController < ActionController::Base
respond_with_error(404, 'record_not_found', exception.message)
end

rescue_from HTTP::Forbidden do |exception|
rescue_from Http::Forbidden do |exception|
respond_with_error(403, 'forbidden', exception.message)
end

rescue_from HTTP::Unauthorized do |exception|
rescue_from Http::Unauthorized do |exception|
respond_with_error(401, 'unauthorized', exception.message)
end

rescue_from HTTP::UnprocessableEntity do |exception|
rescue_from Http::UnprocessableEntity do |exception|
respond_with_error(422, 'unprocessable_entity', exception.message)
end

Expand Down Expand Up @@ -43,7 +43,7 @@ def api_authentication
end

if service_name.nil?
raise HTTP::Unauthorized
raise Http::Unauthorized
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/controllers/kuroko2/api/job_definitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_resource

@resource = Kuroko2::Api::JobDefinitionResource.new(definition)
else
raise HTTP::UnprocessableEntity.new("#{definition.name}: #{definition.errors.full_messages.join}")
raise Http::UnprocessableEntity.new("#{definition.name}: #{definition.errors.full_messages.join}")
end
end

Expand All @@ -42,7 +42,7 @@ def update_resource
if definition.update_and_record_revision(definition_params(params))
@resource = Kuroko2::Api::JobDefinitionResource.new(definition)
else
raise HTTP::UnprocessableEntity.new("#{definition.name}: #{definition.errors.full_messages.join}")
raise Http::UnprocessableEntity.new("#{definition.name}: #{definition.errors.full_messages.join}")
end
end

Expand Down Expand Up @@ -79,7 +79,7 @@ def job_schedules(params, definition)
cron_strings = params.permit(cron: []).fetch(:cron, [])
cron_strings.map do |cron|
schedule = definition.job_schedules.find_or_create_by(cron: cron)
raise HTTP::UnprocessableEntity.new("#{cron}: #{schedule.errors.full_messages.join}") unless schedule.valid?
raise Http::UnprocessableEntity.new("#{cron}: #{schedule.errors.full_messages.join}") unless schedule.valid?
schedule
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/kuroko2/api/job_instances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def require_resources
def create_resource
definition = Kuroko2::JobDefinition.find(params[:job_definition_id])
unless definition.api_allowed?
raise HTTP::Forbidden.new("#{definition.name} is not allowed to be executed via API")
raise Http::Forbidden.new("#{definition.name} is not allowed to be executed via API")
end

instance = definition.create_instance(
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/kuroko2/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Kuroko2::ApplicationController < ActionController::Base
helper_method :current_user, :signed_in?
before_action :require_sign_in

rescue_from HTTP::BadRequest do
rescue_from Http::BadRequest do
respond_to do |format|
format.html { render 'public/500.html', layout: false, status: :bad_request }
format.json { render json: { message: 'Bad Request' }, status: :bad_request }
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/kuroko2/stars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def create
if (star.save)
render json: star, status: :created
else
raise HTTP::BadRequest
raise Http::BadRequest
end
end

Expand All @@ -18,7 +18,7 @@ def destroy
if (star.destroy)
render json: star, status: :ok
else
raise HTTP::BadRequest
raise Http::BadRequest
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/kuroko2/tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def update

@engine.retry(@token)
else
raise HTTP::BadRequest
raise Http::BadRequest
end

redirect_to job_definition_job_instance_path(job_definition_id: @token.job_definition_id, id: @token.job_instance.id)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/kuroko2/workers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
end

def update
@worker.update_attributes(worker_params)
@worker.update(worker_params)
redirect_to workers_path
end

Expand Down
2 changes: 1 addition & 1 deletion app/errors/http/bad_request.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module HTTP
module Http
class BadRequest < StandardError
end
end
2 changes: 1 addition & 1 deletion app/errors/http/forbidden.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module HTTP
module Http
class Forbidden < StandardError
end
end
2 changes: 1 addition & 1 deletion app/errors/http/unauthorized.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module HTTP
module Http
class Unauthorized < StandardError
end
end
2 changes: 1 addition & 1 deletion app/errors/http/unprocessable_entity.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module HTTP
module Http
class UnprocessableEntity < StandardError
end
end
23 changes: 18 additions & 5 deletions app/models/kuroko2/job_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,26 @@ def create_instance(script: nil, launched_by:, token: nil )
end

def save_and_record_revision(edited_user: nil)
record_revision(edited_user: edited_user)
save
transaction do
if save
record_revision(edited_user: edited_user)
true
else
false
end
end
end

def update_and_record_revision(attributes, edited_user: nil)
assign_attributes(attributes)
record_revision(edited_user: edited_user)
save
transaction do
if save
record_revision(edited_user: edited_user)
true
else
false
end
end
end

private
Expand Down Expand Up @@ -142,8 +154,9 @@ def set_default_values

def record_revision(edited_user: nil)
unless revisions.first.try(:script) == script
revisions.new(script: script, user: edited_user, changed_at: Time.current)
revisions.create!(script: script, user: edited_user, changed_at: Time.current)
end
nil
end

def create_default_memory_expectancy
Expand Down
2 changes: 1 addition & 1 deletion app/models/kuroko2/job_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Kuroko2::JobSchedule < Kuroko2::ApplicationRecord

CHRONO_SCHEDULE_METHODS = %i[minutes hours days months wdays]

validates :cron, format: { with: CRON_FORMAT }, uniqueness: { scope: :job_definition_id }
validates :cron, format: { with: CRON_FORMAT }, uniqueness: { scope: :job_definition_id, case_sensitive: true }
validate :validate_cron_schedule

def next(now = Time.current)
Expand Down
2 changes: 1 addition & 1 deletion app/models/kuroko2/job_suspend_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Kuroko2::JobSuspendSchedule < Kuroko2::ApplicationRecord

belongs_to :job_definition

validates :cron, format: { with: Kuroko2::JobSchedule::CRON_FORMAT }, uniqueness: { scope: :job_definition_id }
validates :cron, format: { with: Kuroko2::JobSchedule::CRON_FORMAT }, uniqueness: { scope: :job_definition_id, case_sensitive: true }
validate :validate_cron_schedule

def suspend_times(time_from, time_to)
Expand Down
6 changes: 3 additions & 3 deletions kuroko2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md", "bin/*.rb"]
s.test_files = Dir["spec/**/*"]

s.add_dependency "rails", ">= 5.0.0.1", '< 5.2'
s.add_dependency "rails", ">= 6.0.6.1", '< 6.1'
s.add_dependency "kaminari"
s.add_dependency "chrono"
s.add_dependency "hashie"
Expand All @@ -29,7 +29,7 @@ Gem::Specification.new do |s|
s.add_dependency 'slim-rails'
s.add_dependency 'sass', '~> 3.4.5'
s.add_dependency 'sass-rails'
s.add_dependency 'uglifier', '~> 2.7.1'
s.add_dependency 'uglifier', '>= 4'
s.add_dependency 'jbuilder'

s.add_dependency 'jquery-rails'
Expand Down Expand Up @@ -57,7 +57,7 @@ Gem::Specification.new do |s|

s.add_dependency 'diffy'

s.add_development_dependency 'mysql2', '< 0.5'
s.add_development_dependency 'mysql2', '>= 0.4.4'
s.add_development_dependency "rspec-rails"
s.add_development_dependency "factory_bot_rails", '>= 5.0.0'
s.add_development_dependency 'capybara', '>= 3.0.0'
Expand Down
2 changes: 1 addition & 1 deletion lib/autoload/kuroko2/command/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def execute_shell(command, env, execution)
stdin.close

pid = thread.pid
execution.update_attributes(pid: pid)
execution.update(pid: pid)

reader = Thread.new do
begin
Expand Down
34 changes: 19 additions & 15 deletions lib/autoload/kuroko2/workflow/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,7 @@ def retry(token)

def skip(token)
token.with_lock do
node = extract_node(token)

message = "(token #{token.uuid}) Skip current node: '#{node.type}: #{node.option}'"
token.job_instance.update_column(:error_at, nil)
token.job_instance.logs.info(message)

token.mark_as_working
process_next(node.next, token)

token.save! unless token.destroyed?

Kuroko2.logger.info(message)

Notifier.notify(:skipping, token.job_instance)
skip_with_lock(token)
end
end

Expand All @@ -68,7 +55,7 @@ def failure(token)
Notifier.notify(:failure, token.job_instance)

if token.context['AUTO_SKIP_ERROR']
skip(token)
skip_with_lock(token)
end
end

Expand Down Expand Up @@ -150,6 +137,23 @@ def process_with_lock(token)
token.save! unless token.destroyed?
end

def skip_with_lock(token)
node = extract_node(token)

message = "(token #{token.uuid}) Skip current node: '#{node.type}: #{node.option}'"
token.job_instance.update_column(:error_at, nil)
token.job_instance.logs.info(message)

token.mark_as_working
process_next(node.next, token)

token.save! unless token.destroyed?

Kuroko2.logger.info(message)

Notifier.notify(:skipping, token.job_instance)
end

def extract_node(token)
root = ScriptParser.new(token.script).parse(validate: false)
root.find(token.path)
Expand Down
24 changes: 13 additions & 11 deletions lib/kuroko2/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ class Engine < ::Rails::Engine

config.active_record.table_name_prefix = Kuroko2.config.table_name_prefix

if Kuroko2.config.custom_tasks
Kuroko2.config.custom_tasks.each do |key, klass|
unless Workflow::Node::TASK_REGISTRY.has_key?(key)
Workflow::Node.register(
key: key.to_sym,
klass: Workflow::Task.const_get(klass, false)
)
end
end
end

config.action_mailer.default_url_options = {
host: Kuroko2.config.url_host,
protocol: Kuroko2.config.url_scheme,
Expand All @@ -52,6 +41,19 @@ class Engine < ::Rails::Engine
Kuroko2.config.action_mailer.smtp_settings.to_h.symbolize_keys || {}

app.config.assets.precompile += %w(kuroko2/kuroko-logo-success.png kuroko2/kuroko-logo-error.png)
end

config.after_initialize do
if Kuroko2.config.custom_tasks
Kuroko2.config.custom_tasks.each do |key, klass|
unless Workflow::Node::TASK_REGISTRY.has_key?(key)
Workflow::Node.register(
key: key.to_sym,
klass: Workflow::Task.const_get(klass, false)
)
end
end
end

if Kuroko2.config.extensions && Kuroko2.config.extensions.controller
Kuroko2.config.extensions.controller.each do |extension|
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

context 'with groups parameter' do
before do
users.each {|u| u.update_attributes!(provider: 'group_mail') }
users.each {|u| u.update!(provider: 'group_mail') }
end
it { expect(assigns(:users).to_a).to eq users }
end
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

module Dummy
class Application < Rails::Application
config.load_defaults 6.0
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
4 changes: 3 additions & 1 deletion spec/dummy/config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'uglifier'

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand All @@ -19,7 +21,7 @@
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
config.assets.js_compressor = Uglifier.new(harmony: true)
# config.assets.css_compressor = :sass

# Do not fallback to assets pipeline if a precompiled asset is missed.
Expand Down
20 changes: 0 additions & 20 deletions spec/dummy/config/initializers/new_framework_defaults.rb

This file was deleted.

Loading