Class: WebRake::TasksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/web_rake/tasks_controller.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/web_rake/tasks_controller.rb', line 18

def execute
  task_name = params[:id].gsub('__', ':')

  # Use find_task to validate the task is allowed
  unless find_task
    redirect_to root_path, alert: "Task not found"
    return
  end

  start_time = Time.current
  output = []
  errors = []
  success = false

  begin
    original_stdout = $stdout
    original_stderr = $stderr

    $stdout = StringIO.new
    $stderr = StringIO.new

    # Run the rake task in a subprocess to avoid state issues
    # This ensures clean execution every time

    # Build the rake command
    rake_command = "bundle exec rake #{task_name}"

    # Execute the command and capture output
    stdout_str, stderr_str, status = Open3.capture3(rake_command)

    output = stdout_str.split("\n")
    errors = stderr_str.split("\n") if stderr_str.present?
    success = status.success?
  rescue => e
    errors << e.message
    errors.concat(e.backtrace) if Rails.env.development?
  ensure
    $stdout = original_stdout if original_stdout
    $stderr = original_stderr if original_stderr
  end

  end_time = Time.current
  duration = (end_time - start_time).round(2)

  # Create a simple task object for the view
  task = OpenStruct.new(name: task_name)

  render :execute, locals: {
    task: task,
    output: output,
    errors: errors,
    success: success,
    duration: duration
  }
end

#indexObject



9
10
11
# File 'app/controllers/web_rake/tasks_controller.rb', line 9

def index
  @tasks = custom_tasks.sort_by(&:name)
end

#showObject



13
14
15
16
# File 'app/controllers/web_rake/tasks_controller.rb', line 13

def show
  @task = find_task
  redirect_to root_path, alert: "Task not found" unless @task
end