Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding logging to main script

  • Loading branch information...
commit 37671d91016939576977765f0395d0bf4040ca13 1 parent f29576e
Roman authored March 29, 2012
28  bin/iron_worker_ng
@@ -19,6 +19,11 @@ if ENV['IRON_IO_TOKEN'].nil? || ENV['IRON_IO_PROJECT_ID'].nil?
19 19
   exit 1
20 20
 end
21 21
 
  22
+logger = Logger.new(ENV['IRON_WORKER_LOG_FILE'] || STDERR)
  23
+log_level = ENV['IRON_WORKER_LOG_LEVEL'] || 'INFO'
  24
+logger.level = eval "Logger::#{log_level}"
  25
+logger.datetime_format = "%F %T.%L "
  26
+
22 27
 client = IronWorkerNG::Client.new(:token => ENV['IRON_IO_TOKEN'], :project_id => ENV['IRON_IO_PROJECT_ID'])
23 28
 
24 29
 if command == 'codes.create'
@@ -58,23 +63,26 @@ if command == 'codes.create'
58 63
   begin
59 64
     opts.parse!
60 65
   rescue OptionParser::ParseError
61  
-    puts $!.to_s
  66
+    logger.fatal $!.report
62 67
     exit 1
63 68
   end
64 69
 
65 70
   if execute_features.size == 0
66  
-    puts opts
  71
+    logger.fatal "No features to execute specified\n#{opts}"
67 72
     exit 1
68 73
   end
69 74
 
70 75
   runtime = runtimes[0][:name] if runtime.nil?
71 76
 
  77
+  logger.info 'Creating code bundle'
72 78
   code = runtimes.find { |r| r[:name] == runtime }[:klass].new(name)
73 79
 
74 80
   execute_features.each do |f|
  81
+    logger.info "Executing feature #{f[:name]}"
75 82
     code.send(f[:name], *f[:args])
76 83
   end
77 84
 
  85
+  logger.info 'Uploading code bundle'
78 86
   client.codes.create(code)
79 87
 elsif command == 'tasks.create' || command == 'schedules.create'
80 88
   name = nil
@@ -140,12 +148,12 @@ elsif command == 'tasks.create' || command == 'schedules.create'
140 148
   begin
141 149
     opts.parse!
142 150
   rescue OptionParser::ParseError
143  
-    puts $!.to_s
  151
+    logger.fatal $!.to_s
144 152
     exit 1
145 153
   end
146 154
 
147 155
   if name.nil?
148  
-    puts opts
  156
+    logger.fatal "Required option --name is not specified\n#{opts}"
149 157
     exit 1
150 158
   end
151 159
 
@@ -165,11 +173,15 @@ elsif command == 'tasks.create' || command == 'schedules.create'
165 173
   id = nil
166 174
 
167 175
   if command == 'tasks.create'
  176
+    logger.info 'Creating task'
168 177
     id = client.tasks.create(name, params, options).id
169 178
   else
  179
+    logger.info 'Create schedule'
170 180
     id = client.schedules.create(name, params, options).id
171 181
   end
172 182
 
  183
+  logger.debug "id = #{id}"
  184
+
173 185
   print id if print_id
174 186
 elsif command == 'tasks.log'
175 187
   task_id = nil
@@ -190,21 +202,23 @@ elsif command == 'tasks.log'
190 202
   begin
191 203
     opts.parse!
192 204
   rescue OptionParser::ParseError
193  
-    puts $!.to_s
  205
+    logger.fatal $!.to_s
194 206
     exit 1
195 207
   end
196 208
 
197 209
   if task_id.nil?
198  
-    puts opts
  210
+    logger.fatal "Required option --task-id is not specified\n#{opts}"
199 211
     exit 1
200 212
   end
201 213
 
202 214
   log = ''
203 215
 
  216
+  logger.info 'Fetching task log'
204 217
   if live
205 218
     begin
206 219
       log = client.tasks.log(task_id)
207 220
     rescue IronWorkerNG::APIClientError
  221
+      logger.warn $!.report
208 222
     end
209 223
   else
210 224
     log = client.tasks.log(task_id)
@@ -220,6 +234,7 @@ elsif command == 'tasks.log'
220 234
           print next_log[log.length .. - 1]
221 235
           log = next_log
222 236
         rescue IronWorkerNG::APIClientError
  237
+          logger.warn $!.report
223 238
         end
224 239
       end
225 240
     end
@@ -228,6 +243,7 @@ elsif command == 'tasks.log'
228 243
       next_log = client.tasks.log(task_id)
229 244
       print next_log[log.length .. - 1]
230 245
     rescue IronWorkerNG::APIClientError
  246
+      logger.warn $!.report
231 247
     end
232 248
   end
233 249
 end
1  lib/iron_worker_ng.rb
... ...
@@ -1,6 +1,7 @@
1 1
 require_relative 'iron_worker_ng/version'
2 2
 require_relative 'iron_worker_ng/logger'
3 3
 require_relative 'iron_worker_ng/client'
  4
+require_relative 'iron_worker_ng/logging'
4 5
 require_relative 'iron_worker_ng/code/base'
5 6
 require_relative 'iron_worker_ng/code/ruby'
6 7
 require_relative 'iron_worker_ng/code/java'
7  lib/iron_worker_ng/logging.rb
... ...
@@ -0,0 +1,7 @@
  1
+require 'logger'
  2
+
  3
+class StandardError
  4
+  def report
  5
+    %{#{self.class}: #{message}\n#{backtrace.join("\n")}}
  6
+  end
  7
+end

0 notes on commit 37671d9

Please sign in to comment.
Something went wrong with that request. Please try again.