Our organization has built a few python based windows services to complete some automated tasks; these windows services are installed on the same server running ArcGIS Server so they can use the underlying python deployment and tie into arcpy. These windows services have functioned along side ArcGIS Server quite well until the v10.5 release. At the 10.5 release, we are starting to experience issues. Primarily that...
if the python based windows service is started before the ArcGIS Server service, then all the ArcSOC.exe's fail on launch. We can see the ArcSOC.exe briefly show up taskmgr but they disappear and throw the following exception in the ArcGIS Server manager logs -
| Level | Time | Message | Source | Machine | Code | Time Elapsed | User Name | Process | Thread | Method Name |
| DEBUG | Nov 26, 2018, 5:25:38 PM | Cleaning up previous service instance and server context. | System/PublishingTools.GPServer | <SERVER>.<DOMAIN> | 9999 | | | 3512 | 78 | |
| DEBUG | Nov 26, 2018, 5:25:38 PM | com.esri.arcgis.discovery.servicelib.AGSException: activation failed; nested exception is: java.rmi.activation.ActivationException: object activation failed after 2 tries; nested exception is: java.rmi.ConnectException: Connection refused to host: <xyz.xyz.xyz.xyz>; nested exception is: java.net.ConnectException: Connection refused: connect at com.esri.arcgis.discovery.servicelib.ServerContextFactory.createContext(ServerContextFactory.java:198) at com.esri.arcgis.discovery.ejb.util.EJBBase.initService(EJBBase.java:425) at com.esri.arcgis.discovery.ejb.util.EJBBase.init(EJBBase.java:277) at com.esri.arcgis.discovery.ejb.impl.GPServerSyncBean.init(GPServerSyncBean.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:162) at org.apache.openejb.core.interceptor.ReflectionInvocationContext$LifecycleInvocation.invoke(ReflectionInvocationContext.java:194) at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:144) at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:164) at org.apache.openejb.monitoring.StatsInterceptor.PostConstruct(StatsInterceptor.java:101) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:162) at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:144) at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:122) at org.apache.openejb.core.stateless.StatelessInstanceManager.ceateInstance(StatelessInstanceManager.java:237) at org.apache.openejb.core.stateless.StatelessInstanceManager.createInstance(StatelessInstanceManager.java:448) at org.apache.openejb.core.stateless.StatelessInstanceManager.access$100(StatelessInstanceManager.java:72) at org.apache.openejb.core.stateless.StatelessInstanceManager$BackgroundThread.run(StatelessInstanceManager.java:507) Caused by: java.rmi.activation.ActivateFailedException: activation failed; nested exception is: java.rmi.activation.ActivationException: object activation failed after 2 tries; nested exception is: java.rmi.ConnectException: Connection refused to host: <xyz.xyz.xyz.xyz>; nested exception is: java.net.ConnectException: Connection refused: connect at sun.rmi.server.ActivatableRef.activate(ActivatableRef.java:304) at sun.rmi.server.ActivatableRef.invoke(ActivatableRef.java:133) at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227) at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179) at com.sun.proxy.$Proxy42.setProductCode(Unknown Source) at com.esri.arcgis.discovery.servicelib.ServerContextFactory.a(ServerContextFactory.java:255) at com.esri.arcgis.discovery.servicelib.ServerContextFactory.createContext(ServerContextFactory.java:195) ... 23 more Caused by: java.rmi.activation.ActivationException: object activation failed after 2 tries; nested exception is: java.rmi.ConnectException: Connection refused to host: <xyz.xyz.xyz.xyz>; nested exception is: java.net.ConnectException: Connection refused: connect at sun.rmi.server.Activation$GroupEntry.activate(Activation.java:1184) at sun.rmi.server.Activation$ActivatorImpl.activate(Activation.java:406) at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346) at sun.rmi.transport.Transport$1.run(Transport.java:200) at sun.rmi.transport.Transport$1.run(Transport.java:197) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:196) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276) at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:162) at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227) at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179) at com.sun.proxy.$Proxy41.activate(Unknown Source) at java.rmi.activation.ActivationID.activate(ActivationID.java:118) at sun.rmi.server.ActivatableRef.activate(ActivatableRef.java:277) ... 29 more Caused by: java.rmi.ConnectException: Connection refused to host: <xyz.xyz.xyz.xyz>; nested exception is: java.net.ConnectException: Connection refused: connect at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:130) at java.rmi.activation.ActivationGroup_Stub.newInstance(Unknown Source) at sun.rmi.server.Activation$ObjectEntry.activate(Activation.java:1455) at sun.rmi.server.Activation$GroupEntry.activate(Activation.java:1141) at sun.rmi.server.Activation$ActivatorImpl.activate(Activation.java:406) at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346) at sun.rmi.transport.Transport$1.run(Transport.java:200) at sun.rmi.transport.Transport$1.run(Transport.java:197) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:196) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at java.net.Socket.connect(Socket.java:538) at java.net.Socket.<init>(Socket.java:434) at java.net.Socket.<init>(Socket.java:211) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613) ... 23 more | System/PublishingToolsEx.GPServer | <SERVER>.<DOMAIN> | 9999 | | | 3512 | 83 | |
| DEBUG | Nov 26, 2018, 5:25:38 PM | Could not initialize service 'System.PublishingToolsEx.GPServer'. activation failed; nested exception is: java.rmi.activation.ActivationException: object activation failed after 2 tries; nested exception is: java.rmi.ConnectException: Connection refused to host: <xyz.xyz.xyz.xyz>; nested exception is: java.net.ConnectException: Connection refused: connect | System/PublishingToolsEx.GPServer | <SERVER>.<DOMAIN> | 8275 | | | 3512 | 83 | |
| SEVERE | Nov 26, 2018, 5:25:38 PM | Could not initialize service 'System.PublishingToolsEx.GPServer'. | System/PublishingToolsEx.GPServer | <SERVER>.<DOMAIN> | 8275 | | | 3512 | 83 | |
Here is a quick shell/example of a python based windows service that we have built:
logging_level <SPAN class="operator token">=</SPAN> <SPAN class="string token">"DEBUG"</SPAN>
logfile <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\tmp\A-arcpy-pysvc-test.log'</SPAN>
check_interval <SPAN class="operator token">=</SPAN> <SPAN class="number token">30</SPAN>
<SPAN class="comment token">#standard imports</SPAN>
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> logging
<SPAN class="keyword token">from</SPAN> logging <SPAN class="keyword token">import</SPAN> handlers
<SPAN class="keyword token">import</SPAN> traceback
<SPAN class="keyword token">import</SPAN> socket
<SPAN class="keyword token">import</SPAN> win32serviceutil
<SPAN class="keyword token">import</SPAN> win32service
<SPAN class="keyword token">import</SPAN> win32event
<SPAN class="keyword token">import</SPAN> servicemanager
exception_throw<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">'C:\Program Files\ArcGIS\Server\arcpy'</SPAN><SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">'C:\Program Files\ArcGIS\Server\bin'</SPAN><SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">'C:\Program Files\ArcGIS\Server\ArcToolbox\Scripts'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
exception_throw <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token">####################################</SPAN>
<SPAN class="comment token">### Classes/Functions ###</SPAN>
<SPAN class="comment token">####################################</SPAN>
<SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">WinNtSvc</SPAN> <SPAN class="punctuation token">(</SPAN>win32serviceutil<SPAN class="punctuation token">.</SPAN>ServiceFramework<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
_svc_name_ <SPAN class="operator token">=</SPAN> <SPAN class="string token">"A-arcpy-pysvc-test"</SPAN>
_svc_display_name_ <SPAN class="operator token">=</SPAN> <SPAN class="string token">"A Python ArcPY Service Test"</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">__init__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN>args<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
win32serviceutil<SPAN class="punctuation token">.</SPAN>ServiceFramework<SPAN class="punctuation token">.</SPAN>__init__<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN>args<SPAN class="punctuation token">)</SPAN>
self<SPAN class="punctuation token">.</SPAN>hWaitStop <SPAN class="operator token">=</SPAN> win32event<SPAN class="punctuation token">.</SPAN>CreateEvent<SPAN class="punctuation token">(</SPAN>None<SPAN class="punctuation token">,</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>None<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">SvcStop</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
self<SPAN class="punctuation token">.</SPAN>ReportServiceStatus<SPAN class="punctuation token">(</SPAN>win32service<SPAN class="punctuation token">.</SPAN>SERVICE_STOP_PENDING<SPAN class="punctuation token">)</SPAN>
win32event<SPAN class="punctuation token">.</SPAN>SetEvent<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">.</SPAN>hWaitStop<SPAN class="punctuation token">)</SPAN>
logging<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"***** STOPPING WINDOWS NT SERVICE ON HOST '%s' *****"</SPAN><SPAN class="operator token">%</SPAN>socket<SPAN class="punctuation token">.</SPAN>gethostname<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">SvcDoRun</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
servicemanager<SPAN class="punctuation token">.</SPAN>LogMsg<SPAN class="punctuation token">(</SPAN>
servicemanager<SPAN class="punctuation token">.</SPAN>EVENTLOG_INFORMATION_TYPE<SPAN class="punctuation token">,</SPAN>
servicemanager<SPAN class="punctuation token">.</SPAN>PYS_SERVICE_STARTED<SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">.</SPAN>_svc_name_<SPAN class="punctuation token">,</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">)</SPAN>
self<SPAN class="punctuation token">.</SPAN>main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">############################</SPAN>
<SPAN class="comment token">### SETUP ###</SPAN>
<SPAN class="comment token">############################</SPAN>
<SPAN class="comment token">### SETUP LOGGING ###</SPAN>
logging_levels<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">'DEBUG'</SPAN><SPAN class="punctuation token">:</SPAN>logging<SPAN class="punctuation token">.</SPAN>DEBUG<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'INFO'</SPAN><SPAN class="punctuation token">:</SPAN>logging<SPAN class="punctuation token">.</SPAN>INFO<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'WARNING'</SPAN><SPAN class="punctuation token">:</SPAN>logging<SPAN class="punctuation token">.</SPAN>WARNING<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'ERROR'</SPAN><SPAN class="punctuation token">:</SPAN>logging<SPAN class="punctuation token">.</SPAN>ERROR<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'CRITICAL'</SPAN><SPAN class="punctuation token">:</SPAN>logging<SPAN class="punctuation token">.</SPAN>CRITICAL
<SPAN class="punctuation token">}</SPAN>
lu_log_level<SPAN class="operator token">=</SPAN>logging_levels<SPAN class="punctuation token">[</SPAN>logging_level<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">if</SPAN> logging_levels<SPAN class="punctuation token">.</SPAN>has_key<SPAN class="punctuation token">(</SPAN>logging_level<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="string token">"WARNING"</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"logging to %s"</SPAN><SPAN class="operator token">%</SPAN>logfile
<SPAN class="comment token"># logging.basicConfig(format='[%(asctime)s][%(levelname)s] %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=lu_log_level, filename=logfile, filemode="a")</SPAN>
<SPAN class="comment token"># 1 MB = 1048576 bytes</SPAN>
logger <SPAN class="operator token">=</SPAN> logging<SPAN class="punctuation token">.</SPAN>getLogger<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>setLevel<SPAN class="punctuation token">(</SPAN>lu_log_level<SPAN class="punctuation token">)</SPAN>
filehandler <SPAN class="operator token">=</SPAN> logging<SPAN class="punctuation token">.</SPAN>handlers<SPAN class="punctuation token">.</SPAN>RotatingFileHandler<SPAN class="punctuation token">(</SPAN>logfile<SPAN class="punctuation token">)</SPAN>
formatter<SPAN class="operator token">=</SPAN>logging<SPAN class="punctuation token">.</SPAN>Formatter<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'[%(asctime)s][%(levelname)s][%(name)s][%(lineno)d] %(message)s'</SPAN><SPAN class="punctuation token">,</SPAN> datefmt<SPAN class="operator token">=</SPAN><SPAN class="string token">'%Y/%m/%d %H:%M:%S'</SPAN><SPAN class="punctuation token">)</SPAN>
filehandler<SPAN class="punctuation token">.</SPAN>setFormatter<SPAN class="punctuation token">(</SPAN>formatter<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># write to file handler and standard out</SPAN>
logging<SPAN class="punctuation token">.</SPAN>getLogger<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>addHandler<SPAN class="punctuation token">(</SPAN>filehandler<SPAN class="punctuation token">)</SPAN>
logging<SPAN class="punctuation token">.</SPAN>getLogger<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>addHandler<SPAN class="punctuation token">(</SPAN>logging<SPAN class="punctuation token">.</SPAN>StreamHandler<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
logging<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"***** STARTING '{}' LOOP ON HOST '{}' *****"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">.</SPAN>_svc_name_<SPAN class="punctuation token">,</SPAN>socket<SPAN class="punctuation token">.</SPAN>gethostname<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> logging_levels<SPAN class="punctuation token">.</SPAN>has_key<SPAN class="punctuation token">(</SPAN>logging_level<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logging<SPAN class="punctuation token">.</SPAN>warning<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed to determine logging level. Defaulted to 'WARNING' Options are: %s"</SPAN><SPAN class="operator token">%</SPAN><SPAN class="string token">","</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>logging_levels<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">############################</SPAN>
<SPAN class="comment token">### PRE-LOOP CODE ###</SPAN>
<SPAN class="comment token">############################</SPAN>
<SPAN class="comment token">############################</SPAN>
<SPAN class="comment token">### LOOPING ROUTINE ###</SPAN>
<SPAN class="comment token">############################</SPAN>
logging<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"arcpy loaded from {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> exception_throw<SPAN class="punctuation token">:</SPAN>
logging<SPAN class="punctuation token">.</SPAN>warning<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"unable to load arcpy without adjusting sys.path to: "</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> p <SPAN class="keyword token">in</SPAN> sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">:</SPAN>
logging<SPAN class="punctuation token">.</SPAN>warning<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" - {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
logging<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"starting check every %s seconds"</SPAN><SPAN class="operator token">%</SPAN>check_interval<SPAN class="punctuation token">)</SPAN>
rc<SPAN class="operator token">=</SPAN>None
<SPAN class="keyword token">while</SPAN> rc <SPAN class="operator token">!=</SPAN> win32event<SPAN class="punctuation token">.</SPAN>WAIT_OBJECT_0<SPAN class="punctuation token">:</SPAN>
logging<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Loop"</SPAN><SPAN class="punctuation token">)</SPAN>
logging<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Next check in %s seconds"</SPAN><SPAN class="operator token">%</SPAN>check_interval<SPAN class="punctuation token">)</SPAN>
rc <SPAN class="operator token">=</SPAN> win32event<SPAN class="punctuation token">.</SPAN>WaitForSingleObject<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">.</SPAN>hWaitStop<SPAN class="punctuation token">,</SPAN> check_interval<SPAN class="operator token">*</SPAN><SPAN class="number token">1000</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">##provides an interface to register the service. You can register the service with the system by running</SPAN>
<SPAN class="comment token">#python <service>.py install</SPAN>
<SPAN class="comment token">##one line of executable code</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
win32serviceutil<SPAN class="punctuation token">.</SPAN>HandleCommandLine<SPAN class="punctuation token">(</SPAN>WinNtSvc<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>To install that service, save it as a file (ex: win_svc_sample.py), install the 64-bit version of pywin32 for python 2.7 and run this command from the windows command line as administrator:
python.exe win_svc_sample.py install<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
We've tried a variety of things to resolve including:
- Installing a virtual environment with the pywin32 modules and creating the service from the virtualenv
- Installing ArcGIS Desktop along side server
- Tested on both 10.4.1, 10.5.1, and 10.6.1 on windows 2008 and 2012.
We can run a windows service just fine as long as we do not import arcpy. If we import arcpy then the ArcSOC.exe fail to launch. We were able to achieve success by installing ArcGIS Desktop along side the server, and can confirm that if arcpy is loaded from <module 'arcpy' from 'C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\__init__.pyc'>
then it will function when co-located on the same server as ArcGIS Server. However, if desktop is not there, then arcpy is loaded from <module 'arcpy' from '<C:\Program Files\ArcGIS\Server\ArcPy\arcpy\__init__.pyc>'then the ArcSOC.exe's fail.
Thoughts from the community?
Is anyone else running python based windows services on the same server as ArcGIS for Server?
Any idea why importing arcpy from a windows service would prevent arcgis server from functioning?
We can move these windows services to a new host running ArcGIS Desktop with background geoprocessing in the short term, however we have larger plans to co-locate with server at multiple locations in the future and would really like to ensure compatibility with server. The error
java.rmi.activation.ActivationException: object activation failed after 2 tries; nested exception is: java.rmi.ConnectException: Connection refused to host: <xyz.xyz.xyz.xyz>; nested exception is: java.net.ConnectException: Connection refused: connect
Suggests a firewall block (all are disabled and this works without the python based windows service) or a different component is failing which is listening for an active connection.
Interesting enough.. if the server is started first, then the python windows service, everything appears to work as expected. Including stopping/re-starting arcgis server services (the ArcSOC.exe do not fail to launch as long as the server is running correctly).
Thanks for any help/advise.