Class: Breaktime::Main

Inherits:
Object
  • Object
show all
Includes:
Log4r
Defined in:
lib/breaktime/main.rb

Overview

Handles configuration options, CLI stuff and process daemonizing.

Constant Summary collapse

DEFAULT_OPTIONS =

Default options that can be overridden in the YAML file.

{'interval' => 60,
                     'daemonize' => true,
                     'days' => ['monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday']}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Set up the logger, parse CLI options and the YAML file.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/breaktime/main.rb', line 25

def initialize
  create_logger 'error'
  @cli = Breaktime::CLI.new
  @options = DEFAULT_OPTIONS
  set_log_level @cli.options[:level]

  parse_yaml_file

  @options['log_path'] = @cli.options[:log_file]
  @options['pid_path'] = @cli.options[:pid_file]
end

Instance Attribute Details

#cliObject (readonly)

Returns the value of attribute cli.



11
12
13
# File 'lib/breaktime/main.rb', line 11

def cli
  @cli
end

#logObject (readonly)

Returns the value of attribute log.



11
12
13
# File 'lib/breaktime/main.rb', line 11

def log
  @log
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/breaktime/main.rb', line 11

def options
  @options
end

Class Method Details

.startObject

Create a new Main object and run the mode given as a CLI parameter.

Also rescue exceptions and display helpful messages.

TODO: tidy this up



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/breaktime/main.rb', line 139

def start
  main = self.new
  main.log.debug { "Starting cli mode: #{main.cli.mode}" }

  begin
    main.run_mode main.cli.mode
  # Exception handling and appropriate exit codes.
  rescue Breaktime::LinuxWinManager::ManagerUnknown
    main.log.fatal do
      <<-FATAL
It looks like you're using Linux, but I'm unable to detect your window manager to determine how to start your screensaver.

To get round this problem, just specify a "command" in your $HOME/.breaktime.yml file, and this will be executed at the start of your break.
      FATAL
    end
    exit Breaktime::EX_LINUX_WM_UNKNOWN

  rescue Breaktime::Command::OSUnknown
    main.log.fatal do
      <<-FATAL
I can't work out which operating system you're using. If you think this is unreasonable then please let me know on Github.

To get round this problem in the meantime, just specify a "command" in your $HOME/.breaktime.yml file, and this will be executed at the start of your break.
        FATAL
    end
    exit Breaktime::EX_OS_UNKNOWN

  rescue Interrupt
    main.log.warn { "Caught Control-C, shutting down..." }
    main.say_goodbye Breaktime::EX_INTERRUPT

  rescue SignalException => e
    main.log.warn { "Caught signal #{e.message}, shutting down..." }
    main.say_goodbye Breaktime::EX_SIGNAL

  rescue SystemExit
    raise

  rescue Exception => e
    main.log.fatal { "Unexpected exception {#{e.class.name}}: #{e.message}" }
    main.log.debug { $!.backtrace.join("\n\t") }
    exit Breaktime::EX_UNKNOWN

  end
end

Instance Method Details

#die(message) ⇒ Object

Exit with a trollop message.



38
39
40
# File 'lib/breaktime/main.rb', line 38

def die(message)
  Trollop::die message
end

#fork_implemented?Boolean

Check if Process has the fork method, but also check for Java

Apparently jRuby doesn’t implement this correctly, hence the secondary check.

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/breaktime/main.rb', line 88

def fork_implemented?
  if Process.respond_to?(:fork)
    RUBY_PLATFORM !~ /java/ && RUBY_ENGINE != 'macruby'
  else
    false
  end
end

#run_mode(mode) ⇒ Object

Run the given mode.

The mode is one of the command line modes (run with the –help flag).



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
# File 'lib/breaktime/main.rb', line 51

def run_mode(mode)
  command = Breaktime::Command.new @options['command'], @log

  # Switch on CLI mode.
  case mode
  # Schedule the breaktime.
  when "start"
    @log.info { "When it's breaktime I'll run: `#{command.command}`" }
    startd

  # Stop a currently running daemonized process.
  when "stop"
    @log.info { "Stopping breaktime background process" }
    stopd
    say_goodbye Breaktime::EX_OK

  # Open a dialog to notify the user about an impending screen break.
  when "dialog"
	@log.info { "Opening dialog" }
    require 'breaktime/dialog'
    # Automatically loads green shoes window

  # Run the command that will start the break.
  when "now"
    command.execute

  # Unknown mode.
  else
    die "unknown mode #{mode.inspect}"

  end
end

#say_goodbye(exit_code) ⇒ Object

Print out the gem motto and exit with the given exit code.



43
44
45
46
# File 'lib/breaktime/main.rb', line 43

def say_goodbye(exit_code)
  puts "\n\s\sBreaktime says, \"Have a break, have an unbranded chocolate snack.\""
  exit exit_code
end

#startdObject

Start the scheduler as a daemon.

The process can be kept on top if the “daemonize” option is set to be false in the configuration YAML file.

The logger output format is changed to add the time, as this is more helpful for debugging.

Uses Dante for daemonizing.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/breaktime/main.rb', line 105

def startd
  dante_opts = {:daemonize => @options['daemonize'],
                :pid_path => @options['pid_path'],
                :log_path => @options['log_path']}

  if dante_opts[:daemonize]
    if not fork_implemented?
      dante_opts[:daemonize] = false
      @log.warn { "Daemonizing is not available, as fork is not implemented - running on top" }
    else
      @log.info { "Starting daemon, PID file => #{dante_opts[:pid_path]}, log file => #{dante_opts[:log_path]}" }
    end
  end

  schedule = Breaktime::Schedule.new(@options['interval'], @options['days'], @cli.options, @log)

  Dante::Runner.new('breaktime').execute(dante_opts) do
    schedule.start
  end
end

#stopdObject

Stop the daemonized process, if it is running.



127
128
129
130
131
# File 'lib/breaktime/main.rb', line 127

def stopd
  dante_opts = {:kill => true,
                :pid_path => @options['pid_path']}
  Dante::Runner.new('breaktime').execute(dante_opts)
end