Class: Minicron::CLI::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/minicron/cli/commands.rb

Class Method Summary collapse

Class Method Details

.add_db_cli_command(cli) ⇒ Object

Add the `minicron db` command


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
# File 'lib/minicron/cli/commands.rb', line 9

def self.add_db_cli_command(cli)
  cli.command :db do |c|
    c.syntax = 'minicron db [setup]'
    c.description = 'Sets up the minicron database schema.'

    c.action do |args, opts|
      # Check that exactly one argument has been passed
      if args.length != 1
        fail ArgumentError, 'A valid command to run is required! See `minicron help db`'
      end

      # Parse the file and cli config options
      Minicron::CLI.parse_config(opts)

      # These are inlined as we only need them in this use case
      require 'rake'
      require 'minicron/hub/app'
      require 'sinatra/activerecord/rake'

      # Setup the db
      Minicron::Hub::App.setup_db

      # Tell activerecord where the db folder is, it assumes it is in db/
      Sinatra::ActiveRecordTasks.db_dir = Minicron::HUB_PATH + '/db'

      # Adjust the task name
      task = args.first == 'setup' ? 'load' : args.first

      # Run the task
      Rake.application['db:schema:' + task].invoke
    end
  end
end

.add_run_cli_command(cli) {|output| ... } ⇒ Object

Add the `minicron run [command]` command

Yield Parameters:

  • output (String)

    output from the cli


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/minicron/cli/commands.rb', line 97

def self.add_run_cli_command(cli)
  # Add the run command to the cli
  cli.command :run do |c|
    c.syntax = "minicron run 'command -option value'"
    c.description = 'Runs the command passed as an argument.'
    c.option '--mode STRING', String, "How to capture the command output, each 'line' or each 'char'? Default: #{Minicron.config['cli']['mode']}"
    c.option '--dry-run', "Run the command without sending the output to the server.  Default: #{Minicron.config['cli']['dry_run']}"

    c.action do |args, opts|
      # Check that exactly one argument has been passed
      if args.length != 1
        fail ArgumentError, 'A valid command to run is required! See `minicron help run`'
      end

      # Parse the file and cli config options
      Minicron::CLI.parse_config(opts)

      begin
        # Set up the job and get the job and execution ids
        unless Minicron.config['cli']['dry_run']
          # Get a faye instance so we can send data about the job
          faye = Minicron::Transport::Client.new(
            Minicron.config['client']['scheme'],
            Minicron.config['client']['host'],
            Minicron.config['client']['port'],
            Minicron.config['client']['path']
          )

          # Set up the job and get the jexecution and job ids back from the server
          ids = setup_job(args.first, faye)
        end

        # Execute the command and yield the output
        Minicron::CLI.run_command(args.first, :mode => Minicron.config['cli']['mode'], :verbose => Minicron.config['verbose']) do |output|
          # We need to handle the yielded output differently based on it's type
          case output[:type]
          when :status
            unless Minicron.config['cli']['dry_run']
              faye.send(:job_id => ids[:job_id], :execution_id => ids[:execution_id], :type => :status, :message => output[:output])
            end
          when :command
            unless Minicron.config['cli']['dry_run']
              faye.send(:job_id => ids[:job_id], :execution_id => ids[:execution_id], :type => :output, :message => output[:output])
            end
          end

          yield output[:output] unless output[:type] == :status
        end
      rescue Exception => e
        # Send the exception message to the server and yield it
        unless Minicron.config['cli']['dry_run']
          faye.send(:job_id => ids[:job_id], :execution_id => ids[:execution_id], :type => :output, :message => e.message)
        end

        raise Exception, e
      ensure
        # Ensure that all messages are delivered and that we
        unless Minicron.config['cli']['dry_run']
          faye.ensure_delivery
          faye.tidy_up
        end
      end
    end
  end
end

.add_server_cli_command(cli) ⇒ Object

Add the `minicron server` command


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
# File 'lib/minicron/cli/commands.rb', line 44

def self.add_server_cli_command(cli)
  cli.command :server do |c|
    c.syntax = 'minicron server [start|stop|status]'
    c.description = 'Controls the minicron server.'
    c.option '--host STRING', String, "The host for the server to listen on. Default: #{Minicron.config['server']['host']}"
    c.option '--port STRING', Integer, "How port for the server to listed on. Default: #{Minicron.config['server']['port']}"
    c.option '--path STRING', String, "The path on the host. Default: #{Minicron.config['server']['path']}"
    c.option '--debug', "Enable debug mode. Default: #{Minicron.config['server']['debug']}"

    c.action do |args, opts|
      # Parse the file and cli config options
      Minicron::CLI.parse_config(opts)

      # If we get no arguments then default the action to start
      action = args.first.nil? ? 'start' : args.first

      # Get an instance of insidious and set the pid file
      insidious = Insidious.new(
        :pid_file => '/tmp/minicron.pid',
        :daemonize => Minicron.config['server']['debug'] == false
      )

      case action
      when 'start'
        insidious.start! do
        # Run the execution monitor (this runs in a separate thread)
        monitor = Minicron::Monitor.new
        monitor.start!

        # Start the server!
        Minicron::Transport::Server.start!(
          Minicron.config['server']['host'],
          Minicron.config['server']['port'],
          Minicron.config['server']['path']
        )
        end
      when 'stop'
        insidious.stop!
      when 'status'
        if insidious.running?
          puts 'minicron is running'
        else
          puts 'minicron is not running'
        end
      else
        fail ArgumentError, 'Invalid action, expected [start|stop|status]. See `minicron help server`'
      end
    end
  end
end