| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Collections; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace IceBox; |
| | | 7 | | |
| | | 8 | | internal class ServiceManagerI : ServiceManagerDisp_, IDisposable |
| | | 9 | | { |
| | 1 | 10 | | public ServiceManagerI(Ice.Communicator communicator, string[] args) |
| | | 11 | | { |
| | 1 | 12 | | _communicator = communicator; |
| | 1 | 13 | | _logger = _communicator.getLogger(); |
| | | 14 | | |
| | 1 | 15 | | Ice.Properties props = _communicator.getProperties(); |
| | | 16 | | |
| | 1 | 17 | | if (props.getIceProperty("Ice.Admin.Enabled").Length == 0) |
| | | 18 | | { |
| | 1 | 19 | | _adminEnabled = props.getIceProperty("Ice.Admin.Endpoints").Length > 0; |
| | | 20 | | } |
| | | 21 | | else |
| | | 22 | | { |
| | 0 | 23 | | _adminEnabled = props.getIcePropertyAsInt("Ice.Admin.Enabled") > 0; |
| | | 24 | | } |
| | | 25 | | |
| | 1 | 26 | | if (_adminEnabled) |
| | | 27 | | { |
| | 1 | 28 | | string[] facetFilter = props.getIcePropertyAsList("Ice.Admin.Facets"); |
| | 1 | 29 | | if (facetFilter.Length > 0) |
| | | 30 | | { |
| | 0 | 31 | | _adminFacetFilter = new HashSet<string>(facetFilter); |
| | | 32 | | } |
| | | 33 | | else |
| | | 34 | | { |
| | 1 | 35 | | _adminFacetFilter = new HashSet<string>(); |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | _argv = args; |
| | 1 | 40 | | _traceServiceObserver = _communicator.getProperties().getIcePropertyAsInt("IceBox.Trace.ServiceObserver"); |
| | 1 | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | public void Dispose() => _sharedCommunicator?.Dispose(); |
| | | 44 | | |
| | | 45 | | public override void startService(string name, Ice.Current current) |
| | | 46 | | { |
| | 1 | 47 | | var info = new ServiceInfo(); |
| | 1 | 48 | | lock (_mutex) |
| | | 49 | | { |
| | | 50 | | // |
| | | 51 | | // Search would be more efficient if services were contained in |
| | | 52 | | // a map, but order is required for shutdown. |
| | | 53 | | // |
| | | 54 | | int i; |
| | 1 | 55 | | for (i = 0; i < _services.Count; ++i) |
| | | 56 | | { |
| | 1 | 57 | | info = _services[i]; |
| | 1 | 58 | | if (info.name == name) |
| | | 59 | | { |
| | 1 | 60 | | if (_services[i].status != ServiceStatus.Stopped) |
| | | 61 | | { |
| | 0 | 62 | | throw new AlreadyStartedException(); |
| | | 63 | | } |
| | 1 | 64 | | info.status = ServiceStatus.Starting; |
| | 1 | 65 | | _services[i] = info; |
| | 1 | 66 | | break; |
| | | 67 | | } |
| | | 68 | | } |
| | 1 | 69 | | if (i == _services.Count) |
| | | 70 | | { |
| | 0 | 71 | | throw new NoSuchServiceException(); |
| | | 72 | | } |
| | 1 | 73 | | _pendingStatusChanges = true; |
| | 1 | 74 | | } |
| | | 75 | | |
| | 1 | 76 | | bool started = false; |
| | | 77 | | try |
| | | 78 | | { |
| | 1 | 79 | | info.service.start( |
| | 1 | 80 | | info.name, |
| | 1 | 81 | | info.communicator ?? _sharedCommunicator, |
| | 1 | 82 | | info.args); |
| | 1 | 83 | | started = true; |
| | 1 | 84 | | } |
| | 0 | 85 | | catch (Exception e) |
| | | 86 | | { |
| | 0 | 87 | | _logger.warning("ServiceManager: exception while starting service " + info.name + ":\n" + e.ToString()); |
| | 0 | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | lock (_mutex) |
| | | 91 | | { |
| | | 92 | | int i; |
| | 1 | 93 | | for (i = 0; i < _services.Count; ++i) |
| | | 94 | | { |
| | 1 | 95 | | info = _services[i]; |
| | 1 | 96 | | if (info.name.Equals(name, StringComparison.Ordinal)) |
| | | 97 | | { |
| | 1 | 98 | | if (started) |
| | | 99 | | { |
| | 1 | 100 | | info.status = ServiceStatus.Started; |
| | | 101 | | |
| | 1 | 102 | | var services = new List<string> |
| | 1 | 103 | | { |
| | 1 | 104 | | name |
| | 1 | 105 | | }; |
| | 1 | 106 | | servicesStarted(services, _observers.Keys); |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 0 | 110 | | info.status = ServiceStatus.Stopped; |
| | | 111 | | } |
| | 1 | 112 | | _services[i] = info; |
| | 1 | 113 | | break; |
| | | 114 | | } |
| | | 115 | | } |
| | 1 | 116 | | _pendingStatusChanges = false; |
| | 1 | 117 | | Monitor.PulseAll(_mutex); |
| | 1 | 118 | | } |
| | 1 | 119 | | } |
| | | 120 | | |
| | | 121 | | public override void stopService(string name, Ice.Current current) |
| | | 122 | | { |
| | 1 | 123 | | var info = new ServiceInfo(); |
| | 1 | 124 | | lock (_mutex) |
| | | 125 | | { |
| | | 126 | | // |
| | | 127 | | // Search would be more efficient if services were contained in |
| | | 128 | | // a map, but order is required for shutdown. |
| | | 129 | | // |
| | | 130 | | int i; |
| | 1 | 131 | | for (i = 0; i < _services.Count; ++i) |
| | | 132 | | { |
| | 1 | 133 | | info = _services[i]; |
| | 1 | 134 | | if (info.name.Equals(name, StringComparison.Ordinal)) |
| | | 135 | | { |
| | 1 | 136 | | if (info.status != ServiceStatus.Started) |
| | | 137 | | { |
| | 0 | 138 | | throw new AlreadyStoppedException(); |
| | | 139 | | } |
| | 1 | 140 | | info.status = ServiceStatus.Stopping; |
| | 1 | 141 | | _services[i] = info; |
| | 1 | 142 | | break; |
| | | 143 | | } |
| | | 144 | | } |
| | 1 | 145 | | if (i == _services.Count) |
| | | 146 | | { |
| | 0 | 147 | | throw new NoSuchServiceException(); |
| | | 148 | | } |
| | 1 | 149 | | _pendingStatusChanges = true; |
| | 1 | 150 | | } |
| | | 151 | | |
| | 1 | 152 | | bool stopped = false; |
| | | 153 | | try |
| | | 154 | | { |
| | 1 | 155 | | info.service.stop(); |
| | 1 | 156 | | stopped = true; |
| | 1 | 157 | | } |
| | 0 | 158 | | catch (Exception e) |
| | | 159 | | { |
| | 0 | 160 | | _logger.warning("ServiceManager: exception while stopping service " + info.name + "\n" + e.ToString()); |
| | 0 | 161 | | } |
| | | 162 | | |
| | 1 | 163 | | lock (_mutex) |
| | | 164 | | { |
| | | 165 | | int i; |
| | 1 | 166 | | for (i = 0; i < _services.Count; ++i) |
| | | 167 | | { |
| | 1 | 168 | | info = _services[i]; |
| | 1 | 169 | | if (info.name == name) |
| | | 170 | | { |
| | 1 | 171 | | if (stopped) |
| | | 172 | | { |
| | 1 | 173 | | info.status = ServiceStatus.Stopped; |
| | | 174 | | |
| | 1 | 175 | | var services = new List<string> |
| | 1 | 176 | | { |
| | 1 | 177 | | name |
| | 1 | 178 | | }; |
| | 1 | 179 | | servicesStopped(services, _observers.Keys); |
| | | 180 | | } |
| | | 181 | | else |
| | | 182 | | { |
| | 0 | 183 | | info.status = ServiceStatus.Started; |
| | | 184 | | } |
| | 1 | 185 | | _services[i] = info; |
| | 1 | 186 | | break; |
| | | 187 | | } |
| | | 188 | | } |
| | 1 | 189 | | _pendingStatusChanges = false; |
| | 1 | 190 | | Monitor.PulseAll(_mutex); |
| | 1 | 191 | | } |
| | 1 | 192 | | } |
| | | 193 | | |
| | | 194 | | public override bool isServiceRunning(string name, Ice.Current current) |
| | | 195 | | { |
| | 0 | 196 | | lock (_mutex) |
| | | 197 | | { |
| | 0 | 198 | | foreach (ServiceInfo info in _services) |
| | | 199 | | { |
| | 0 | 200 | | if (info.name == name) |
| | | 201 | | { |
| | 0 | 202 | | return info.status == ServiceStatus.Started; |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | } |
| | 0 | 206 | | throw new NoSuchServiceException(); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public override void addObserver(ServiceObserverPrx observer, Ice.Current current) |
| | | 210 | | { |
| | 0 | 211 | | var activeServices = new List<string>(); |
| | | 212 | | |
| | | 213 | | // |
| | | 214 | | // Null observers and duplicate registrations are ignored |
| | | 215 | | // |
| | 0 | 216 | | lock (_mutex) |
| | | 217 | | { |
| | 0 | 218 | | if (observer != null) |
| | | 219 | | { |
| | | 220 | | try |
| | | 221 | | { |
| | 0 | 222 | | _observers.Add(observer, true); |
| | 0 | 223 | | } |
| | 0 | 224 | | catch (ArgumentException) |
| | | 225 | | { |
| | 0 | 226 | | return; |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | if (_traceServiceObserver >= 1) |
| | | 230 | | { |
| | 0 | 231 | | _logger.trace( |
| | 0 | 232 | | "IceBox.ServiceObserver", |
| | 0 | 233 | | "Added service observer " + _communicator.proxyToString(observer)); |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | foreach (ServiceInfo info in _services) |
| | | 237 | | { |
| | 0 | 238 | | if (info.status == ServiceStatus.Started) |
| | | 239 | | { |
| | 0 | 240 | | activeServices.Add(info.name); |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | } |
| | 0 | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | if (activeServices.Count > 0) |
| | | 247 | | { |
| | 0 | 248 | | _ = servicesStartedAsync(observer, activeServices.ToArray()); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | async Task servicesStartedAsync(ServiceObserverPrx observer, string[] services) |
| | | 252 | | { |
| | | 253 | | try |
| | | 254 | | { |
| | 0 | 255 | | await observer.servicesStartedAsync(services).ConfigureAwait(false); |
| | 0 | 256 | | } |
| | 0 | 257 | | catch (System.Exception ex) |
| | | 258 | | { |
| | 0 | 259 | | removeObserver(observer, ex); |
| | 0 | 260 | | } |
| | 0 | 261 | | } |
| | 0 | 262 | | } |
| | | 263 | | |
| | 1 | 264 | | public override void shutdown(Ice.Current current) => _communicator.shutdown(); |
| | | 265 | | |
| | | 266 | | public int run() |
| | | 267 | | { |
| | | 268 | | try |
| | | 269 | | { |
| | 1 | 270 | | Ice.Properties properties = _communicator.getProperties(); |
| | | 271 | | |
| | | 272 | | // |
| | | 273 | | // Parse the property set with the prefix "IceBox.Service.". These |
| | | 274 | | // properties should have the following format: |
| | | 275 | | // |
| | | 276 | | // IceBox.Service.Foo=<assembly>:Package.Foo [args] |
| | | 277 | | // |
| | | 278 | | // We parse the service properties specified in IceBox.LoadOrder |
| | | 279 | | // first, then the ones from remaining services. |
| | | 280 | | // |
| | 1 | 281 | | string prefix = "IceBox.Service."; |
| | 1 | 282 | | Dictionary<string, string> services = properties.getPropertiesForPrefix(prefix); |
| | | 283 | | |
| | 1 | 284 | | if (services.Count == 0) |
| | | 285 | | { |
| | 0 | 286 | | throw new FailureException("ServiceManager: configuration must include at least one IceBox service."); |
| | | 287 | | } |
| | | 288 | | |
| | 1 | 289 | | string[] loadOrder = properties.getIcePropertyAsList("IceBox.LoadOrder"); |
| | 1 | 290 | | var servicesInfo = new List<StartServiceInfo>(); |
| | 1 | 291 | | for (int i = 0; i < loadOrder.Length; ++i) |
| | | 292 | | { |
| | 1 | 293 | | if (loadOrder[i].Length > 0) |
| | | 294 | | { |
| | 1 | 295 | | string key = prefix + loadOrder[i]; |
| | 1 | 296 | | if (!services.TryGetValue(key, out string value)) |
| | | 297 | | { |
| | 0 | 298 | | throw new FailureException($"ServiceManager: no service definition for '{loadOrder[i]}'."); |
| | | 299 | | } |
| | 1 | 300 | | servicesInfo.Add(new StartServiceInfo(loadOrder[i], value, _argv)); |
| | 1 | 301 | | services.Remove(key); |
| | | 302 | | } |
| | | 303 | | } |
| | 1 | 304 | | foreach (KeyValuePair<string, string> entry in services) |
| | | 305 | | { |
| | 1 | 306 | | string name = entry.Key[prefix.Length..]; |
| | 1 | 307 | | string value = entry.Value; |
| | 1 | 308 | | servicesInfo.Add(new StartServiceInfo(name, value, _argv)); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | // |
| | | 312 | | // Check if some services are using the shared communicator in which |
| | | 313 | | // case we create the shared communicator now with a property set that |
| | | 314 | | // is the union of all the service properties (from services that use |
| | | 315 | | // the shared communicator). |
| | | 316 | | // |
| | 1 | 317 | | if (properties.getPropertiesForPrefix("IceBox.UseSharedCommunicator.").Count > 0) |
| | | 318 | | { |
| | 1 | 319 | | var initData = new Ice.InitializationData(); |
| | 1 | 320 | | initData.properties = createServiceProperties("SharedCommunicator"); |
| | 1 | 321 | | foreach (StartServiceInfo service in servicesInfo) |
| | | 322 | | { |
| | 1 | 323 | | if (properties.getIcePropertyAsInt("IceBox.UseSharedCommunicator." + service.name) <= 0) |
| | | 324 | | { |
| | | 325 | | continue; |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | // |
| | | 329 | | // Load the service properties using the shared communicator properties as |
| | | 330 | | // the default properties. |
| | | 331 | | // |
| | 1 | 332 | | var svcProperties = new Ice.Properties(ref service.args, initData.properties); |
| | | 333 | | |
| | | 334 | | // |
| | | 335 | | // Remove properties from the shared property set that a service explicitly clears. |
| | | 336 | | // |
| | 1 | 337 | | Dictionary<string, string> allProps = initData.properties.getPropertiesForPrefix(""); |
| | 1 | 338 | | foreach (string key in allProps.Keys) |
| | | 339 | | { |
| | 1 | 340 | | if (svcProperties.getProperty(key).Length == 0) |
| | | 341 | | { |
| | 1 | 342 | | initData.properties.setProperty(key, ""); |
| | | 343 | | } |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | // |
| | | 347 | | // Add the service properties to the shared communicator properties. |
| | | 348 | | // |
| | 1 | 349 | | foreach (KeyValuePair<string, string> entry in svcProperties.getPropertiesForPrefix("")) |
| | | 350 | | { |
| | 1 | 351 | | initData.properties.setProperty(entry.Key, entry.Value); |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | // |
| | | 355 | | // Parse <service>.* command line options (the Ice command line options |
| | | 356 | | // were parsed by the Properties constructor called above). |
| | | 357 | | // |
| | 1 | 358 | | service.args = initData.properties.parseCommandLineOptions(service.name, service.args); |
| | | 359 | | } |
| | | 360 | | |
| | 1 | 361 | | string facetNamePrefix = "IceBox.SharedCommunicator."; |
| | 1 | 362 | | bool addFacets = configureAdmin(initData.properties, facetNamePrefix); |
| | | 363 | | |
| | 1 | 364 | | _sharedCommunicator = new Ice.Communicator(initData); |
| | | 365 | | |
| | 1 | 366 | | if (addFacets) |
| | | 367 | | { |
| | | 368 | | // Add all facets created on shared communicator to the IceBox communicator |
| | | 369 | | // but renamed <prefix>.<facet-name>, except for the Process facet which is |
| | | 370 | | // never added. |
| | 1 | 371 | | foreach (KeyValuePair<string, Ice.Object> p in _sharedCommunicator.findAllAdminFacets()) |
| | | 372 | | { |
| | 1 | 373 | | if (p.Key != "Process") |
| | | 374 | | { |
| | 1 | 375 | | _communicator.addAdminFacet(p.Value, facetNamePrefix + p.Key); |
| | | 376 | | } |
| | | 377 | | } |
| | | 378 | | } |
| | | 379 | | } |
| | | 380 | | |
| | 1 | 381 | | foreach (StartServiceInfo s in servicesInfo) |
| | | 382 | | { |
| | 1 | 383 | | startService(s.name, s.entryPoint, s.args); |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | // |
| | | 387 | | // Start Admin (if enabled). |
| | | 388 | | // |
| | 1 | 389 | | _communicator.addAdminFacet(this, "IceBox.ServiceManager"); |
| | 1 | 390 | | _communicator.getAdmin(); |
| | | 391 | | |
| | | 392 | | // |
| | | 393 | | // We may want to notify external scripts that the services |
| | | 394 | | // have started and that IceBox is "ready". |
| | | 395 | | // This is done by defining the property IceBox.PrintServicesReady=bundleName |
| | | 396 | | // |
| | | 397 | | // bundleName is whatever you choose to call this set of |
| | | 398 | | // services. It will be echoed back as "bundleName ready". |
| | | 399 | | // |
| | | 400 | | // This must be done after start() has been invoked on the |
| | | 401 | | // services. |
| | | 402 | | // |
| | 1 | 403 | | string bundleName = properties.getIceProperty("IceBox.PrintServicesReady"); |
| | 1 | 404 | | if (bundleName.Length > 0) |
| | | 405 | | { |
| | 1 | 406 | | Console.Out.WriteLine(bundleName + " ready"); |
| | | 407 | | } |
| | | 408 | | |
| | 1 | 409 | | _communicator.waitForShutdown(); |
| | 1 | 410 | | } |
| | 0 | 411 | | catch (FailureException ex) |
| | | 412 | | { |
| | 0 | 413 | | _logger.error(ex.ToString()); |
| | 0 | 414 | | return 1; |
| | | 415 | | } |
| | 0 | 416 | | catch (Ice.CommunicatorDestroyedException) |
| | | 417 | | { |
| | | 418 | | // Expected if the communicator is destroyed |
| | 0 | 419 | | } |
| | 0 | 420 | | catch (Ice.ObjectAdapterDeactivatedException) |
| | | 421 | | { |
| | | 422 | | // Expected if the communicator is shutdown |
| | 0 | 423 | | } |
| | 0 | 424 | | catch (Ice.ObjectAdapterDestroyedException) |
| | | 425 | | { |
| | | 426 | | // Expected if the communicator is destroyed |
| | 0 | 427 | | } |
| | 0 | 428 | | catch (Exception ex) |
| | | 429 | | { |
| | 0 | 430 | | _logger.error("ServiceManager: caught exception:\n" + ex.ToString()); |
| | 0 | 431 | | return 1; |
| | | 432 | | } |
| | | 433 | | finally |
| | | 434 | | { |
| | | 435 | | // |
| | | 436 | | // Invoke stop() on the services. |
| | | 437 | | // |
| | 1 | 438 | | stopAll(); |
| | 1 | 439 | | } |
| | | 440 | | |
| | 1 | 441 | | return 0; |
| | 0 | 442 | | } |
| | | 443 | | |
| | | 444 | | private void startService(string service, string entryPoint, string[] args) |
| | | 445 | | { |
| | 1 | 446 | | lock (_mutex) |
| | | 447 | | { |
| | | 448 | | // |
| | | 449 | | // Extract the assembly name and the class name. |
| | | 450 | | // |
| | 1 | 451 | | string err = "ServiceManager: unable to load service '" + entryPoint + "': "; |
| | 1 | 452 | | int sepPos = entryPoint.IndexOf(':', StringComparison.Ordinal); |
| | 1 | 453 | | if (sepPos != -1) |
| | | 454 | | { |
| | | 455 | | const string driveLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| | 1 | 456 | | if (entryPoint.Length > 3 && |
| | 1 | 457 | | sepPos == 1 && |
| | 1 | 458 | | driveLetters.Contains(entryPoint[0], StringComparison.Ordinal) && |
| | 1 | 459 | | (entryPoint[2] == '\\' || entryPoint[2] == '/')) |
| | | 460 | | { |
| | 0 | 461 | | sepPos = entryPoint.IndexOf(':', 3); |
| | | 462 | | } |
| | | 463 | | } |
| | 1 | 464 | | if (sepPos == -1) |
| | | 465 | | { |
| | 0 | 466 | | throw new FailureException($"{err}invalid entry point format."); |
| | | 467 | | } |
| | | 468 | | |
| | 1 | 469 | | System.Reflection.Assembly serviceAssembly = null; |
| | 1 | 470 | | string assemblyName = entryPoint[..sepPos]; |
| | 1 | 471 | | string className = entryPoint[(sepPos + 1)..]; |
| | | 472 | | |
| | | 473 | | try |
| | | 474 | | { |
| | | 475 | | // |
| | | 476 | | // First try to load the assembly using Assembly.Load, which will succeed |
| | | 477 | | // if a fully-qualified name is provided or if a partial name has been qualified |
| | | 478 | | // in configuration. If that fails, try Assembly.LoadFrom(), which will succeed |
| | | 479 | | // if a file name is configured or a partial name is configured and DEVPATH is used. |
| | | 480 | | // |
| | | 481 | | try |
| | | 482 | | { |
| | 1 | 483 | | serviceAssembly = System.Reflection.Assembly.Load(assemblyName); |
| | 0 | 484 | | } |
| | 1 | 485 | | catch (Exception ex) |
| | | 486 | | { |
| | | 487 | | try |
| | | 488 | | { |
| | 1 | 489 | | serviceAssembly = System.Reflection.Assembly.LoadFrom(assemblyName); |
| | 1 | 490 | | } |
| | 0 | 491 | | catch (Exception) |
| | | 492 | | { |
| | 0 | 493 | | throw ex; |
| | | 494 | | } |
| | 1 | 495 | | } |
| | 1 | 496 | | } |
| | 0 | 497 | | catch (Exception ex) |
| | | 498 | | { |
| | 0 | 499 | | throw new FailureException($"{err}unable to load assembly: {assemblyName}", ex); |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | // |
| | | 503 | | // Instantiate the class. |
| | | 504 | | // |
| | 1 | 505 | | Type c = null; |
| | | 506 | | try |
| | | 507 | | { |
| | 1 | 508 | | c = serviceAssembly.GetType(className, true); |
| | 1 | 509 | | } |
| | 0 | 510 | | catch (Exception ex) |
| | | 511 | | { |
| | 0 | 512 | | throw new FailureException($"{err}GetType failed for '{className}'.", ex); |
| | | 513 | | } |
| | | 514 | | |
| | 1 | 515 | | var info = new ServiceInfo(); |
| | 1 | 516 | | info.name = service; |
| | 1 | 517 | | info.status = ServiceStatus.Stopped; |
| | 1 | 518 | | info.args = args; |
| | | 519 | | |
| | | 520 | | // |
| | | 521 | | // If IceBox.UseSharedCommunicator.<name> is defined, create a |
| | | 522 | | // communicator for the service. The communicator inherits |
| | | 523 | | // from the shared communicator properties. If it's not |
| | | 524 | | // defined, add the service properties to the shared |
| | | 525 | | // communicator property set. |
| | | 526 | | // |
| | | 527 | | Ice.Communicator communicator; |
| | 1 | 528 | | if (_communicator.getProperties().getIcePropertyAsInt("IceBox.UseSharedCommunicator." + service) > 0) |
| | | 529 | | { |
| | | 530 | | Debug.Assert(_sharedCommunicator != null); |
| | 1 | 531 | | communicator = _sharedCommunicator; |
| | | 532 | | } |
| | | 533 | | else |
| | | 534 | | { |
| | | 535 | | // |
| | | 536 | | // Create the service properties. We use the communicator properties as the default |
| | | 537 | | // properties if IceBox.InheritProperties is set. |
| | | 538 | | // |
| | 1 | 539 | | var initData = new Ice.InitializationData(); |
| | 1 | 540 | | initData.properties = createServiceProperties(service); |
| | 1 | 541 | | if (info.args.Length > 0) |
| | | 542 | | { |
| | | 543 | | // |
| | | 544 | | // Create the service properties with the given service arguments. This should |
| | | 545 | | // read the service config file if it's specified with --Ice.Config. |
| | | 546 | | // |
| | 1 | 547 | | initData.properties = new Ice.Properties(ref info.args, initData.properties); |
| | | 548 | | |
| | | 549 | | // |
| | | 550 | | // Next, parse the service "<service>.*" command line options (the Ice command |
| | | 551 | | // line options were parsed by the Properties constructor above) |
| | | 552 | | // |
| | 1 | 553 | | info.args = initData.properties.parseCommandLineOptions(service, info.args); |
| | | 554 | | } |
| | | 555 | | |
| | | 556 | | // |
| | | 557 | | // If Admin is enabled on the IceBox communicator, for each service that does not set |
| | | 558 | | // Ice.Admin.Enabled, we set Ice.Admin.Enabled=1 to have this service create facets; then |
| | | 559 | | // we add these facets to the IceBox Admin object as IceBox.Service.<service>.<facet>. |
| | | 560 | | // |
| | 1 | 561 | | string serviceFacetNamePrefix = "IceBox.Service." + service + "."; |
| | 1 | 562 | | bool addFacets = configureAdmin(initData.properties, serviceFacetNamePrefix); |
| | | 563 | | |
| | 1 | 564 | | info.communicator = new Ice.Communicator(initData); |
| | 1 | 565 | | communicator = info.communicator; |
| | | 566 | | |
| | 1 | 567 | | if (addFacets) |
| | | 568 | | { |
| | | 569 | | // Add all facets created on the service communicator to the IceBox communicator |
| | | 570 | | // but renamed IceBox.Service.<service>.<facet-name>, except for the Process facet |
| | | 571 | | // which is never added |
| | 1 | 572 | | foreach (KeyValuePair<string, Ice.Object> p in communicator.findAllAdminFacets()) |
| | | 573 | | { |
| | 1 | 574 | | if (p.Key != "Process") |
| | | 575 | | { |
| | 1 | 576 | | _communicator.addAdminFacet(p.Value, serviceFacetNamePrefix + p.Key); |
| | | 577 | | } |
| | | 578 | | } |
| | | 579 | | } |
| | | 580 | | } |
| | | 581 | | |
| | | 582 | | try |
| | | 583 | | { |
| | | 584 | | // |
| | | 585 | | // Instantiate the service. |
| | | 586 | | // |
| | | 587 | | try |
| | | 588 | | { |
| | | 589 | | // |
| | | 590 | | // If the service class provides a constructor that accepts an Ice.Communicator argument, |
| | | 591 | | // use that in preference to the default constructor. |
| | | 592 | | // |
| | 1 | 593 | | var parameterTypes = new Type[1]; |
| | 1 | 594 | | parameterTypes[0] = typeof(Ice.Communicator); |
| | 1 | 595 | | System.Reflection.ConstructorInfo ci = c.GetConstructor(parameterTypes); |
| | 1 | 596 | | if (ci != null) |
| | | 597 | | { |
| | | 598 | | try |
| | | 599 | | { |
| | 1 | 600 | | object[] parameters = new object[1]; |
| | 1 | 601 | | parameters[0] = _communicator; |
| | 1 | 602 | | info.service = (Service)ci.Invoke(parameters); |
| | 1 | 603 | | } |
| | 0 | 604 | | catch (MethodAccessException ex) |
| | | 605 | | { |
| | 0 | 606 | | throw new FailureException( |
| | 0 | 607 | | $"{err}unable to access service constructor {className}(Ice.Communicator).", |
| | 0 | 608 | | ex); |
| | | 609 | | } |
| | | 610 | | } |
| | | 611 | | else |
| | | 612 | | { |
| | | 613 | | // |
| | | 614 | | // Fall back to the default constructor. |
| | | 615 | | // |
| | | 616 | | try |
| | | 617 | | { |
| | 1 | 618 | | info.service = (Service)Ice.Internal.AssemblyUtil.createInstance(c); |
| | 1 | 619 | | if (info.service == null) |
| | | 620 | | { |
| | 0 | 621 | | throw new FailureException($"{err}no default constructor for '{className}'."); |
| | | 622 | | } |
| | 1 | 623 | | } |
| | 0 | 624 | | catch (UnauthorizedAccessException ex) |
| | | 625 | | { |
| | 0 | 626 | | throw new FailureException( |
| | 0 | 627 | | $"{err}unauthorized access to default service constructor for '{className}'.", ex); |
| | | 628 | | } |
| | | 629 | | } |
| | 1 | 630 | | } |
| | 0 | 631 | | catch (FailureException) |
| | | 632 | | { |
| | 0 | 633 | | throw; |
| | | 634 | | } |
| | 0 | 635 | | catch (InvalidCastException ex) |
| | | 636 | | { |
| | 0 | 637 | | throw new FailureException($"{err}service does not implement IceBox.Service.", ex); |
| | | 638 | | } |
| | 0 | 639 | | catch (System.Reflection.TargetInvocationException ex) |
| | | 640 | | { |
| | 0 | 641 | | if (ex.InnerException is FailureException) |
| | | 642 | | { |
| | 0 | 643 | | throw ex.InnerException; |
| | | 644 | | } |
| | | 645 | | else |
| | | 646 | | { |
| | 0 | 647 | | throw new FailureException( |
| | 0 | 648 | | $"{err}exception in service constructor for '{className}'.", ex.InnerException); |
| | | 649 | | } |
| | | 650 | | } |
| | 0 | 651 | | catch (Exception ex) |
| | | 652 | | { |
| | 0 | 653 | | throw new FailureException($"{err}exception in service constructor for '{className}'.", ex); |
| | | 654 | | } |
| | | 655 | | |
| | | 656 | | try |
| | | 657 | | { |
| | 1 | 658 | | info.service.start(service, communicator, info.args); |
| | 1 | 659 | | } |
| | 0 | 660 | | catch (FailureException) |
| | | 661 | | { |
| | 0 | 662 | | throw; |
| | | 663 | | } |
| | 0 | 664 | | catch (Exception ex) |
| | | 665 | | { |
| | 0 | 666 | | throw new FailureException($"{err}exception while starting service '{service}'.", ex); |
| | | 667 | | } |
| | | 668 | | |
| | 1 | 669 | | info.status = ServiceStatus.Started; |
| | 1 | 670 | | _services.Add(info); |
| | 1 | 671 | | } |
| | 0 | 672 | | catch (Exception) |
| | | 673 | | { |
| | 0 | 674 | | if (info.communicator != null) |
| | | 675 | | { |
| | 0 | 676 | | destroyServiceCommunicator(service, info.communicator); |
| | | 677 | | } |
| | 0 | 678 | | throw; |
| | | 679 | | } |
| | | 680 | | } |
| | 1 | 681 | | } |
| | | 682 | | |
| | | 683 | | private void stopAll() |
| | | 684 | | { |
| | 1 | 685 | | lock (_mutex) |
| | | 686 | | { |
| | | 687 | | // |
| | | 688 | | // First wait for any active startService/stopService calls to complete. |
| | | 689 | | // |
| | 1 | 690 | | while (_pendingStatusChanges) |
| | | 691 | | { |
| | 0 | 692 | | Monitor.Wait(_mutex); |
| | | 693 | | } |
| | | 694 | | |
| | | 695 | | // |
| | | 696 | | // For each service, we call stop on the service. |
| | | 697 | | // Services are stopped in the reverse order of the order they were started. |
| | | 698 | | // |
| | 1 | 699 | | _services.Reverse(); |
| | 1 | 700 | | var stoppedServices = new List<string>(); |
| | 1 | 701 | | foreach (ServiceInfo info in _services) |
| | | 702 | | { |
| | 1 | 703 | | if (info.status == ServiceStatus.Started) |
| | | 704 | | { |
| | | 705 | | try |
| | | 706 | | { |
| | 1 | 707 | | info.service.stop(); |
| | 1 | 708 | | stoppedServices.Add(info.name); |
| | 1 | 709 | | } |
| | 0 | 710 | | catch (Exception e) |
| | | 711 | | { |
| | 0 | 712 | | _logger.warning( |
| | 0 | 713 | | "IceBox.ServiceManager: exception while stopping service " + |
| | 0 | 714 | | info.name + |
| | 0 | 715 | | ":\n" + |
| | 0 | 716 | | e.ToString()); |
| | 0 | 717 | | } |
| | | 718 | | } |
| | | 719 | | |
| | 1 | 720 | | if (info.communicator != null) |
| | | 721 | | { |
| | 1 | 722 | | destroyServiceCommunicator(info.name, info.communicator); |
| | | 723 | | } |
| | | 724 | | } |
| | | 725 | | |
| | 1 | 726 | | if (_sharedCommunicator != null) |
| | | 727 | | { |
| | 1 | 728 | | removeAdminFacets("IceBox.SharedCommunicator."); |
| | | 729 | | |
| | | 730 | | try |
| | | 731 | | { |
| | 1 | 732 | | _sharedCommunicator.destroy(); |
| | 1 | 733 | | } |
| | 0 | 734 | | catch (Exception e) |
| | | 735 | | { |
| | 0 | 736 | | _logger.warning( |
| | 0 | 737 | | "ServiceManager: exception while destroying shared communicator:\n" + e.ToString()); |
| | 0 | 738 | | } |
| | 1 | 739 | | _sharedCommunicator = null; |
| | | 740 | | } |
| | | 741 | | |
| | 1 | 742 | | _services.Clear(); |
| | 1 | 743 | | servicesStopped(stoppedServices, _observers.Keys); |
| | 1 | 744 | | } |
| | 1 | 745 | | } |
| | | 746 | | |
| | | 747 | | private void servicesStarted(List<string> services, Dictionary<ServiceObserverPrx, bool>.KeyCollection observers) |
| | | 748 | | { |
| | | 749 | | // |
| | | 750 | | // Must be called with 'this' unlocked |
| | | 751 | | // |
| | | 752 | | |
| | 1 | 753 | | if (services.Count > 0) |
| | | 754 | | { |
| | 1 | 755 | | string[] servicesArray = services.ToArray(); |
| | | 756 | | |
| | 1 | 757 | | foreach (ServiceObserverPrx observer in observers) |
| | | 758 | | { |
| | 0 | 759 | | _ = servicesStartedAsync(observer, servicesArray); |
| | | 760 | | } |
| | | 761 | | } |
| | | 762 | | |
| | | 763 | | async Task servicesStartedAsync(ServiceObserverPrx observer, string[] services) |
| | | 764 | | { |
| | | 765 | | try |
| | | 766 | | { |
| | 0 | 767 | | await observer.servicesStartedAsync(services).ConfigureAwait(false); |
| | 0 | 768 | | } |
| | 0 | 769 | | catch (System.Exception ex) |
| | | 770 | | { |
| | 0 | 771 | | removeObserver(observer, ex); |
| | 0 | 772 | | } |
| | 0 | 773 | | } |
| | 1 | 774 | | } |
| | | 775 | | |
| | | 776 | | private void servicesStopped(List<string> services, Dictionary<ServiceObserverPrx, bool>.KeyCollection observers) |
| | | 777 | | { |
| | | 778 | | // |
| | | 779 | | // Must be called with 'this' unlocked |
| | | 780 | | // |
| | | 781 | | |
| | 1 | 782 | | if (services.Count > 0) |
| | | 783 | | { |
| | 1 | 784 | | string[] servicesArray = services.ToArray(); |
| | | 785 | | |
| | 1 | 786 | | foreach (ServiceObserverPrx observer in observers) |
| | | 787 | | { |
| | 0 | 788 | | _ = servicesStoppedAsync(observer, servicesArray); |
| | | 789 | | } |
| | | 790 | | } |
| | | 791 | | |
| | | 792 | | async Task servicesStoppedAsync(ServiceObserverPrx observer, string[] services) |
| | | 793 | | { |
| | | 794 | | try |
| | | 795 | | { |
| | 0 | 796 | | await observer.servicesStoppedAsync(services).ConfigureAwait(false); |
| | 0 | 797 | | } |
| | 0 | 798 | | catch (System.Exception ex) |
| | | 799 | | { |
| | 0 | 800 | | removeObserver(observer, ex); |
| | 0 | 801 | | } |
| | 0 | 802 | | } |
| | 1 | 803 | | } |
| | | 804 | | |
| | | 805 | | private void |
| | | 806 | | removeObserver(ServiceObserverPrx observer, System.Exception ex) |
| | | 807 | | { |
| | 0 | 808 | | lock (_mutex) |
| | | 809 | | { |
| | 0 | 810 | | if (_observers.Remove(observer)) |
| | | 811 | | { |
| | 0 | 812 | | observerRemoved(observer, ex); |
| | | 813 | | } |
| | 0 | 814 | | } |
| | 0 | 815 | | } |
| | | 816 | | |
| | | 817 | | private void observerRemoved(ServiceObserverPrx observer, Exception ex) |
| | | 818 | | { |
| | 0 | 819 | | if (_traceServiceObserver >= 1) |
| | | 820 | | { |
| | | 821 | | // |
| | | 822 | | // CommunicatorDestroyedException may occur during shutdown. The observer notification has |
| | | 823 | | // been sent, but the communicator was destroyed before the reply was received. We do not |
| | | 824 | | // log a message for this exception. |
| | | 825 | | // |
| | 0 | 826 | | if (!(ex is Ice.CommunicatorDestroyedException)) |
| | | 827 | | { |
| | 0 | 828 | | _logger.trace( |
| | 0 | 829 | | "IceBox.ServiceObserver", |
| | 0 | 830 | | "Removed service observer " + _communicator.proxyToString(observer) |
| | 0 | 831 | | + "\nafter catching " + ex.ToString()); |
| | | 832 | | } |
| | | 833 | | } |
| | 0 | 834 | | } |
| | | 835 | | |
| | | 836 | | private enum ServiceStatus |
| | | 837 | | { |
| | | 838 | | Stopping, |
| | | 839 | | Stopped, |
| | | 840 | | Starting, |
| | | 841 | | Started |
| | | 842 | | } |
| | | 843 | | |
| | | 844 | | private struct ServiceInfo |
| | | 845 | | { |
| | | 846 | | public string name; |
| | | 847 | | public Service service; |
| | | 848 | | public Ice.Communicator communicator; |
| | | 849 | | public ServiceStatus status; |
| | | 850 | | public string[] args; |
| | | 851 | | } |
| | | 852 | | |
| | | 853 | | private class StartServiceInfo |
| | | 854 | | { |
| | 1 | 855 | | public StartServiceInfo(string service, string value, string[] serverArgs) |
| | | 856 | | { |
| | | 857 | | // |
| | | 858 | | // Separate the entry point from the arguments. |
| | | 859 | | // |
| | 1 | 860 | | name = service; |
| | | 861 | | |
| | | 862 | | try |
| | | 863 | | { |
| | 1 | 864 | | args = Ice.UtilInternal.Options.split(value); |
| | 1 | 865 | | } |
| | 0 | 866 | | catch (Ice.ParseException ex) |
| | | 867 | | { |
| | 0 | 868 | | throw new FailureException($"ServiceManager: invalid arguments for service '{name}'.", ex); |
| | | 869 | | } |
| | | 870 | | |
| | | 871 | | Debug.Assert(args.Length > 0); |
| | | 872 | | |
| | 1 | 873 | | entryPoint = args[0]; |
| | | 874 | | |
| | | 875 | | // |
| | | 876 | | // Shift the arguments. |
| | | 877 | | // |
| | 1 | 878 | | string[] tmp = new string[args.Length - 1]; |
| | 1 | 879 | | Array.Copy(args, 1, tmp, 0, args.Length - 1); |
| | 1 | 880 | | args = tmp; |
| | | 881 | | |
| | 1 | 882 | | if (serverArgs.Length > 0) |
| | | 883 | | { |
| | 0 | 884 | | var l = new ArrayList(); |
| | 0 | 885 | | for (int j = 0; j < args.Length; j++) |
| | | 886 | | { |
| | 0 | 887 | | l.Add(args[j]); |
| | | 888 | | } |
| | 0 | 889 | | for (int j = 0; j < serverArgs.Length; j++) |
| | | 890 | | { |
| | 0 | 891 | | if (serverArgs[j].StartsWith("--" + service + ".", StringComparison.Ordinal)) |
| | | 892 | | { |
| | 0 | 893 | | l.Add(serverArgs[j]); |
| | | 894 | | } |
| | | 895 | | } |
| | 0 | 896 | | args = (string[])l.ToArray(typeof(string)); |
| | | 897 | | } |
| | 1 | 898 | | } |
| | | 899 | | |
| | | 900 | | public string name; |
| | | 901 | | public string entryPoint; |
| | | 902 | | public string[] args; |
| | | 903 | | } |
| | | 904 | | |
| | | 905 | | private Ice.Properties createServiceProperties(string service) |
| | | 906 | | { |
| | 1 | 907 | | var properties = new Ice.Properties(); |
| | 1 | 908 | | Ice.Properties communicatorProperties = _communicator.getProperties(); |
| | 1 | 909 | | if (communicatorProperties.getIcePropertyAsInt("IceBox.InheritProperties") > 0) |
| | | 910 | | { |
| | | 911 | | // Inherit all except IceBox. and Ice.Admin. properties |
| | 1 | 912 | | foreach (KeyValuePair<string, string> property in communicatorProperties.getPropertiesForPrefix("")) |
| | | 913 | | { |
| | 1 | 914 | | if (!property.Key.StartsWith("IceBox.", StringComparison.Ordinal) && |
| | 1 | 915 | | !property.Key.StartsWith("Ice.Admin.", StringComparison.Ordinal)) |
| | | 916 | | { |
| | 1 | 917 | | properties.setProperty(property.Key, property.Value); |
| | | 918 | | } |
| | | 919 | | } |
| | | 920 | | } |
| | | 921 | | |
| | 1 | 922 | | string programName = communicatorProperties.getIceProperty("Ice.ProgramName"); |
| | 1 | 923 | | if (programName.Length == 0) |
| | | 924 | | { |
| | 0 | 925 | | properties.setProperty("Ice.ProgramName", service); |
| | | 926 | | } |
| | | 927 | | else |
| | | 928 | | { |
| | 1 | 929 | | properties.setProperty("Ice.ProgramName", programName + "-" + service); |
| | | 930 | | } |
| | 1 | 931 | | return properties; |
| | | 932 | | } |
| | | 933 | | |
| | | 934 | | private void destroyServiceCommunicator(string service, Ice.Communicator communicator) |
| | | 935 | | { |
| | 1 | 936 | | if (communicator != null) |
| | | 937 | | { |
| | | 938 | | try |
| | | 939 | | { |
| | 1 | 940 | | communicator.shutdown(); |
| | 1 | 941 | | communicator.waitForShutdown(); |
| | 1 | 942 | | } |
| | 0 | 943 | | catch (Ice.CommunicatorDestroyedException) |
| | | 944 | | { |
| | | 945 | | // |
| | | 946 | | // Ignore, the service might have already destroyed |
| | | 947 | | // the communicator for its own reasons. |
| | | 948 | | // |
| | 0 | 949 | | } |
| | 0 | 950 | | catch (Exception e) |
| | | 951 | | { |
| | 0 | 952 | | _logger.warning("ServiceManager: exception while shutting down communicator for service " |
| | 0 | 953 | | + service + "\n" + e.ToString()); |
| | 0 | 954 | | } |
| | | 955 | | |
| | 1 | 956 | | removeAdminFacets("IceBox.Service." + service + "."); |
| | 1 | 957 | | communicator.destroy(); |
| | | 958 | | } |
| | 1 | 959 | | } |
| | | 960 | | |
| | | 961 | | private bool configureAdmin(Ice.Properties properties, string prefix) |
| | | 962 | | { |
| | 1 | 963 | | if (_adminEnabled && properties.getIceProperty("Ice.Admin.Enabled").Length == 0) |
| | | 964 | | { |
| | 1 | 965 | | var facetNames = new List<string>(); |
| | 1 | 966 | | foreach (string p in _adminFacetFilter) |
| | | 967 | | { |
| | 0 | 968 | | if (p.StartsWith(prefix, StringComparison.Ordinal)) |
| | | 969 | | { |
| | 0 | 970 | | facetNames.Add(p[prefix.Length..]); |
| | | 971 | | } |
| | | 972 | | } |
| | | 973 | | |
| | 1 | 974 | | if (_adminFacetFilter.Count == 0 || facetNames.Count > 0) |
| | | 975 | | { |
| | 1 | 976 | | properties.setProperty("Ice.Admin.Enabled", "1"); |
| | | 977 | | |
| | 1 | 978 | | if (facetNames.Count > 0) |
| | | 979 | | { |
| | | 980 | | // TODO: need String.Join with escape! |
| | 0 | 981 | | properties.setProperty("Ice.Admin.Facets", string.Join(" ", facetNames.ToArray())); |
| | | 982 | | } |
| | 1 | 983 | | return true; |
| | | 984 | | } |
| | | 985 | | } |
| | 0 | 986 | | return false; |
| | | 987 | | } |
| | | 988 | | |
| | | 989 | | private void removeAdminFacets(string prefix) |
| | | 990 | | { |
| | | 991 | | try |
| | | 992 | | { |
| | 1 | 993 | | foreach (string p in _communicator.findAllAdminFacets().Keys) |
| | | 994 | | { |
| | 1 | 995 | | if (p.StartsWith(prefix, StringComparison.Ordinal)) |
| | | 996 | | { |
| | 1 | 997 | | _communicator.removeAdminFacet(p); |
| | | 998 | | } |
| | | 999 | | } |
| | 1 | 1000 | | } |
| | 0 | 1001 | | catch (Ice.CommunicatorDestroyedException) |
| | | 1002 | | { |
| | | 1003 | | // Ignored |
| | 0 | 1004 | | } |
| | 0 | 1005 | | catch (Ice.ObjectAdapterDeactivatedException) |
| | | 1006 | | { |
| | | 1007 | | // Ignored |
| | 0 | 1008 | | } |
| | 0 | 1009 | | catch (Ice.ObjectAdapterDestroyedException) |
| | | 1010 | | { |
| | | 1011 | | // Ignored |
| | 0 | 1012 | | } |
| | 1 | 1013 | | } |
| | | 1014 | | |
| | | 1015 | | private readonly Ice.Communicator _communicator; |
| | | 1016 | | private readonly bool _adminEnabled; |
| | | 1017 | | private readonly HashSet<string> _adminFacetFilter; |
| | | 1018 | | private Ice.Communicator _sharedCommunicator; |
| | | 1019 | | |
| | | 1020 | | #pragma warning disable CA2213 |
| | | 1021 | | // This class does not own _logger. |
| | | 1022 | | private readonly Ice.Logger _logger; |
| | | 1023 | | #pragma warning restore CA2213 |
| | | 1024 | | private readonly string[] _argv; // Filtered server argument vector |
| | 1 | 1025 | | private readonly List<ServiceInfo> _services = new(); |
| | | 1026 | | private bool _pendingStatusChanges; |
| | 1 | 1027 | | private readonly Dictionary<ServiceObserverPrx, bool> _observers = new(); |
| | | 1028 | | private readonly int _traceServiceObserver; |
| | 1 | 1029 | | private readonly object _mutex = new(); |
| | | 1030 | | } |