0
# Application Management
1
2
Core application lifecycle management for the Flower web server, including initialization, startup, configuration, and shutdown operations.
3
4
## Capabilities
5
6
### Main Application Class
7
8
The `Flower` class is the central application component that manages the web server, Celery integration, and all monitoring functionality.
9
10
```python { .api }
11
class Flower(tornado.web.Application):
12
"""
13
Main Flower web application class extending Tornado's Application.
14
15
Manages the web server, Celery integration, event monitoring, and worker inspection.
16
"""
17
18
def __init__(self, options=None, capp=None, events=None, io_loop=None, **kwargs):
19
"""
20
Initialize Flower application.
21
22
Args:
23
options: Tornado options namespace with configuration
24
capp: Celery application instance
25
events: Events handler instance (optional)
26
io_loop: Tornado IOLoop instance (optional)
27
**kwargs: Additional tornado.web.Application arguments
28
"""
29
30
def start(self):
31
"""
32
Start the Flower application server.
33
34
Starts event monitoring, binds to configured port/address,
35
updates worker information, and begins the IO loop.
36
"""
37
38
def stop(self):
39
"""
40
Stop the Flower application server.
41
42
Stops event monitoring, shuts down executors, and stops the IO loop.
43
"""
44
45
def update_workers(self, workername=None):
46
"""
47
Update worker information from cluster.
48
49
Args:
50
workername (str, optional): Specific worker to update, or None for all
51
52
Returns:
53
Inspection results from worker updates
54
"""
55
56
@property
57
def transport(self):
58
"""
59
Get the broker transport type.
60
61
Returns:
62
str: Transport driver type (e.g., 'redis', 'amqp')
63
"""
64
65
@property
66
def workers(self):
67
"""
68
Get current worker information dictionary.
69
70
Returns:
71
dict: Worker information keyed by worker name
72
"""
73
```
74
75
### Application Properties
76
77
The Flower application provides key properties for accessing cluster state and configuration.
78
79
```python { .api }
80
# Core components accessible from Flower instance
81
flower_app.capp # Celery application instance
82
flower_app.events # Events monitoring instance
83
flower_app.inspector # Worker inspector instance
84
flower_app.executor # Thread pool executor
85
flower_app.io_loop # Tornado IOLoop instance
86
flower_app.options # Configuration options
87
flower_app.ssl_options # SSL configuration (if enabled)
88
flower_app.started # Boolean indicating if server is running
89
```
90
91
### Usage Examples
92
93
#### Basic Application Setup
94
95
```python
96
import celery
97
from flower.app import Flower
98
from flower.options import default_options
99
100
# Create Celery application
101
celery_app = celery.Celery('myapp')
102
celery_app.config_from_object({
103
'broker_url': 'redis://localhost:6379',
104
'result_backend': 'redis://localhost:6379'
105
})
106
107
# Create Flower application
108
flower_app = Flower(
109
capp=celery_app,
110
options=default_options
111
)
112
113
# Start monitoring
114
flower_app.start()
115
```
116
117
#### Custom Configuration
118
119
```python
120
from tornado.options import options
121
from flower.app import Flower
122
import celery
123
124
# Configure options
125
options.port = 5555
126
options.address = '0.0.0.0'
127
options.debug = True
128
options.persistent = True
129
options.max_workers = 1000
130
131
# Create application with custom settings
132
celery_app = celery.Celery('myapp', broker='redis://localhost:6379')
133
flower_app = Flower(
134
capp=celery_app,
135
options=options,
136
cookie_secret='your-secret-key'
137
)
138
139
flower_app.start()
140
```
141
142
#### SSL Configuration
143
144
```python
145
from flower.app import Flower
146
from tornado.options import options
147
148
# Configure SSL
149
options.certfile = '/path/to/cert.pem'
150
options.keyfile = '/path/to/key.pem'
151
options.ca_certs = '/path/to/ca.pem'
152
153
flower_app = Flower(capp=celery_app, options=options)
154
flower_app.start() # Will use SSL based on certfile/keyfile options
155
```
156
157
#### Graceful Shutdown
158
159
```python
160
import signal
161
import atexit
162
from flower.app import Flower
163
164
flower_app = Flower(capp=celery_app, options=options)
165
166
# Register cleanup handlers
167
atexit.register(flower_app.stop)
168
signal.signal(signal.SIGTERM, lambda signum, frame: flower_app.stop())
169
170
try:
171
flower_app.start()
172
except (KeyboardInterrupt, SystemExit):
173
flower_app.stop()
174
```
175
176
## Integration Points
177
178
### Celery Integration
179
180
Flower integrates deeply with Celery applications:
181
- Automatic task registration discovery
182
- Event monitoring from Celery's event system
183
- Worker inspection using Celery's remote control
184
- Broker connection sharing
185
186
### Tornado Integration
187
188
Built on Tornado web framework:
189
- Asynchronous request handling
190
- WebSocket support for real-time updates
191
- Static file serving for web interface
192
- Template rendering system
193
194
### Threading Model
195
196
Flower uses a hybrid threading model:
197
- Main thread runs Tornado's IOLoop
198
- Background thread for event capture
199
- Thread pool executor for blocking operations
200
- Asynchronous operations for I/O tasks