fume 0.3.2 → 0.3.3

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.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :build:
3
3
  :minor: 3
4
- :patch: 2
4
+ :patch: 3
5
5
  :major: 0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fume}
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sunteya"]
12
- s.date = %q{2010-01-21}
12
+ s.date = %q{2010-01-25}
13
13
  s.email = %q{Sunteya@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README"
@@ -62,13 +62,18 @@ Gem::Specification.new do |s|
62
62
  "generators/fume/simple_theme/templates/public/stylesheets/src/vendor/yui-3.0/reset.css",
63
63
  "lib/fume.rb",
64
64
  "lib/fume/authlogic.rb",
65
+ "lib/fume/daemon.rb",
65
66
  "lib/fume/formtastic.rb",
67
+ "lib/fume/logger_support.rb",
66
68
  "lib/fume/rails_ext.rb",
67
69
  "lib/fume/searchlogic.rb",
68
70
  "lib/fume/simple_theme.rb",
69
71
  "lib/fume/smart_logger.rb",
70
72
  "rails/init.rb",
71
- "spec/smart_logger_spec.rb",
73
+ "spec/fume/daemon_spec.rb",
74
+ "spec/fume/logger_support_spec.rb",
75
+ "spec/fume/smart_logger_spec.rb",
76
+ "spec/rails_stub.rb",
72
77
  "spec/spec.opts",
73
78
  "spec/spec_helper.rb"
74
79
  ]
@@ -78,7 +83,10 @@ Gem::Specification.new do |s|
78
83
  s.rubygems_version = %q{1.3.5}
79
84
  s.summary = %q{a simple rails scaffold.}
80
85
  s.test_files = [
81
- "spec/smart_logger_spec.rb",
86
+ "spec/fume/daemon_spec.rb",
87
+ "spec/fume/logger_support_spec.rb",
88
+ "spec/fume/smart_logger_spec.rb",
89
+ "spec/rails_stub.rb",
82
90
  "spec/spec_helper.rb"
83
91
  ]
84
92
 
@@ -4,6 +4,8 @@ require 'fume/simple_theme'
4
4
  require 'fume/rails_ext'
5
5
  require 'fume/formtastic'
6
6
  require 'fume/smart_logger'
7
+ require 'fume/logger_support'
8
+ require 'fume/daemon'
7
9
 
8
10
  # module Fume
9
11
  # def self.root
@@ -0,0 +1,85 @@
1
+ require "fileutils"
2
+
3
+ module Fume
4
+ module Daemon
5
+
6
+ def self.included(base)
7
+ base.instance_eval do
8
+ include LoggerSupport
9
+
10
+ include InstanceMethods
11
+ extend ClassMethods
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ end
17
+
18
+ module InstanceMethods
19
+ attr_accessor :pid_file_path
20
+
21
+ def initialize
22
+ self.pid_file_path = Rails.root.join("tmp", "pids", "#{daemon_name}.pid")
23
+ end
24
+
25
+ def daemon_name
26
+ self.class.name
27
+ end
28
+
29
+ def register_trap
30
+ %w{TERM INT HUP}.each do |signal_name|
31
+ Signal.trap(signal_name) do
32
+ self.stop
33
+ end
34
+ end
35
+ end
36
+
37
+ def pid_file_path
38
+ ENV["PID_FILE"] || @pid_file_path
39
+ end
40
+
41
+ def write_pid_file(pid)
42
+ if self.pid_file_path
43
+ File.open(self.pid_file_path, "w") { |f| f << pid }
44
+ end
45
+ end
46
+
47
+ def remove_pid_file
48
+ FileUtils.rm(pid_file_path, :force => true) if self.pid_file_path && File.exists?(self.pid_file_path)
49
+ end
50
+
51
+ def already_running?
52
+ if self.pid_file_path && File.exists?(self.pid_file_path)
53
+ pid = File.read(self.pid_file_path)
54
+ return Process.kill(0, pid.to_i) == 1 if pid # test running
55
+ end
56
+ return false
57
+ rescue Errno::ESRCH # NOT running or zombied
58
+ return false
59
+ end
60
+
61
+ def start
62
+ return if already_running?
63
+
64
+ register_trap
65
+ write_pid_file(Process.pid)
66
+ logger.info { "Daemon: #{daemon_name} starting ... PID: #{Process.pid}" }
67
+
68
+ @run = true
69
+
70
+ while @run
71
+ self.execute
72
+ end
73
+ logger.info { "Daemon: #{daemon_name} stoped ..." }
74
+ rescue Exception => e
75
+ logger.error(e)
76
+ ensure
77
+ remove_pid_file
78
+ end
79
+
80
+ def stop
81
+ @run = false
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,22 @@
1
+ module Fume
2
+ module LoggerSupport
3
+ def self.included(base)
4
+ base.instance_eval do
5
+ extend ClassMethods
6
+ include InstanceMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def logger
12
+ @logger ||= Rails.logger || Logger.new(STDOUT)
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+ def logger
18
+ @logger ||= self.class.logger
19
+ end
20
+ end
21
+ end
22
+ end
@@ -4,12 +4,18 @@ module Fume
4
4
  def self.try_enable
5
5
  if defined? ::Searchlogic
6
6
  ActionController::Base.send :helper, RailsHelpers
7
- ::Searchlogic::Search.send :include, SearchExtensions::InstanceMethods
8
- ::Searchlogic::Search.send :extend, SearchExtensions::ClassMethods
7
+ ::Searchlogic::Search.send :include, SearchExtensions
9
8
  end
10
9
  end
11
10
 
12
11
  module SearchExtensions
12
+ def self.included(base)
13
+ base.instance_eval do
14
+ include InstanceMethods
15
+ extend ClassMethods
16
+ end
17
+ end
18
+
13
19
  module ClassMethods
14
20
  def human_name
15
21
  "Search"
@@ -7,11 +7,11 @@ module Fume
7
7
  when "STDOUT"
8
8
  Logger.new(STDOUT)
9
9
  when /(\w+)/
10
- Logger.new("log/#{$1}.log")
10
+ Logger.new(Rails.root.join("log", "#{$1}.log"))
11
11
  when /(\w+\.\w+)/
12
- Logger.new("log/#{$1}")
12
+ Logger.new(Rails.root.join("log", $1))
13
13
  else
14
- Logger.new("log/#{Rails.env}.log")
14
+ Logger.new(Rails.root.join("log", "#{Rails.env}.log"))
15
15
  end
16
16
  end
17
17
  end
@@ -0,0 +1,88 @@
1
+ require "spec_helper"
2
+
3
+ describe Fume::Daemon do
4
+ class TestDaemon
5
+ include Fume::Daemon
6
+
7
+ attr_writer :callback
8
+
9
+ def callback(&block)
10
+ if block
11
+ @callback = block
12
+ else
13
+ @callback
14
+ end
15
+ end
16
+
17
+ def execute
18
+ callback.call if callback
19
+ sleep 0.05
20
+ end
21
+ end
22
+
23
+ before(:each) do
24
+ ENV["PID_FILE"] = nil
25
+ @pid_path = Rails.root.join("tmp", "pids", "test.pid").to_s
26
+ end
27
+
28
+ it "should be create pid file by path" do
29
+ build_and_start_test_daemon do |d|
30
+ d.callback { File.exists?(@pid_path).should be_true }
31
+ d.pid_file_path = @pid_path
32
+ end
33
+ File.exists?(@pid_path).should be_false
34
+ end
35
+
36
+ it "should be create pid file by ENV" do
37
+ ENV["PID_FILE"] = @pid_path
38
+ build_and_start_test_daemon do |d|
39
+ d.callback { File.exists?(@pid_path).should be_true }
40
+ end
41
+ File.exists?(@pid_path).should be_false
42
+ end
43
+
44
+ it "should be create pid file by default" do
45
+ build_and_start_test_daemon do |d|
46
+ d.pid_file_path.should_not be_nil
47
+ d.callback { File.exists?(d.pid_file_path).should be_true }
48
+ end
49
+ end
50
+
51
+ it "should be not create pid file" do
52
+ build_and_start_test_daemon do |d|
53
+ pid_path = d.pid_file_path
54
+ d.pid_file_path = nil
55
+ d.callback { File.exists?(pid_path).should be_false }
56
+ end
57
+ end
58
+
59
+ it "should be catch exception" do
60
+ daemon = build_and_start_test_daemon do |d|
61
+ d.callback { raise "exception" }
62
+ end
63
+
64
+ File.exists?(daemon.pid_file_path).should be_false
65
+ end
66
+
67
+ it "should be exit if pid file exists" do
68
+ build_and_start_test_daemon do |d|
69
+ d.pid_file_path = @pid_path
70
+ d.write_pid_file(Process.pid)
71
+ d.callback { fail("MUST exit at start") }
72
+ end
73
+ end
74
+
75
+ def build_and_start_test_daemon
76
+ Thread.new do
77
+ sleep 0.5
78
+ Process.kill("INT", $$)
79
+ end
80
+
81
+ daemon = TestDaemon.new
82
+ daemon.logger.level = Logger::FATAL
83
+ yield(daemon) if block_given?
84
+
85
+ daemon.start
86
+ daemon
87
+ end
88
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe Fume::LoggerSupport do
4
+ class TestLog
5
+ include Fume::LoggerSupport
6
+ end
7
+
8
+ it "should be return rails logger" do
9
+ Rails.logger = "Test"
10
+ TestLog.logger.should be_eql "Test"
11
+ end
12
+
13
+
14
+ it "should be return STDOUT logger" do
15
+ Rails.logger = nil
16
+ TestLog.logger.should_not be_nil
17
+ end
18
+ end
@@ -1,11 +1,5 @@
1
1
  require "spec_helper"
2
2
 
3
- module Rails
4
- class << self
5
- attr_accessor :env
6
- end
7
- end
8
-
9
3
  describe Fume::SmartLogger do
10
4
  before(:each) do
11
5
  @logger = Logger.new(STDOUT)
@@ -17,17 +11,17 @@ describe Fume::SmartLogger do
17
11
  end
18
12
 
19
13
  it "should be create log file logger" do
20
- Logger.should_receive(:new).with("log/file.log").and_return(@logger)
14
+ Logger.should_receive(:new).with(Rails.root.join("log", "file.log")).and_return(@logger)
21
15
  SmartLogger.create("file").should be_eql @logger
22
16
  end
23
17
 
24
18
  it "should be create log file logger with ext" do
25
- Logger.should_receive(:new).with("log/file.log").and_return(@logger)
19
+ Logger.should_receive(:new).with(Rails.root.join("log", "file.log")).and_return(@logger)
26
20
  SmartLogger.create("file.log").should be_eql @logger
27
21
  end
28
22
 
29
23
  it "should be create log file by Rails env" do
30
- Logger.should_receive(:new).with("log/development.log").and_return(@logger)
24
+ Logger.should_receive(:new).with(Rails.root.join("log", "development.log")).and_return(@logger)
31
25
  Rails.env = "development"
32
26
  SmartLogger.create(nil).should be_eql @logger
33
27
  end
@@ -0,0 +1,9 @@
1
+ module Rails
2
+ class << self
3
+ attr_accessor :env, :logger
4
+
5
+ def root
6
+ @root ||= Pathname.new(File.dirname(__FILE__)).join("..")
7
+ end
8
+ end
9
+ end
@@ -2,8 +2,7 @@
2
2
  # require 'spec'
3
3
 
4
4
  require File.join(File.dirname(__FILE__), '..', 'lib', 'fume')
5
-
6
-
5
+ require "rails_stub"
7
6
 
8
7
  include Fume
9
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fume
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sunteya
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-21 00:00:00 +08:00
12
+ date: 2010-01-25 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -69,13 +69,18 @@ files:
69
69
  - generators/fume/simple_theme/templates/public/stylesheets/src/vendor/yui-3.0/reset.css
70
70
  - lib/fume.rb
71
71
  - lib/fume/authlogic.rb
72
+ - lib/fume/daemon.rb
72
73
  - lib/fume/formtastic.rb
74
+ - lib/fume/logger_support.rb
73
75
  - lib/fume/rails_ext.rb
74
76
  - lib/fume/searchlogic.rb
75
77
  - lib/fume/simple_theme.rb
76
78
  - lib/fume/smart_logger.rb
77
79
  - rails/init.rb
78
- - spec/smart_logger_spec.rb
80
+ - spec/fume/daemon_spec.rb
81
+ - spec/fume/logger_support_spec.rb
82
+ - spec/fume/smart_logger_spec.rb
83
+ - spec/rails_stub.rb
79
84
  - spec/spec.opts
80
85
  - spec/spec_helper.rb
81
86
  has_rdoc: true
@@ -107,5 +112,8 @@ signing_key:
107
112
  specification_version: 3
108
113
  summary: a simple rails scaffold.
109
114
  test_files:
110
- - spec/smart_logger_spec.rb
115
+ - spec/fume/daemon_spec.rb
116
+ - spec/fume/logger_support_spec.rb
117
+ - spec/fume/smart_logger_spec.rb
118
+ - spec/rails_stub.rb
111
119
  - spec/spec_helper.rb