switches 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.markdown +10 -10
  2. data/VERSION +1 -1
  3. data/lib/switches.rb +41 -10
  4. data/switches.gemspec +2 -2
  5. metadata +2 -2
@@ -98,29 +98,29 @@ The switches will get applied to any role that matches <tt>/app/</tt> (so :app_m
98
98
 
99
99
  ## Throwing switches before you db:migrate ##
100
100
 
101
- I like to use Switches to turn off <tt>%w{memoization caching facebook campaign\_monitor delayed\_job}</tt> before running rake db:migrate, so I put this in <tt>lib/tasks/databases.rake</tt>:
101
+ I like to use Switches to turn off <tt>%w{memoization caching facebook campaign\_monitor delayed\_job}</tt> before running rake db:migrate, so I put this in <tt>lib/tasks/zzz\_rake_switches.rake</tt>:
102
102
 
103
- namespace :db do
104
- # what to do before db:migrate
105
- task :before_load_config do
103
+ namespace :rake_switches do
104
+ task :turn_stuff_off do
106
105
  Rake::Task['s:backup'].execute
107
106
  %w{memoization caching facebook campaign_monitor delayed_job}.each do |switch|
108
107
  Rake::Task['s:off'].execute(Rake::TaskArguments.new([:name], [switch]))
109
108
  end
110
109
  end
111
- # what to do after db:migrate
112
- task :after_load_config do
110
+ task :turn_stuff_back_on do
113
111
  Rake::Task['s:restore'].execute
114
112
  Rake::Task['cache:clear'].execute
115
113
  end
116
114
  end
117
115
 
118
- # make it happen
119
- Rake::Task['db:migrate'].enhance(['db:before_load_config']) do
120
- Rake::Task['db:after_load_config'].invoke
116
+ # modify what happens on db:migrate, etc.
117
+ [ 'db:migrate', 'your:task:if:it:needs:wrapping' ].each do |task_to_wrap|
118
+ Rake::Task[task_to_wrap].enhance(['rake_switches:turn_stuff_off']) do
119
+ Rake::Task['rake_switches:turn_stuff_back_on'].invoke
120
+ end
121
121
  end
122
122
 
123
- Now my question is: how can I wrap that in a begin/ensure block so the switches are always restored?
123
+ Note that 's:backup' and 's:restore' are not thread safe or really GFS safe, either.
124
124
 
125
125
  ## Usage ##
126
126
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -1,6 +1,6 @@
1
1
  require 'yaml'
2
- require 'rubygems'
3
- require 'activesupport'
2
+ require 'fileutils'
3
+ require 'active_support'
4
4
 
5
5
  # TODO not agnostic, expects RAILS_ROOT
6
6
 
@@ -13,6 +13,7 @@ module Switches
13
13
  CURRENT_PATH = File.join CONFIG_DIR, 'current.yml'
14
14
  DEFAULT_PATH = File.join CONFIG_DIR, 'default.yml'
15
15
  BACKUP_PATH = File.join CONFIG_DIR, 'backup.yml'
16
+ TRANSACTION_PID_PATH = File.join CONFIG_DIR, 'transaction.pid'
16
17
 
17
18
  class << self
18
19
  def say(str)
@@ -20,8 +21,6 @@ module Switches
20
21
  end
21
22
 
22
23
  def setup
23
- require 'fileutils'
24
-
25
24
  say "Making #{CONFIG_DIR}."
26
25
  FileUtils.mkdir_p CONFIG_DIR
27
26
 
@@ -68,8 +67,12 @@ module Switches
68
67
  suffix = method_name.to_s[-1,1]
69
68
  key = method_name.to_s[0..-2]
70
69
 
71
- if suffix == "?" and current.has_key?(key)
72
- current[key]
70
+ if suffix == "?"
71
+ if current.has_key?(key)
72
+ current[key] # set, so could be true or false
73
+ else
74
+ false # unset, so always false
75
+ end
73
76
  elsif suffix == "="
74
77
  current[key] = args.first
75
78
  # TEMPORARY since we're not doing a write_current here
@@ -80,8 +83,9 @@ module Switches
80
83
 
81
84
  def default
82
85
  return @_default unless @_default.nil?
83
- # say "file system read #{DEFAULT_PATH}"
84
- @_default = YAML.load(File.read(DEFAULT_PATH))
86
+ # say "file system activity #{DEFAULT_PATH}"
87
+ resolve_transaction!
88
+ @_default = YAML.load(IO.read(DEFAULT_PATH))
85
89
  @_default.stringify_keys!
86
90
  rescue Errno::ENOENT
87
91
  say "Couldn't read defaults from #{DEFAULT_PATH}."
@@ -91,9 +95,10 @@ module Switches
91
95
 
92
96
  def current
93
97
  return @_current unless @_current.nil?
98
+ resolve_transaction!
94
99
  if File.exist?(CURRENT_PATH)
95
- # say "file system read #{CURRENT_PATH}"
96
- @_current = YAML.load(File.read(CURRENT_PATH))
100
+ # say "file system activity #{CURRENT_PATH}"
101
+ @_current = YAML.load(IO.read(CURRENT_PATH))
97
102
  @_current.stringify_keys!
98
103
  else
99
104
  @_current = default.dup
@@ -143,6 +148,8 @@ module Switches
143
148
 
144
149
  def backup
145
150
  write_current
151
+ start_transaction!
152
+ # say "file system activity #{BACKUP_PATH}"
146
153
  FileUtils.cp CURRENT_PATH, BACKUP_PATH
147
154
  end
148
155
 
@@ -152,6 +159,7 @@ module Switches
152
159
  else
153
160
  raise ArgumentError, "#{BACKUP_PATH} doesn't exist."
154
161
  end
162
+ end_transaction!
155
163
  @_current = nil
156
164
  end
157
165
 
@@ -159,5 +167,28 @@ module Switches
159
167
  current # load it first!
160
168
  File.open(CURRENT_PATH, 'w') { |f| f.write current.stringify_keys.to_yaml }
161
169
  end
170
+
171
+ def transaction_pid
172
+ # say "file system activity #{TRANSACTION_PID_PATH}"
173
+ IO.readlines(TRANSACTION_PID_PATH).first.chomp.to_i if File.exists?(TRANSACTION_PID_PATH)
174
+ end
175
+
176
+ def resolve_transaction!
177
+ if transaction_pid.present? and transaction_pid != Process.pid
178
+ say "Resolving... calling restore"
179
+ restore
180
+ end
181
+ end
182
+
183
+ def start_transaction!
184
+ resolve_transaction!
185
+ say "Starting transaction"
186
+ File.open(TRANSACTION_PID_PATH, 'w') { |f| f.write Process.pid }
187
+ end
188
+
189
+ def end_transaction!
190
+ say "Finishing transaction"
191
+ FileUtils.rm_f TRANSACTION_PID_PATH
192
+ end
162
193
  end
163
194
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{switches}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Seamus Abshere"]
12
- s.date = %q{2009-11-05}
12
+ s.date = %q{2009-11-17}
13
13
  s.description = %q{
14
14
  Switches lets you turn on and off parts of your code from the commandline. There's a defaults.yml and a current.yml in the background.
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switches
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seamus Abshere
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-05 00:00:00 -05:00
12
+ date: 2009-11-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency