scccp 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/scccp/scp.rb CHANGED
@@ -3,6 +3,8 @@ require 'fileutils'
3
3
 
4
4
  module Scccp
5
5
  class Scp
6
+ SIGNALS = [ :QUIT, :INT, :TERM, :USR1, :USR2, :HUP ]
7
+
6
8
  ATTRS = [
7
9
  :remote_host,
8
10
  :remote_user_name,
@@ -12,6 +14,7 @@ module Scccp
12
14
  :ok_folder,
13
15
  :ng_folder,
14
16
  :timeout,
17
+ :lockfile,
15
18
  :logger2ssh,
16
19
  :logger
17
20
  ]
@@ -30,7 +33,7 @@ module Scccp
30
33
  #v = instance_variable_get("@#{attr.to_s}")
31
34
  v = send(attr)
32
35
  case attr
33
- when :remote_user_name,:remote_user_password,:timeout,:logger2ssh
36
+ when :remote_user_name,:remote_user_password,:timeout,:logger2ssh,:lockfile
34
37
  next
35
38
  when :queue_folder,:ok_folder,:ng_folder
36
39
  unless File.directory?(v)
@@ -45,8 +48,14 @@ module Scccp
45
48
  true
46
49
  end
47
50
 
51
+ def receive_signal(signal)
52
+ @killing = true
53
+ end
54
+
48
55
  def proceed
49
56
  attrs_ok?
57
+ SIGNALS.each { |sig| trap(sig){receive_signal(sig)} }
58
+
50
59
  opt = {}
51
60
  if remote_user_password
52
61
  opt[:password] = remote_user_password
@@ -64,15 +73,25 @@ module Scccp
64
73
  !(o =~ /\.(tmp|ok)$/)
65
74
  end
66
75
 
76
+ if lockfile
77
+ if File.exists?(lockfile)
78
+ logger.warn("lockfile:#{lockfile} already exists")
79
+ return :lockfile_error
80
+ end
81
+ File.open(lockfile, "w"){|f|f.write $$}
82
+ end
83
+
67
84
  logger.info("queue_folder is #{queue_folder}")
68
85
  logger.info("target is #{remote_host}:#{remote_path}")
69
86
  #logger.info(files.inspect)
87
+ uploaded_count = 0
70
88
 
71
89
  begin
72
90
  # ブロックで使うと途中で失敗した場合にscpインスタンスを
73
91
  # 使いまわせない(というか固まる)のでこんな感じの使い方で
74
92
  scp = Net::SCP.start(remote_host, remote_user_name, opt)
75
93
  files.each do |file|
94
+ break if @killing
76
95
  ok_file = file + '.ok'
77
96
  begin
78
97
  logger.info "uploading_start:#{file}"
@@ -84,6 +103,7 @@ module Scccp
84
103
  logger.info "uploading_finish:#{ok_file}"
85
104
  FileUtils.mv(file,ok_folder)
86
105
  FileUtils.mv(ok_file,ok_folder)
106
+ uploaded_count += 1
87
107
  true
88
108
  rescue => e
89
109
  logger.error e
@@ -103,7 +123,12 @@ module Scccp
103
123
  if scp
104
124
  scp.session.close
105
125
  end
126
+ if lockfile
127
+ FileUtils.rm lockfile
128
+ end
106
129
  end
130
+
131
+ uploaded_count
107
132
  end
108
133
  end
109
134
  end
data/lib/scccp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Scccp
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/test/test.rb CHANGED
@@ -14,6 +14,7 @@ class TestScccp < Test::Unit::TestCase
14
14
  OK_FOLDER = "#{SCCCP_WORKING_DIR}done/"
15
15
  NG_FOLDER = "#{SCCCP_WORKING_DIR}error/"
16
16
  REMOTE_PATH = "#{SCCCP_WORKING_DIR}remote/"
17
+ LOCK_FILE = "#{SCCCP_WORKING_DIR}test.lock"
17
18
  REMOTE_USER_NAME = 'paco'
18
19
  REMOTE_USER_PASSWORD = nil
19
20
  REMOTE_HOST = "localhost"
@@ -72,6 +73,55 @@ class TestScccp < Test::Unit::TestCase
72
73
  assert_false File.exists?(NG_FOLDER + "testfile2.ok")
73
74
  end
74
75
 
76
+ # アップロードに5秒程度かかるように調整が必要
77
+ # 200個SCPで今の環境で5秒程度かかる
78
+ # スレッド立ててからsleep 1 (ロックファイルが出来る前に
79
+ # 後のプロセスが起動しないように)が良い感じで動くように
80
+ # 個々の値は将来調整しないといけないかもしれない
81
+ def prepare_heavy_test
82
+ (1..200).each do |i|
83
+ file = QUEUE_FOLDER + "#{i}.dat"
84
+ File.write(file,'test_data')
85
+ end
86
+ end
87
+
88
+
89
+ def test_signal
90
+ prepare_heavy_test
91
+
92
+ th = Thread.new do
93
+ scccp = Scccp::Scp.new(attr.merge({:lockfile=>LOCK_FILE}))
94
+ scccp.proceed
95
+ end
96
+
97
+ sleep 1
98
+
99
+ assert_equal true, File.exists?(LOCK_FILE)
100
+ `kill #{File.read(LOCK_FILE)}`
101
+
102
+ sleep 1
103
+ assert_equal false, File.exists?(LOCK_FILE)
104
+
105
+ th.join
106
+ end
107
+
108
+
109
+ def test_lockfile
110
+ prepare_heavy_test
111
+
112
+ th = Thread.new do
113
+ scccp = Scccp::Scp.new(attr.merge({:lockfile=>LOCK_FILE}))
114
+ scccp.proceed
115
+ end
116
+
117
+ sleep 1
118
+ assert_equal true, File.exists?(LOCK_FILE)
119
+
120
+ scccp2 = Scccp::Scp.new(attr.merge({:lockfile=>LOCK_FILE}))
121
+ assert_equal :lockfile_error, scccp2.proceed
122
+ th.join
123
+ end
124
+
75
125
  def test_connection_error
76
126
  scccp = Scccp::Scp.new(attr)
77
127
  scccp.remote_host = 'sefisfejisfe.sefijsefij.esfij'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scccp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-09 00:00:00.000000000 Z
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-scp