Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Moved Titan::Thread.pid_files and Titan::Thread.check_filesystem to T…
Browse files Browse the repository at this point in the history
…itan::System
  • Loading branch information
flippingbits committed Feb 7, 2011
1 parent 0c76a01 commit ff553dd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 50 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.4.0.dev

* moved Titan::Thread.pid\_files and Titan::Thread.check\_filesystem
to Titan::System

- Features

* Added Titan::System that provides common methods
Expand Down
2 changes: 2 additions & 0 deletions lib/titan.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "titan/version"

module Titan
TITAN_DIRECTORY = File.expand_path('.titan_threads', '~')

autoload :CLI, "titan/cli"
autoload :System, "titan/system"
autoload :Thread, "titan/thread"
Expand Down
14 changes: 14 additions & 0 deletions lib/titan/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ def ps(command, pid)
nil
end
end

#
# Checks the file system for neccessary directories and permissions
#
def check_filesystem
Dir.mkdir(Titan::TITAN_DIRECTORY) unless File.directory?(Titan::TITAN_DIRECTORY)
end

#
# Returns a list of all pid files available in the Titan::TITAN_DIRECTORY
#
def pid_files
Dir.entries(Titan::TITAN_DIRECTORY) - [".", ".."]
end
end
end
end
26 changes: 5 additions & 21 deletions lib/titan/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Titan
# that gets created automatically.
#
class Thread
TITAN_DIRECTORY = File.expand_path('.titan_threads', '~')

attr_accessor :id, :pid

@@threads = {}
Expand Down Expand Up @@ -53,7 +51,7 @@ def id=(id)
# Returns the file where its pid gets saved
#
def pid_file
File.expand_path(@id.to_s + ".pid", Titan::Thread::TITAN_DIRECTORY)
File.expand_path(@id.to_s + ".pid", TITAN_DIRECTORY)
end

#
Expand All @@ -76,7 +74,7 @@ def used_cpu
# Opens the pid file and save its pid in it
#
def save
Titan::Thread.check_filesystem
Titan::System.check_filesystem
File.open(pid_file, 'w') { |file| file.write(@pid) }
@@threads[@id] = self
end
Expand Down Expand Up @@ -123,8 +121,8 @@ def all
# Loads threads from pid files inside the TITAN_DIRECTORY
#
def load_threads
check_filesystem
pid_files.each{ |pid_file|
Titan::System.check_filesystem
Titan::System.pid_files.each{ |pid_file|
thread = Titan::Thread.new(:id => File.basename(pid_file, ".pid"))
thread.pid = File.read(File.expand_path(pid_file, TITAN_DIRECTORY)).to_i
@@threads[thread.id] = thread
Expand All @@ -135,7 +133,7 @@ def load_threads
# Saves threads to pid files inside the TITAN_DIRECTORY
#
def save_threads
pid_files.each { |pid_file| File.delete(File.expand_path(pid_file, TITAN_DIRECTORY)) }
Titan::System.pid_files.each { |pid_file| File.delete(File.expand_path(pid_file, TITAN_DIRECTORY)) }
@@threads.each_value { |thread| thread.save }
end

Expand All @@ -147,20 +145,6 @@ def remove_dead_threads
save_threads
@@threads
end

#
# Checks the file system for neccessary directories and permissions
#
def check_filesystem
Dir.mkdir(TITAN_DIRECTORY) unless File.directory?(TITAN_DIRECTORY)
end

#
# Returns a list of all pid files available in the TITAN_DIRECTORY
#
def pid_files
Dir.entries(TITAN_DIRECTORY) - [".", ".."]
end
end
end
end
25 changes: 25 additions & 0 deletions spec/titan/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,29 @@
end
end
end

describe ".check_file_system" do
before(:each) do
File.stub!(:directory?).and_return(false)
end

it "should create the directory" do
Dir.should_receive(:mkdir).with(Titan::TITAN_DIRECTORY)
Titan::System.check_filesystem
end
end

describe ".pid_files" do
it "should return an Array" do
Titan::System.pid_files.should be_an(Array)
end

it "should not include ." do
Titan::System.pid_files.should_not include(".")
end

it "should not include .." do
Titan::System.pid_files.should_not include("..")
end
end
end
38 changes: 9 additions & 29 deletions spec/titan/thread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def new_thread(id=nil)
Titan::Thread.__send__("class_variable_set", "@@threads", {})
end

describe "TITAN_DIRECTORY" do
describe "Titan::TITAN_DIRECTORY" do
it "should get stored in the home directory" do
File.dirname(Titan::Thread::TITAN_DIRECTORY).should eql(File.expand_path('~'))
File.dirname(Titan::TITAN_DIRECTORY).should eql(File.expand_path('~'))
end
end

Expand Down Expand Up @@ -98,7 +98,7 @@ def new_thread(id=nil)
end

it "should return a file named like its ids" do
@thread.pid_file.should eql(File.expand_path(@thread.id.to_s + ".pid", Titan::Thread::TITAN_DIRECTORY))
@thread.pid_file.should eql(File.expand_path(@thread.id.to_s + ".pid", Titan::TITAN_DIRECTORY))
end
end

Expand Down Expand Up @@ -161,6 +161,11 @@ def new_thread(id=nil)
@thread = new_thread
end

it "should check the file system" do
Titan::System.should_receive(:check_filesystem)
@thread.save
end

it "should open its pid file" do
File.should_receive(:open).with(@thread.pid_file, 'w')
@thread.save
Expand Down Expand Up @@ -223,7 +228,7 @@ def new_thread(id=nil)
end

it "should check the file system" do
Titan::Thread.should_receive(:check_filesystem)
Titan::System.should_receive(:check_filesystem)
Titan::Thread.load_threads
end

Expand Down Expand Up @@ -263,29 +268,4 @@ def new_thread(id=nil)
Titan::Thread.remove_dead_threads.should equal(Titan::Thread.all)
end
end

describe ".check_file_system" do
before(:each) do
File.stub!(:directory?).and_return(false)
end

it "should create the directory" do
Dir.should_receive(:mkdir).with(Titan::Thread::TITAN_DIRECTORY)
Titan::Thread.check_filesystem
end
end

describe ".pid_files" do
it "should return an Array" do
Titan::Thread.pid_files.should be_an(Array)
end

it "should not include ." do
Titan::Thread.pid_files.should_not include(".")
end

it "should not include .." do
Titan::Thread.pid_files.should_not include("..")
end
end
end

0 comments on commit ff553dd

Please sign in to comment.