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

Feature/an error when tried to run the rake task no method error undefined method file contents for nil nil class cu 86b1zgdgq #2

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
/test/dummy/tmp/development_secret.txt

.byebug_history

.DS_Store
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
rake-ui (0.1.0)
actionpack
activestorage
activesupport
railties
rake
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ RakeUi.configuration do |config|
end
```

### Enabling ActiveStorage
In order to turn on/off active_storage for RakeUI gem, add configuration in initializer as follows

```rb
RakeUi.configuration do |config|
config.active_storage = false
NewAlexandria marked this conversation as resolved.
Show resolved Hide resolved
end
```

We recommend adding guards in your route to ensure that the proper authentication is in place to ensure that users are authenticated so that if this were ever to be rendered in production, you would be covered. The best way for that is [router constraints](https://guides.rubyonrails.org/routing.html#specifying-constraints)

## Testing
Expand Down
40 changes: 39 additions & 1 deletion app/controllers/rake_ui/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,51 @@
module RakeUi
class ApplicationController < ActionController::Base
before_action :black_hole_production
before_action :auth_validate
before_action :policy_validate

# include Pundit::Authorization

# before_action :authorize_pundit
# before_action :authorize!

STAGING_OK = (Rails.env.staging? && RakeUi.configuration.allow_staging)
PROD_OK = RakeUi.configuration.allow_production

private

def authorize_pundit
r = 3
@current_user = authenticate_admin_user!
authorize(@current_user)
# binding.pry
# authorize :rake_tasks, :show?
end

def black_hole_production
return if Rails.env.test? || Rails.env.development? || RakeUi.configuration.allow_production
return if Rails.env.test? || Rails.env.development? || STAGING_OK || PROD_OK

raise ActionController::RoutingError, "Not Found"
end

def auth_validate
return true unless RakeUi.configuration.auth_engine

if defined?(RakeUi.configuration.auth_engine)
cb = RakeUi.configuration.auth_callback
return false unless cb && (cb.class == Proc)
RakeUi.configuration.auth_callback.call(self)
end
end

def policy_validate
return true unless RakeUi.configuration.policy_engine

if defined?(RakeUi.configuration.policy_engine)
cb = RakeUi.configuration.policy_callback
return false unless cb && (cb.class == Proc)
RakeUi.configuration.policy_callback.call(self)
end
end
end
end
11 changes: 7 additions & 4 deletions app/controllers/rake_ui/rake_task_logs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class RakeTaskLogsController < ApplicationController
:log_file_full_path].freeze

def index
@rake_task_logs = RakeUi::RakeTaskLog.all.sort_by(&:id)

@rake_task_logs = klass.all
respond_to do |format|
format.html
format.json do
Expand All @@ -25,8 +24,7 @@ def index
end

def show
@rake_task_log = RakeUi::RakeTaskLog.find_by_id(params[:id])

@rake_task_log = klass.find_by_id(params[:id])
@rake_task_log_content = @rake_task_log.file_contents.gsub("\n", "<br />")
@rake_task_log_content_url = rake_task_log_path(@rake_task_log.id, format: :json)
@is_rake_task_log_finished = @rake_task_log.finished?
Expand Down Expand Up @@ -55,5 +53,10 @@ def rake_task_log_as_json(task)
def rake_task_logs_as_json(tasks = [])
tasks.map { |task| rake_task_log_as_json(task) }
end

def klass
RakeUi.configuration.active_storage ? ::RakeTaskLog : RakeUi::RakeTaskLog
NewAlexandria marked this conversation as resolved.
Show resolved Hide resolved
end

end
end
9 changes: 9 additions & 0 deletions app/helpers/rake_ui/rake_task_log_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module RakeUi
module RakeTaskLogHelper
def attributers_to_show
RakeUi.configuration.active_storage ? ::RakeTaskLog::ATTRIBUTES_TO_SHOW : []
end
end
end
39 changes: 39 additions & 0 deletions app/models/concerns/rake_task_logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module RakeTaskLogs
extend ActiveSupport::Concern
ID_DATE_FORMAT = "%Y-%m-%d-%H-%M-%S%z"
REPOSITORY_DIR = Rails.root.join("tmp", "rake_ui")
FILE_DELIMITER = "____"
TASK_HEADER_OUTPUT_DELIMITER = "-------------------------------"
FILE_ITEM_SEPARATOR = ": "
FINISHED_STRING = "+++++ COMMAND FINISHED +++++"
INPROGRESS = "Task is in Progress...."
NOT_AVAILABLE = "Log File Not Avaialable..."


class_methods do
def create_tmp_file_dir
FileUtils.mkdir_p(REPOSITORY_DIR.to_s)
end

def generate_task_attributes(raker_id:)
date = Time.now.strftime(ID_DATE_FORMAT)
id = "#{date}#{FILE_DELIMITER}#{raker_id}"
log_file_name = "#{id}.txt"
log_file_full_path = REPOSITORY_DIR.join(log_file_name).to_s
{
date:,
id:,
log_file_name:,
log_file_full_path:
}
end
end

def rake_command_with_logging
"#{rake_command} 2>&1 >> #{log_file_full_path}"
end

def command_to_mark_log_finished
"echo #{FINISHED_STRING} >> #{log_file_full_path}"
end
end
54 changes: 54 additions & 0 deletions app/models/rake_task_log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class RakeTaskLog < ApplicationRecord
include RakeTaskLogs

ATTRIBUTES_TO_SHOW = %w[id name date args environment rake_command rake_definition_file log_file_name log_file_full_path]
has_one_attached :log_file
enum status: { in_progress: 0, finished: 1 }

def self.build_new_for_command(name:, rake_definition_file:, rake_command:, raker_id:, args: nil, environment: nil)
create_tmp_file_dir
generate_file_content(log_file_full_path: generate_task_attributes(raker_id:)[:log_file_full_path])
create_rake_task_log(name:, args:, environment:, rake_command:, rake_definition_file:, raker_id:)
end

def self.generate_file_content(log_file_full_path:)
File.open(log_file_full_path, "w+") do |f|
f.puts TASK_HEADER_OUTPUT_DELIMITER.to_s
f.puts " INVOKED RAKE TASK OUTPUT BELOW"
f.puts TASK_HEADER_OUTPUT_DELIMITER.to_s
end
end

def self.create_rake_task_log(name:, args:, environment:, rake_command:, rake_definition_file:, raker_id:)
attributes = generate_task_attributes(raker_id:)

::RakeTaskLog.create(name:,
args:,
environment:,
rake_command:,
rake_definition_file:,
log_file_name: attributes[:log_file_name],
log_file_full_path: attributes[:log_file_full_path],
raker_id:)
end

def attach_file_with_rake_task_log
if File.exist?(log_file_full_path)
log_file.attach(io: File.open(log_file_full_path), filename: 'log.txt')
end
self.status = :finished
self.save!
File.delete(log_file_full_path)
end

def file_contents
return INPROGRESS unless finished?

log_file.download if log_file.attached?
end

def date
created_at
end

end
8 changes: 6 additions & 2 deletions app/models/rake_ui/rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def internal_task?

def call(args: nil, environment: nil)
rake_command = build_rake_command(args: args, environment: environment)

rake_task_log = RakeUi::RakeTaskLog.build_new_for_command(
rake_task_log = klass.build_new_for_command(
name: name,
args: args,
environment: environment,
Expand All @@ -98,6 +97,7 @@ def call(args: nil, environment: nil)
system(rake_task_log.rake_command_with_logging)

system(rake_task_log.command_to_mark_log_finished)
rake_task_log.attach_file_with_rake_task_log if RakeUi.configuration.active_storage
end

rake_task_log
Expand All @@ -122,5 +122,9 @@ def build_rake_command(args: nil, environment: nil)

command
end

def klass
@klass ||= RakeUi.configuration.active_storage ? ::RakeTaskLog : RakeUi::RakeTaskLog
end
end
end
20 changes: 1 addition & 19 deletions app/models/rake_ui/rake_task_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

module RakeUi
class RakeTaskLog < OpenStruct
# year-month-day-hour(24hour time)-minute-second-utc
ID_DATE_FORMAT = "%Y-%m-%d-%H-%M-%S%z"
REPOSITORY_DIR = Rails.root.join("tmp", "rake_ui")
FILE_DELIMITER = "____"
FINISHED_STRING = "+++++ COMMAND FINISHED +++++"
TASK_HEADER_OUTPUT_DELIMITER = "-------------------------------"
FILE_ITEM_SEPARATOR = ": "

def self.create_tmp_file_dir
FileUtils.mkdir_p(REPOSITORY_DIR.to_s)
end
include RakeTaskLogs

def self.truncate
FileUtils.rm_rf(Dir.glob(REPOSITORY_DIR.to_s + "/*"))
Expand Down Expand Up @@ -116,18 +106,10 @@ def log_file_full_path
super || parsed_file_contents[:log_file_full_path]
end

def rake_command_with_logging
"#{rake_command} 2>&1 >> #{log_file_full_path}"
end
NewAlexandria marked this conversation as resolved.
Show resolved Hide resolved

def file_contents
@file_contents ||= File.read(log_file_full_path)
end

def command_to_mark_log_finished
"echo #{FINISHED_STRING} >> #{log_file_full_path}"
end

def finished?
file_contents.include? FINISHED_STRING
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/rake_ui/rake_task_logs/_log_attributes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<%= "#{attr}: #{val}" %>
<br />
</div>
3 changes: 3 additions & 0 deletions app/views/rake_ui/rake_task_logs/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<h1>Rake Task Log</h1>

<hr />
<% attributers_to_show.each do |attr| %>
<%= render 'log_attributes', attr: attr, val: @rake_task_log.send(attr) %>
<% end %>

<div id="log_content" class="overflow-y: scroll">
<%= @rake_task_log_content.html_safe %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type, if_not_exists: true do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type, if_not_exists: true do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type, if_not_exists: true do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private

def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
16 changes: 16 additions & 0 deletions db/migrate/20240906183055_create_rake_task_logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateRakeTaskLogs < ActiveRecord::Migration[5.2]
def change
create_table :rake_task_logs, if_not_exists: true do |t|
t.integer :status, default: 0
t.string :name
t.string :args
t.string :environment
t.string :rake_command
t.string :rake_definition_file
t.string :log_file_name
t.string :log_file_full_path
t.string :raker_id
t.timestamps
end
end
end
13 changes: 13 additions & 0 deletions lib/rake-ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@

module RakeUi
mattr_accessor :allow_production
mattr_accessor :allow_staging
mattr_accessor :policy_engine
mattr_accessor :policy_callback
mattr_accessor :auth_engine
mattr_accessor :auth_callback
mattr_accessor :active_storage

self.active_storage = false
self.allow_production = false
self.allow_staging = true
self.policy_engine = nil
self.policy_callback = nil
self.auth_engine = nil
self.auth_callback = nil

def self.configuration
yield(self) if block_given?
Expand Down
Loading