work_queue 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Miguel Fonseca
1
+ Copyright (c) 2009-2010 Miguel Fonseca
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.rdoc CHANGED
@@ -24,4 +24,13 @@ Run the code:
24
24
  require 'work_queue'
25
25
  wq = WorkQueue.new
26
26
  wq.enqueue_b { puts "Hello from the WorkQueue" }
27
- wq.join
27
+ wq.join
28
+
29
+ Note that you generally want to bound the resources used:
30
+
31
+ # Limit the maximum number of simultaneous worker threads
32
+ WorkQueue.new(10)
33
+ # Limit the maximum number of queued tasks
34
+ WorkQueue.new(nil,20)
35
+ # Limit the maximum time to preserve idle worker threads
36
+ WorkQueue.new(nil,nil,10)
data/lib/work_queue.rb CHANGED
@@ -6,13 +6,13 @@
6
6
  # This file contains an implementation of a work queue structure.
7
7
  #
8
8
  # == Version
9
- # 0.1.1
9
+ # 0.1.2
10
10
  #
11
11
  # == Author
12
12
  # Miguel Fonseca <fmmfonseca@gmail.com>
13
13
  #
14
14
  # == Copyright
15
- # Copyright 2009 Miguel Fonseca
15
+ # Copyright 2009-2010 Miguel Fonseca
16
16
  #
17
17
  # == License
18
18
  # MIT (see LICENSE file)
@@ -34,7 +34,7 @@ require 'timeout'
34
34
  #
35
35
  class WorkQueue
36
36
 
37
- VERSION = "0.1.1"
37
+ VERSION = "0.1.2"
38
38
 
39
39
  ##
40
40
  # Creates a new work queue with the desired parameters.
@@ -154,7 +154,7 @@ class WorkQueue
154
154
  #
155
155
  def join
156
156
  cur_threads.times { dismiss_thread }
157
- @threads.dup.each { |t| t.join }
157
+ @threads.dup.each { |thread| thread.join }
158
158
  self
159
159
  end
160
160
 
@@ -166,7 +166,7 @@ class WorkQueue
166
166
  # wq.stop
167
167
  #
168
168
  def stop
169
- @threads.dup.each { |t| t.exit.join }
169
+ @threads.dup.each { |thread| thread.exit.join }
170
170
  @tasks.clear
171
171
  self
172
172
  end
data/tasks/ftp.rake ADDED
@@ -0,0 +1,177 @@
1
+ require 'net/ftp'
2
+ require 'pathname'
3
+ require 'yaml'
4
+ require 'rubygems'
5
+ require 'rake'
6
+ require 'rake/tasklib'
7
+
8
+ module Rake
9
+
10
+ # = FTPTask
11
+ #
12
+ # == Description
13
+ # A Rake task that transfers local files to an FTP server.
14
+ #
15
+ # == Usage
16
+ # Rake::FTPTask.new do |t|
17
+ # t.host = "ftp.example.com"
18
+ # t.user_name = "user"
19
+ # t.password = "pass"
20
+ # t.path = "public_html/"
21
+ # t.upload_files = FileList["doc/**/*"].to_a
22
+ # end
23
+ #
24
+ # To avoid hard-coding the connection configuration into the source code, the task can obtain that data from a YAML file.
25
+ #
26
+ # Rake::FTPTask.new("ftp.yml") do |t|
27
+ # t.path = "public_html/"
28
+ # t.upload_files = FileList["doc/**/*"].to_a
29
+ # end
30
+ #
31
+ # # ftp.yml
32
+ # host = ftp.example.com
33
+ # user_name = user
34
+ # password = pass
35
+ # path = public_html/
36
+ #
37
+ class FTPTask < TaskLib
38
+
39
+ ##
40
+ # The address of the server (default is nil).
41
+ #
42
+ attr_accessor :host
43
+
44
+ ##
45
+ # The user name required to log into the server (default is "anonymous").
46
+ #
47
+ attr_accessor :user_name
48
+
49
+ ##
50
+ # The password required for the selected user name (default is nil).
51
+ #
52
+ attr_accessor :password
53
+
54
+ ##
55
+ # The (remote) base directory (default is "").
56
+ #
57
+ attr_accessor :path
58
+
59
+ ##
60
+ # The array of files to be included in the FTP upload (default is []).
61
+ #
62
+ attr_accessor :upload_files
63
+
64
+ ##
65
+ # The boolean to enable progress messages when true (default is false).
66
+ #
67
+ attr_accessor :verbose
68
+
69
+ ##
70
+ # Creates a new FTP task.
71
+ #
72
+ def initialize(config_file=nil)
73
+ @host = nil
74
+ @user_name = "anonymous"
75
+ @password = nil
76
+ @path = ""
77
+ @upload_files = []
78
+ @verbose = false
79
+ @ftp = nil
80
+ @history = {}
81
+ load_config(config_file) unless config_file.nil?
82
+ yield self if block_given?
83
+ define
84
+ end
85
+
86
+ ##
87
+ # Creates the tasks defined by this task lib.
88
+ #
89
+ def define
90
+ desc "Upload files to an FTP account"
91
+ task(:upload) do
92
+ connect
93
+ upload
94
+ disconnect
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ ##
101
+ # Reads configuration values from a YAML file.
102
+ #
103
+ def load_config(file)
104
+ config = YAML::load_file(file)
105
+ @host = config["host"] || @host
106
+ @user_name = config["user_name"] || @user_name
107
+ @password = config["password"] || @password
108
+ @path = config["path"] || @path
109
+ end
110
+
111
+ ##
112
+ # Establishes the FTP connection.
113
+ #
114
+ def connect
115
+ @ftp = Net::FTP.new(@host, @user_name, @password)
116
+ puts "Connected to #{@host}" if @verbose
117
+ puts "Using #{@ftp.binary ? "binary" : "text"} mode to transfer files" if @verbose
118
+ unless @path.nil? or @path.empty?
119
+ make_dirs(@path)
120
+ @ftp.chdir(@path)
121
+ puts "The working directory is now #{@ftp.getdir}" if @verbose
122
+ end
123
+ end
124
+
125
+ ##
126
+ # Closes the FTP connection. Further remote operations are impossible.
127
+ #
128
+ def disconnect
129
+ @ftp.close
130
+ puts "Disconnected" if @verbose
131
+ end
132
+
133
+ ##
134
+ # Iterates through the array of files.
135
+ #
136
+ def upload
137
+ puts "Uploading #{@upload_files.length} files..." if @verbose
138
+ @upload_files.each do |entry|
139
+ if File.directory?(entry)
140
+ make_dirs(entry)
141
+ else
142
+ put_file(entry)
143
+ end
144
+ end
145
+ end
146
+
147
+ ##
148
+ # Transfers a local file to the server, relative to the current working directory.
149
+ #
150
+ def put_file(name)
151
+ puts "Uploading file #{name}" if @verbose
152
+ path = File.dirname(name)
153
+ make_dirs(path)
154
+ @ftp.put(name,name)
155
+ end
156
+
157
+ ##
158
+ # Creates a directory and all its parent directories in the server, relative to the current working directory.
159
+ #
160
+ def make_dirs(name)
161
+ Pathname.new(name).descend do |dir|
162
+ if @history[dir].nil?
163
+ @history[dir] = true
164
+ puts "Creating directory #{dir}" if @verbose
165
+ @ftp.mkdir(dir) rescue nil
166
+ end
167
+ end
168
+ end
169
+
170
+ end
171
+
172
+ end
173
+
174
+ Rake::FTPTask.new("config/ftp.yml") do |ftp|
175
+ ftp.upload_files = FileList["doc/**/*"].to_a
176
+ ftp.verbose = true
177
+ end
@@ -9,7 +9,7 @@
9
9
  # Miguel Fonseca <fmmfonseca@gmail.com>
10
10
  #
11
11
  # == Copyright
12
- # Copyright 2009 Miguel Fonseca
12
+ # Copyright 2009-2010 Miguel Fonseca
13
13
  #
14
14
  # == License
15
15
  # MIT (see LICENSE file)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: work_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Fonseca
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-24 00:00:00 +00:00
12
+ date: 2009-12-28 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,7 @@ files:
25
25
  - LICENSE
26
26
  - Rakefile
27
27
  - README.rdoc
28
+ - tasks/ftp.rake
28
29
  - tasks/gem.rake
29
30
  - tasks/rdoc.rake
30
31
  - tasks/test.rake