jcarley-simplews 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ require 'rmail'
2
+ require 'net/smtp'
3
+ require 'soap/wsdlDriver'
4
+ require 'simplews/jobs'
5
+
6
+ class SimpleWS::Jobs::Notifier
7
+
8
+ def driver
9
+ case
10
+ when String === @ws
11
+ if File.exists? @ws
12
+ return SOAP::WSDLDriverFactory.new(@ws).create_rpc_driver
13
+ end
14
+ when Array === @ws
15
+ return SimpleWS.get_driver(@ws[0], @ws[1])
16
+ when SOAP::WSDLDriverFactory === @ws
17
+ return @ws
18
+ end
19
+
20
+ raise "Do not know how to connect to driver"
21
+ end
22
+
23
+ def initialize(name, host, ws, options = {})
24
+ @host = host
25
+ @name = name
26
+ @ws = ws
27
+ @smtp_host = options[:smtp_host] || 'localhost'
28
+ @smtp_port = options[:smtp_port] || 25
29
+ @sleep_time = options[:sleep_time] || 2
30
+ @filename = options[:filename]
31
+
32
+ if @filename && File.exists?(@filename)
33
+ @jobs = Marshal.load(File.open(@filename))
34
+ else
35
+ @jobs = {}
36
+ end
37
+ end
38
+
39
+ def add_job(job_id, email)
40
+ @jobs[job_id] = email
41
+ File.open(@filename, 'w') do |f| f.write Marshal.dump(@jobs) end if @filename
42
+ end
43
+
44
+ def delete_job(job_id)
45
+ @jobs.delete(job_id)
46
+ File.open(@filename, 'w') do |f| f.write Marshal.dump(@jobs) end if @filename
47
+ end
48
+
49
+ def process
50
+ @jobs.each do |job_id, email|
51
+ if driver.done job_id
52
+ begin
53
+ if driver.error job_id
54
+ error(job_id, email, driver.messages(job_id).last)
55
+ else
56
+ success(job_id, email)
57
+ end
58
+ ensure
59
+ delete_job(job_id)
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def send_mail(to, subject, body)
66
+ puts "Sending mail to #{ to }: #{ subject }"
67
+ message = RMail::Message.new
68
+
69
+ from = "noreply@" + @host.sub(/:.*/,'')
70
+
71
+ message.header['To'] = to
72
+ message.header['From'] = from
73
+ message.header['Subject'] = subject
74
+
75
+ main = RMail::Message.new
76
+ main.body = body
77
+
78
+ message.add_part(main)
79
+
80
+ Net::SMTP.start(@smtp_host.chomp, @smtp_port.to_i) do |smtp|
81
+ smtp.send_message message.to_s, from, to
82
+ end
83
+ end
84
+
85
+
86
+ def error(job_id, email, msg)
87
+ body =<<-EOF
88
+ Dear #{ @name } user:
89
+
90
+ You job with id '#{ job_id }' has finished with error message:
91
+
92
+ #{ msg }
93
+
94
+ URL: http://#{@host.chomp}/#{ job_id }
95
+
96
+ Note: Do not reply to this message, it is automatically generated.
97
+ EOF
98
+ send_mail(email, "#{@name} [ERROR]: #{ job_id }", body)
99
+ end
100
+
101
+ def success(job_id, email)
102
+ body =<<-EOF
103
+ Dear #{ @name } user:
104
+
105
+ You job with id '#{ job_id }' has finished successfully:
106
+
107
+ URL: http://#{@host.chomp}/#{ job_id }
108
+
109
+ Note: Do not reply to this message, it is automatically generated.
110
+ EOF
111
+ send_mail(email, "#{@name} [SUCCESS]: #{ job_id }", body)
112
+ end
113
+
114
+ def pending?
115
+ ! @jobs.empty?
116
+ end
117
+
118
+ def start
119
+ puts "Starting Email notifier."
120
+ puts "Name: #{ @name }"
121
+ puts "Host: #{ @host }"
122
+ puts "SMTP: #{ @smtp_host }"
123
+ @thread = Thread.new do
124
+ while true
125
+ begin
126
+ process
127
+ sleep @sleep_time
128
+ rescue
129
+ puts $!.message
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ def stop
136
+ @thread.kill
137
+ end
138
+ end
@@ -0,0 +1,72 @@
1
+ # This file add support for Rake pipelines.
2
+
3
+ require 'simplews/jobs'
4
+ require 'rake'
5
+ require 'fileutils'
6
+
7
+ class SimpleWS::Jobs::Scheduler::Job
8
+
9
+ # Add step information to rule tasks, as the 'desc' method cannot be used to
10
+ # describe them for the time being.
11
+ def add_description(reg_exp, step, message)
12
+ @step_descriptions ||= {}
13
+ @step_descriptions[Regexp.new(reg_exp)] = "#{ step }: #{ message }"
14
+ end
15
+
16
+ # Instruct rake to load the rakefile, named Rakefile by default, and use it
17
+ # to produce the file specified first as product of the web service task. The
18
+ # 'execute' method of the Rake::Tasks class method execute is monkey-patched
19
+ # to log the steps. Since this is executed on a new process, there should be
20
+ # no side-effects from the patching.
21
+ def rake(rakefile = "Rakefile", target = nil)
22
+ Rake::Task.class_eval <<-'EOC'
23
+ alias_method :old_execute, :execute
24
+ def execute(*args)
25
+ action = name
26
+ message = $_step_descriptions.collect{|rexp, msg|
27
+ if name.match(rexp)
28
+ msg
29
+ else
30
+ nil
31
+ end
32
+ }.compact.first
33
+
34
+ message ||= comment
35
+
36
+ message ||= "Invoking #{name}"
37
+
38
+ if message.match(/^(\w+): (.*)/)
39
+ $_current_job.step($1, $2)
40
+ else
41
+ $_current_job.step(action, message)
42
+ end
43
+
44
+ old_execute(*args)
45
+ end
46
+ EOC
47
+
48
+ load rakefile
49
+ @@steps.each{|step|
50
+ step_dirname = File.join(workdir, step.to_s)
51
+ FileUtils.mkdir_p step_dirname unless File.exists? step_dirname
52
+ }
53
+
54
+ if defined? Rake::Pipeline
55
+ Rake::Pipeline::step_descriptions.each{|re, description|
56
+ if description.match(/(.*): (.*)/)
57
+ add_description(re, $1, $2)
58
+ end
59
+ }
60
+ end
61
+
62
+ files = result_filenames
63
+ target ||= files.first
64
+
65
+ $_current_job = self
66
+ $_step_descriptions = @step_descriptions || {}
67
+
68
+ Rake::Task[target].invoke
69
+ end
70
+
71
+ end
72
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcarley-simplews
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.11.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jefferson Carley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: soap4r-ruby1.9
16
+ requirement: &70229594631520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70229594631520
25
+ - !ruby/object:Gem::Dependency
26
+ name: rand
27
+ requirement: &70229594630660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70229594630660
36
+ - !ruby/object:Gem::Dependency
37
+ name: builder
38
+ requirement: &70229594629780 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70229594629780
47
+ description: Generates WSDL automatically. It manages jobs as asynchronous processes
48
+ email: jeff.carley@gmail.com
49
+ executables:
50
+ - start_jobs_ws
51
+ - start_ws
52
+ extensions: []
53
+ extra_rdoc_files:
54
+ - LICENSE
55
+ - README.rdoc
56
+ files:
57
+ - lib/rake_pipeline.rb
58
+ - lib/simplews.rb
59
+ - lib/simplews/jobs.rb
60
+ - lib/simplews/notifier.rb
61
+ - lib/simplews/rake.rb
62
+ - LICENSE
63
+ - README.rdoc
64
+ - bin/start_jobs_ws
65
+ - bin/start_ws
66
+ homepage: http://github.com/jcarley/jcarley-simplews
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.17
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Simplifies creating soap4r web services as stand-alone servers
90
+ test_files: []