Permalink
Browse files

Working sync command

  • Loading branch information...
1 parent 9bfbc15 commit aa41a8281316622c55a60564050f6ccd64586b12 @diedthreetimes committed Jan 4, 2012
@@ -22,3 +22,4 @@ def config
require 'ip_tracker/cli/methods/register'
require 'ip_tracker/cli/methods/update'
+require 'ip_tracker/cli/methods/sync'
@@ -0,0 +1,27 @@
+module IpTracker
+ class CLI
+ desc "sync", "continuously sync the ip with IpMe"
+ method_option :start, :type => :boolean, :desc => "Command start", :default => true
+ method_option :stop, :type => :boolean, :desc => "Command stop", :default => false
+ method_option :daemon, :aliases => ["b"], :default => false, :desc => "Run this as a daemon, only works with start"
+
+ def sync
+ command = options[:stop] ? :stop : :start
+ daemonize = options[:daemon]
+
+ case command
+ when :start
+ if config.pid
+ say "IpMe is already running."
+ else
+ SyncDaemon.new.run
+
+
+ end
+ when :stop
+
+ end
+
+ end
+ end
+end
@@ -11,6 +11,10 @@ def host_token
config_hash["host_token"] || nil
end
+ def pid
+ config_hash["pid"] || nil
+ end
+
def update(attr, value)
config_hash[attr.to_s] = value
@@ -25,14 +25,14 @@ def run
new_ip = local_ip
if @last_ip != new_ip
+ puts "#{Time.now}: Updating to #{new_ip}"
update_ip new_ip
@last_ip = new_ip
end
- rescue
- # TODO: Figure out what error is raised by start
- puts "Some terrible error occured"
- @run = false
+ rescue Client::TargetError
+ puts "An error occured trying to communicate with the server, sleeping"
+ sleep(6000) unless !@runs.nil?
end
end
end
@@ -48,9 +48,17 @@ def local_ip
Socket.do_not_reverse_lookup = orig
end
+ def update_ip ip
+ client.update(config.host_token, :ip, ip)
+ # CLI.start( ['update', '--ip', ip] )
+ end
+
+ def config
+ @config ||= Config.new
+ end
- def update_ip ip
- CLI.start( ['update', '--ip', ip] )
+ def client
+ @client ||= Client.new
end
end
end
@@ -0,0 +1,56 @@
+require 'spec_helper'
+require 'ip_tracker'
+
+describe IpTracker::CLI do
+ describe '#sync' do
+
+ def stub_config(update=false)
+ config = mock_config
+ config.should_receive(:update).with(:pid, 34) if update
+ config.should_receive(:pid).and_return(nil)
+
+ daemon = double(IpTracker::SyncDaemon, {})
+ IpTracker::SyncDaemon.stub(:new) { daemon }
+ daemon.should_receive(:run)
+ end
+ describe "start" do
+ context "when there are no options" do
+ let(:sync) { IpTracker::CLI.start(['sync']) }
+
+ it "should call sync_daemon" do
+ stub_config
+
+ sync
+ end
+
+ it "prints an error when a pid is saved" do
+ mock_config.should_receive(:pid).and_return(343)
+ IpTracker::SyncDaemon.should_not_receive(:new)
+
+ results = capture(:stdout) { sync }
+ results.should match /IpMe is already running./
+ end
+ end
+
+ context "when daemonize is provided" do
+ let(:sync) { IpTracker::CLI.start(['sync', '--start', '-d']) }
+
+ it "should detach and store a pid" do
+ # stub_config(true)
+ end
+ end
+ end
+
+ describe "#stop" do
+ context "when a pid is saved" do
+ it "should send sigterm to the daemon and delete the pid" do
+
+ end
+ end
+
+ it "should print an error when a pid file doesn't exist" do
+
+ end
+ end
+ end
+end
@@ -9,14 +9,22 @@
it "should update the ip exactly once" do
UDPSocket.should_receive(:open).exactly(6).times.and_return(mock_ip)
- IpTracker::CLI.should_receive(:start).with(['update', '--ip', mock_ip])
+ mock_config.should_receive(:host_token).and_return(6)
+ mock_client.should_receive(:update).with(6, :ip, mock_ip)
results = capture(:stdout) { run }
results.should match /process started/
+ results.should_not match /error/
end
it "should degrade gracefully upon error" do
- # run
+ UDPSocket.should_receive(:open).exactly(6).times.and_return(mock_ip)
+
+ mock_config.should_receive(:host_token).at_least(1).times.and_return(6)
+ mock_client.should_receive(:update).at_least(1).times.with(6, :ip, mock_ip) { raise IpTracker::Client::TargetError }
+
+ results = capture(:stdout){ run }
+ results.should match /error/
end
end
end

0 comments on commit aa41a82

Please sign in to comment.