Module: Scheduler

Defined in:
lib/scheduler.rb,
lib/scheduler/cli.rb,
lib/scheduler/version.rb,
lib/scheduler/schedulable.rb,
lib/scheduler/main_process.rb,
lib/scheduler/configuration.rb,
lib/scheduler/examples/executable_class.rb,
lib/scheduler/examples/schedulable_model.rb

Defined Under Namespace

Modules: Examples, Schedulable Classes: CLI, Configuration, Error, MainProcess

Constant Summary collapse

VERSION =
"1.0.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationScheduler::Configuration

Initializes configuration.

Returns:



18
19
20
# File 'lib/scheduler.rb', line 18

def configuration
  @configuration
end

Class Method Details

.configure {|@configuration| ... } ⇒ nil

Method to configure various Scheduler options.

Yields:

Returns:

  • (nil)


32
33
34
35
# File 'lib/scheduler.rb', line 32

def configure
  @configuration ||= Scheduler::Configuration.new
  yield @configuration
end

.envString

Returns current environment.

Returns:

  • (String)

    Scheduler environment.



49
50
51
# File 'lib/scheduler.rb', line 49

def env
  Scheduler.configuration.environment
end

.loggerLogger

Return the Scheduler logger.

Returns:

  • (Logger)

    the configured logger.



81
82
83
# File 'lib/scheduler.rb', line 81

def logger
  @@logger ||= Logger.new Scheduler.configuration.log_file
end

.perform_jobs_in_test_or_development?Boolean

Checks whether to run jobs in test or development.

Returns:

  • (Boolean)

    to run jobs.



73
74
75
# File 'lib/scheduler.rb', line 73

def perform_jobs_in_test_or_development?
  Scheduler.configuration.perform_jobs_in_test_or_development
end

.pidInteger

Gets scheduler main process pid.

Returns:

  • (Integer)

    main process pid.



65
66
67
# File 'lib/scheduler.rb', line 65

def pid
  File.read(self.pid_file).to_i rescue nil
end

.pid_fileString

Gets scheduler pid file.

Returns:

  • (String)

    the pid file.



57
58
59
# File 'lib/scheduler.rb', line 57

def pid_file
  '/tmp/scheduler.pid'
end

.restartnil

Restarts the scheduler.

Returns:

  • (nil)


124
125
126
127
# File 'lib/scheduler.rb', line 124

def restart
  self.stop
  self.start
end

.rootString

Returns Scheduler gem root path.

Returns:

  • (String)

    Scheduler gem root path.



41
42
43
# File 'lib/scheduler.rb', line 41

def root
  File.dirname __dir__
end

.startnil

Starts a Scheduler::MainProcess in a separate process.

Returns:

  • (nil)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/scheduler.rb', line 89

def start
  logger.info Rainbow("[Scheduler:#{Process.pid}] Starting..").cyan
  scheduler_pid = Process.fork do
    begin
      logger.info Rainbow("[Scheduler:#{Process.pid}] Forked.").cyan
      Process.daemon true, true
      logger.info Rainbow("[Scheduler:#{Process.pid}] Going into background..").cyan
      File.open(self.pid_file, 'w+') do |pidfile|
        pidfile.puts Process.pid
      end
      scheduler = Scheduler::MainProcess.new
    rescue StandardError => error
      puts Rainbow("#{error.class}: #{error.message} (#{error.backtrace.first})").red
    end
  end
  Process.detach(scheduler_pid)
  scheduler_pid
end

.stopnil

Reschedules all running jobs and stops the scheduler main process.

Returns:

  • (nil)


112
113
114
115
116
117
118
# File 'lib/scheduler.rb', line 112

def stop
  begin
    Process.kill :TERM, Scheduler.pid
    FileUtils.rm(self.pid_file)
  rescue TypeError, Errno::ENOENT, Errno::ESRCH
  end
end