This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
trinidad_scheduler_extension.rb
126 lines (93 loc) · 3.7 KB
/
trinidad_scheduler_extension.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'trinidad'
# Jar files that are needed
require 'trinidad_scheduler_extension/jars'
# Trinidad Scheduler Extension files
require 'trinidad_scheduler_extension/version'
require 'trinidad_scheduler_extension/trinidad_scheduler'
require 'trinidad_scheduler_extension/app_job'
module Trinidad
module Extensions
class SchedulerWebAppExtension < WebAppExtension
def configure(context)
SchedulerLifecycle.setup_listener(context, options)
end
class SchedulerLifecycle < Trinidad::Lifecycle::Base
def self.setup_listener(context, options)
unless context.find_lifecycle_listeners.find { |l| l.is_a?(self) }
context.add_lifecycle_listener(self.new(context, options))
end
end
def initialize(context, options)
servlet_context = context.servlet_context
# TODO
$servlet_context = servlet_context unless $servlet_context.is_a?(javax.servlet.ServletContext)
TrinidadScheduler.store_scheduler_options(servlet_context, options)
@start_scheduler = nil
end
def configure_start(event)
# NOTE: context.application_listeners setup at this point
return unless start_scheduler?(event)
TrinidadScheduler.initialize_configuration!
end
def start(event)
return unless start_scheduler?(event)
servlet_context = event.lifecycle.servlet_context
scheduler = TrinidadScheduler.scheduler(servlet_context)
if scheduler && ! scheduler.started?
scheduler.start
scheduler.resume_all
end
TrinidadScheduler.set_servlet_started(servlet_context)
end
def stop(event)
servlet_context = event.lifecycle.servlet_context
scheduler = TrinidadScheduler.scheduler(servlet_context)
scheduler.shutdown if scheduler && scheduler.started?
end
private
def start_scheduler?(event)
return @start_scheduler unless @start_scheduler.nil?
context = event.lifecycle
# NOTE: in "rackup" mode empty no 'org.jruby.rack.RackServletContextListener'
start = context.findApplicationListeners.length > 0
start ||= Java::JavaLang::Boolean.getBoolean('trinidad.extensions.scheduler')
unless @start_scheduler = start
context.logger.info "Skipped configuration of scheduler extension (due rackup mode)"
end
@start_scheduler
end
end
end
# NOTE: disabled the server extension - since the extensions[:scheduler]
# hash gets merged into each web-app's configuration (on recent Trinidad).
# @private
class SchedulerServerExtension < ServerExtension
def configure(tomcat)
return unless ENV['Trindidad::Extensions::SchedulerServerExtension']
tomcat.host.add_container_listener(ContextListener.new(options))
end
class ContextListener
include Trinidad::Tomcat::ContainerListener
def initialize(options)
@options = options || {}
end
# @private
CONTAINER = Trinidad::Tomcat::Container
def containerEvent(event)
case event.type # ContainerEvent
when CONTAINER::ADD_CHILD_EVENT then
if ( context = event.data ).is_a?(Trinidad::Tomcat::Context)
SchedulerWebAppExtension::SchedulerLifecycle.setup_listener(context, @options)
end
end
end
end
end
end
end
module TrinidadScheduler
# @deprecated
WebAppListener = Trinidad::Extensions::SchedulerWebAppExtension::SchedulerLifecycle
# @deprecated
GlobalListener = Trinidad::Extensions::SchedulerServerExtension::ContextListener
end