Changeset 2d1ac7b


Ignore:
Timestamp:
Dec 11, 2015 3:13:40 PM (4 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
00a5d195
Parents:
2852383
Message:

Wrapper: Listen for Windows Service shutdown events and shutdown router hard.
As a result, event log will now show "shutdown" instead of "crashed".

Location:
apps/routerconsole/java/src/net/i2p/router/web
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java

    r2852383 r2d1ac7b  
    161161     */
    162162    synchronized static void registerSignalHandler(RouterContext ctx) {
    163         if (ctx.hasWrapper() && _wrapperListener == null &&
    164             !SystemVersion.isWindows()) {
     163        if (ctx.hasWrapper() && _wrapperListener == null) {
    165164            String wv = System.getProperty("wrapper.version");
    166165            if (wv != null && VersionComparator.comp(wv, LISTENER_AVAILABLE) >= 0) {
  • apps/routerconsole/java/src/net/i2p/router/web/WrapperListener.java

    r2852383 r2d1ac7b  
    44import net.i2p.router.RouterContext;
    55import net.i2p.util.Log;
     6import net.i2p.util.SystemVersion;
    67
    78import org.tanukisoftware.wrapper.WrapperManager;
    89import org.tanukisoftware.wrapper.event.WrapperControlEvent;
     10import org.tanukisoftware.wrapper.event.WrapperServiceControlEvent;
    911import org.tanukisoftware.wrapper.event.WrapperEvent;
    1012import org.tanukisoftware.wrapper.event.WrapperEventListener;
     
    2931    public WrapperListener(RouterContext ctx) {
    3032        _listener = new SignalHandler(ctx);
    31         long mask = WrapperEventListener.EVENT_FLAG_CONTROL;
     33        long mask = SystemVersion.isWindows() ? WrapperEventListener.EVENT_FLAG_SERVICE :
     34                                                WrapperEventListener.EVENT_FLAG_CONTROL;
     35
    3236        WrapperManager.addWrapperEventListener(_listener, mask);
    3337    }
     
    5660
    5761        public void fired(WrapperEvent event) {
    58             if (!(event instanceof WrapperControlEvent))
     62            Log log = _ctxt.logManager().getLog(ConfigServiceHandler.class);
     63            if (SystemVersion.isWindows() && (event instanceof WrapperServiceControlEvent)) {
     64                WrapperServiceControlEvent wcse = (WrapperServiceControlEvent) event;
     65                int code = wcse.getServiceControlCode();
     66                switch (code) {
     67                  case WrapperManager.SERVICE_CONTROL_CODE_STOP:       // 1
     68                  case WrapperManager.SERVICE_CONTROL_CODE_SHUTDOWN:   // 5
     69                    log.log(Log.CRIT, "Hard shutdown initiated by Windows service control: " + code);
     70                    // JVM will call ShutdownHook if we don't do it ourselves
     71                    ConfigServiceHandler.registerWrapperNotifier(_ctxt, Router.EXIT_HARD, false);
     72                    _ctxt.router().shutdown(Router.EXIT_HARD);
     73                    break;
     74
     75                  // TODO Power suspend/resume?
     76                  // Warning, definitions not available in 3.2.0, use integers
     77                  // Tanuki doesn't usually mark things with @since, sadly
     78                  // case 35xx // WrapperManager.SERVICE_CONTROL_POWEREVENT_ ...
     79                  //  break;
     80
     81                  default:
     82                    if (log.shouldWarn())
     83                        log.warn("Unhandled control event code: " + code);
     84                    break;
     85                }
    5986                return;
     87            } else if (!(event instanceof WrapperControlEvent)) {
     88                if (log.shouldWarn())
     89                    log.warn("Got unhandled event: " + event);
     90                return;
     91            }
    6092            WrapperControlEvent wce = (WrapperControlEvent) event;
    61             Log log = _ctxt.logManager().getLog(ConfigServiceHandler.class);
    6293            if (log.shouldLog(Log.WARN))
    6394                log.warn("Got signal: " + wce.getControlEventName());
Note: See TracChangeset for help on using the changeset viewer.