gregfitz23-chrono_trigger 0.0.5 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -13,13 +13,14 @@ A cron framework for defining cron tasks using a readable DSL.
13
13
  Create trigger files (by default in the lib/triggers) directory.
14
14
  Triggers should follow the pattern:
15
15
 
16
+ do
16
17
  trigger "name" do
17
18
  runs { code to execute }
18
19
  on :monday
19
20
  every :minutes=>10
20
21
  at :hour=>9, :minute=>[30,50]
21
22
  end
22
-
23
+ end
23
24
  Run chrono_trigger -t{full path to trigger file}
24
25
 
25
26
  == REQUIREMENTS:
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 5
2
+ :patch: 0
3
3
  :major: 0
4
- :minor: 0
4
+ :minor: 1
@@ -5,7 +5,7 @@ module ChronoTrigger
5
5
  VERSION = '0.0.2'
6
6
  end
7
7
 
8
- require "activesupport"
8
+ require "activesupport" unless defined? ActiveSupport
9
9
  require "chrono_trigger/shell"
10
10
  require "chrono_trigger/trigger"
11
11
  require "chrono_trigger/cron_entry"
@@ -4,6 +4,8 @@ module ChronoTrigger
4
4
 
5
5
  def run(options={})
6
6
  @t = Thread.new do
7
+ setup(options)
8
+
7
9
  shell = ChronoTrigger::Shell.new
8
10
  shell.load_triggers(options[:trigger_file])
9
11
  loop do
@@ -18,5 +20,16 @@ module ChronoTrigger
18
20
  def stop
19
21
  @t.exit
20
22
  end
23
+
24
+ private
25
+ def setup(options={})
26
+ if application_context = options[:application_context]
27
+ ENV['RAILS_ENV'] = options[:env] || "development"
28
+
29
+ application_path = File.join(application_context, 'config', 'environment')
30
+ STDOUT.puts "Loading application environment at #{File.join(application_context, 'config', 'environment')} for '#{ENV['RAILS_ENV']}' enviroment."
31
+ require(application_path)
32
+ end
33
+ end
21
34
  end
22
35
  end
@@ -1,5 +1,5 @@
1
1
  require File.join(File.dirname(__FILE__), 'process')
2
- require "chrono_trigger"
2
+ require "logger"
3
3
  require 'optparse'
4
4
  require 'yaml'
5
5
 
@@ -31,7 +31,7 @@ module ChronoTrigger
31
31
  pid = @process.running?
32
32
  if pid
33
33
  if options[:force]
34
- STDERR.puts "Shutting down existing ChronoTrigger."
34
+ STDOUT.puts "Shutting down existing ChronoTrigger."
35
35
  @process.kill
36
36
  @process = ProcessHelper.new(options[:logger], options[:pid_file], options[:user], options[:group])
37
37
  else
@@ -49,13 +49,14 @@ module ChronoTrigger
49
49
  self.options = {
50
50
  :log_level => Logger::INFO,
51
51
  :daemonize => false,
52
- :pid_file => File.join('', 'var', 'run', 'chrono_trigger.pid')
52
+ :pid_file => File.join('', 'var', 'run', 'chrono_trigger.pid'),
53
+ :env => "development"
53
54
  }
54
55
 
55
56
  OptionParser.new do |opts|
56
57
  opts.summary_width = 25
57
58
 
58
- opts.banner = "ChronoTrigger\n\n",
59
+ opts.banner = "ChronoTrigger - Execute cron jobs within the context of a Rails application\n\n",
59
60
  "usage: chrono_trigger [options...]\n",
60
61
  " chrono_trigger --help\n",
61
62
  " chrono_trigger --version\n"
@@ -63,18 +64,29 @@ module ChronoTrigger
63
64
  opts.separator ""
64
65
  opts.separator ""; opts.separator "ChronoTrigger Options:"
65
66
 
66
- opts.on("-tTRIGGER_FILE", "--triggers TRIGGERS", "Path to file specifying triggers to be executed") do |trigger_file|
67
+ opts.on("-tTRIGGER_FILE", "--triggers TRIGGERS", "Path to file specifying triggers to be executed. When also specifying -a, this path will be relative to the application path") do |trigger_file|
67
68
  options[:trigger_file] = trigger_file
68
69
  end
69
70
 
70
- opts.on("-f", "--force", "Force restart of ChronoTrigger process.") do
71
+ opts.on("-f", "--force", "Force restart of ChronoTrigger process (can be used in conjunction with -P).") do
71
72
  options[:force] = true
72
73
  end
73
74
 
74
- opts.on("-s", "--stop", "Stop a currently running ChronoTrigger process.") do
75
+ opts.on("-s", "--stop", "Stop a currently running ChronoTrigger process (can be used in conjunction with -P).") do
75
76
  options[:stop] = true
76
77
  end
77
78
 
79
+ opts.separator ""
80
+ opts.separator ""; opts.separator "Rails options:"
81
+
82
+ opts.on("-aAPPLICATION", "--application RAILS", "Path to Rails application context to execture triggers in.") do |application_context|
83
+ options[:application_context] = application_context
84
+ end
85
+
86
+ opts.on("-eENVIRONMENT", "--environment ENVIRONMENT", "Rails environment to execute triggers in.") do |environment|
87
+ options[:env] = environment
88
+ end
89
+
78
90
  opts.separator ""
79
91
  opts.separator ""; opts.separator "Process:"
80
92
 
@@ -111,6 +123,10 @@ module ChronoTrigger
111
123
 
112
124
  @process.daemonize if options[:daemonize]
113
125
 
126
+ if application_context = options[:application_context]
127
+ Dir.chdir(application_context)
128
+ end
129
+
114
130
  setup_signal_traps
115
131
  @process.write_pid_file
116
132
 
@@ -25,7 +25,6 @@ module ChronoTrigger
25
25
  #Run execute on any trigger who's cron entry matches the current time.
26
26
  def execute_triggers
27
27
  now = Time.now
28
- STDOUT.puts "triggers: #{triggers}"
29
28
  triggers.map {|trigger| trigger.execute_on_match(now)}
30
29
  end
31
30
 
@@ -55,7 +55,6 @@ module ChronoTrigger
55
55
  end
56
56
 
57
57
  def execute
58
- STDOUT.puts "executing #{@exec_block}"
59
58
  @exec_block.call
60
59
  end
61
60
 
@@ -1,5 +1,5 @@
1
1
  trigger "trigger1" do
2
- runs { File.open("/tmp/test_#{Time.now}.txt", 'w') {|file| file.puts "trigger 1 runs every 1 minutes; executed at #{Time.now}"}}
2
+ runs { puts "trigger 1 runs every 1 minutes; executed at #{Time.now}"}}
3
3
  every :minutes=>1
4
4
  end
5
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gregfitz23-chrono_trigger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Fitzgerald