Module: Mutx::Commands

Defined in:
lib/mutx/commands/bye.rb,
lib/mutx/commands/help.rb,
lib/mutx/commands/stop.rb,
lib/mutx/commands/reset.rb,
lib/mutx/commands/start.rb,
lib/mutx/commands/install.rb,
lib/mutx/commands/restart.rb,
lib/mutx/commands/reset_tasks.rb

Class Method Summary collapse

Class Method Details

.byeObject



4
5
6
7
8
9
10
# File 'lib/mutx/commands/bye.rb', line 4

def self.bye
    Mutx::Support::Log.debug "#{self}:#{__method__}" if Mutx::Support::Log
    self.stop
    Mutx::Support::FilesCleanner.delete_mutx_folder
    puts "Files cleanned"
    puts "Bye!..."
end

.helpObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mutx/commands/help.rb', line 4

def self.help
  Mutx::Support::Log.debug "#{self}:#{__method__}" if Mutx::Support::Log
  puts "

  Mutx has some commands:

    - install
    - start
    - stop
    - restart
    - reset

  Note: If you stop mutx and then you want to get it up and the port you are using is already in use
        you could use the following commands (Ubunutu OS):

            $sudo netstat -tapen | grep :8080

        In this example we use the port 8080. This command will give you the app that is using the port.
        Then you could kill it getting its PID previously."
end

.install(origin = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mutx/commands/install.rb', line 4

def self.install origin=nil

  begin


    Mutx::TaskRack.start([])

    puts "

    A new folder called mutx was created. Check the configuration file under config/ folder with the name mutx.conf.
    You'll find some configuration values there. Take a look and set your preferences!
    Enjoy Mutx
    Thanks
    "
    puts "Now, you can run bundle install and then `mutx start` command"

  rescue => e
    puts "\n\nERROR: #{e}\n\n #{e.backtrace}"

  end
end

.resetObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mutx/commands/reset.rb', line 4

def self.reset
  Mutx::Support::Log.debug "#{self}:#{__method__}" if Mutx::Support::Log
  begin

    Mutx::Support::Configuration.get
    Mutx::Database::MongoConnector.new(Mutx::Support::Configuration.db_connection_data)

    print "\nCleanning database..."

    Mutx::Database::MongoConnector.drop_collections
    print "Done!\n\n"

    print "\nCleanning project..."

    Mutx::Support::FilesCleanner.start!
    print "Done!\n\n"

  rescue => e
    puts "CANNOT CLEAN SYSTEM\n#{e}\n\n#{e.backtrace}"
  end
end

.reset_tasksObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mutx/commands/reset_tasks.rb', line 4

def self.reset_tasks
  Mutx::Support::Log.debug "#{self}:#{__method__}" if Mutx::Support::Log
  begin

    Mutx::Support::Configuration.get

    Mutx::Database::MongoConnector.new(Mutx::Support::Configuration.db_connection_data)

    print "\nCleanning tasks from database..."

    Mutx::Database::MongoConnector.drop_collections
    print "Done!\n\n"

    print "\nCleanning project..."

    Mutx::Support::FilesCleanner.start!
    print "Done!\n\n"

  rescue => e
    puts "CANNOT CLEAN SYSTEM\n#{e}\n\n#{e.backtrace}"
  end
end

.restartObject



4
5
6
7
8
# File 'lib/mutx/commands/restart.rb', line 4

def self.restart
  Mutx::Support::Log.debug "#{self}:#{__method__}" if Mutx::Support::Log
  self.stop
  self.start
end

.start(nodemon = false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mutx/commands/start.rb', line 4

def self.start nodemon=false

  Mutx::Support::Log.start

  Mutx::Support::Log.debug "Starting Mutx" if Mutx::Support::Log
  begin

    Mutx::Support::Configuration.new

    Mutx::Support::Log.debug "Starting...\n\n#{Mutx::Support::Logo.}" if Mutx::Support::Log

    Mutx::Support::Log.debug "Checking config.ru file existance" if Mutx::Support::Log
    raise "ERROR --- mutx/config.ru file was not found. Try with `mutx prepare` command before `mutx start`" unless File.exist?("#{Dir.pwd}/mutx/config.ru")

    Mutx::Support::Log.debug "Checking unicorn.rb file existance" if Mutx::Support::Log
    raise "ERROR --- mutx/unicorn.rb file was not found. Try with `mutx prepare` command before `mutx start`" unless File.exist?("#{Dir.pwd}/mutx/unicorn.rb")

    Mutx::Support::Logo.show

    Mutx::Support::Configuration.show_configuration_values


    Mutx::Support::Log.debug "Connecting to database" if Mutx::Support::Log
    Mutx::Database::MongoConnector.new(Mutx::Support::Configuration.db_connection_data)


    Mutx::Support::Log.debug "Loading doc" if Mutx::Support::Log
    Mutx::Support::Documentation.load_documentation

    if Mutx::Support::Configuration.headless?
      Mutx::Support::Log.debug "Headless mode: ON - Checking xvfb existance" if Mutx::Support::Log
      res = Mutx::Support::Console.execute "xvfb-run"
      if res.include? "sudo apt-get install xvfb"
        puts "You have configured headless mode but xvfb package is not installed on your system.
Please, install xvfb package if you want to run browsers in headless mode
or set headless active value as false if you do not use browser in your tests."
        return
      end
    end

    puts "\n"
    Mutx::Support::Log.debug "Cleanning old mutx report files" if Mutx::Support::Log
    Mutx::Support::FilesCleanner.delete_mutx_reports()
    Mutx::Support::Log.debug "Old mutx report files cleanned" if Mutx::Support::Log


    Mutx::Support::Log.debug "Cleanning old mutx console files" if Mutx::Support::Log
    Mutx::Support::FilesCleanner.delete_console_outputs_files()
    Mutx::Support::Log.debug "Old mutx console files cleanned" if Mutx::Support::Log

    Mutx::Support::Log.debug "Clearing mutx log file" if Mutx::Support::Log
    Mutx::Support::FilesCleanner.clear_mutx_log
    Mutx::Support::Log.debug "Mutx log file cleanned" if Mutx::Support::Log


    Mutx::Support::Log.debug "Clearing sidekiq log file" if Mutx::Support::Log
    Mutx::Support::FilesCleanner.clear_sidekiq_log
    Mutx::Support::Log.debug "Sidekiq log file cleanned" if Mutx::Support::Log

    # To prevent showing tasks as runnnig when service started recently reset all tasks
    Mutx::Support::Log.debug "Reseting tasks statuses" if Mutx::Support::Log
    Mutx::Tasks.reset!
    Mutx::Support::Log.debug "Tasks statuses reseted" if Mutx::Support::Log
    puts "\n* Tasks Status: Reseted"

    # Force results to reset or finished status
    Mutx::Support::Log.debug "Reseting defunct executions" if Mutx::Support::Log
    Mutx::Results.reset!
    Mutx::Support::Log.debug "Defunct execution reseted" if Mutx::Support::Log
    puts "\n* Results: Reseted".green

    mutx_arg = "-D" unless nodemon

    Mutx::Support::Log.debug "Starting Sidekiq" if Mutx::Support::Log
    Mutx::BackgroundJobs::Sidekiq.start
    Mutx::Support::Log.debug "Sidekiq Started" if Mutx::Support::Log

    # Start mutx app
    Mutx::Support::Log.debug "Starting Mutx" if Mutx::Support::Log
    Mutx::Support::Console.execute "unicorn -c #{Dir.pwd}/mutx/unicorn.rb -p #{Mutx::Support::Configuration.port} #{mutx_arg} mutx/config.ru"
    #Mutx::Support::Console.execute "rackup mutx/config.ru"
    Mutx::Support::Log.debug "Mutx started" if Mutx::Support::Log

    # Save all mutx pids
    Mutx::Support::Log.debug "Saving PIDs for Mutx" if Mutx::Support::Log
    File.open("#{Dir.pwd}/mutx/mutx_pids", "a"){ |f| f.write Mutx::Support::Processes.mutx_pids.join("\n")}
    Mutx::Support::Log.debug "Mutx PIDs saved" if Mutx::Support::Log

    puts "\n\n* Mutx is succesfully Started!\n".green
    if $IP_ADDRESS
        puts "\n\n You can go now to http://#{$IP_ADDRESS}:#{Mutx::Support::Configuration.port}/mutx\n\n"
        Mutx::Support::Log.debug "You can go now to http://#{$IP_ADDRESS}:#{Mutx::Support::Configuration.port}/mutx" if Mutx::Support::Log
    end

  rescue => e
    Mutx::Support::Log.error "Error starting Mutx: #{e}#{e.backtrace}" if Mutx::Support::Log
    puts "An error ocurred while starting Mutx. See mutx log for more information.#{e} #{e.backtrace}".red
  end
end

.stopObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
# File 'lib/mutx/commands/stop.rb', line 4

def self.stop

  Mutx::Support::Log.debug "#{self}:#{__method__}" if Mutx::Support::Log

  Mutx::Support::Configuration.get

  # Get pids from saved file on start process
  if File.exist? "#{Dir.pwd}/mutx/mutx_pids"

    mutx_pids = IO.read("#{Dir.pwd}/mutx/mutx_pids").split("\n")
    # Kill all pids specified on file

    begin
      Mutx::Support::Processes.kill_all_these mutx_pids
    rescue
    end

    # Delete pid file
    File.delete("#{Dir.pwd}/mutx/mutx_pids")
  end

  if File.exist? "#{Dir.pwd}/mutx/sidekiq_pid"

    sidekiq_pid = IO.read("#{Dir.pwd}/mutx/sidekiq_pid").split("\n")

    begin
      Mutx::Support::Processes.kill_all_these sidekiq_pid
    rescue
    end


    File.delete("#{Dir.pwd}/mutx/sidekiq_pid")
  end

  # Evaluates if any pid could not be killed (retry)
  Mutx::Support::Processes.kill_all_these(Mutx::Support::Processes.mutx_pids)

  if Mutx::Support::Processes.mutx_pids.empty?

    puts "
Mutx stopped!"

  else
    puts "
Could not stop Mutx.
If Mutx is still running please type `mutx help` to get some help"
  end
end