Java file watchservice quit working

I am using a slightly modified version from this post


The watch service worked perfectly for a couple of months but now it doesn’t see any changes to my directory. I have it setup to run on gateway start. I checked to make sure the directory files are reachable, by using system.file.readFileAsString from the gateway and works fine. If I start a watch service on my local machine in the script console it works as expected on a local test folder. Any ideas on what the problem could be? I’ve stopped and started the service, the gateway, and rebooted…no changes.

Have you checked a thread dump while it’s not responding?

No I haven’t, I will look into doing that and see what turns up

@PGriffith what should I be looking for?

Looks like it is this thread

Thread [script-invoke-async] id=617, (TIMED_WAITING for java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@a201f51)
  sun.misc.Unsafe.park(Native Method)
  java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
  java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)
  java.util.concurrent.LinkedBlockingDeque.pollFirst(Unknown Source)
  java.util.concurrent.LinkedBlockingDeque.poll(Unknown Source)
  sun.nio.fs.AbstractWatchService.poll(Unknown Source)
  sun.reflect.GeneratedMethodAccessor99.invoke(Unknown Source)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  java.lang.reflect.Method.invoke(Unknown Source)
  org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)
  org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)
  org.python.core.PyObject.__call__(PyObject.java:422)
  org.python.core.PyObject.__call__(PyObject.java:426)
  org.python.core.PyMethod.__call__(PyMethod.java:139)
  org.python.pycode._pyx24._doAsync$3(<module:shared.watchService>:29)
  org.python.pycode._pyx24.call_function(<module:shared.watchService>)
  org.python.core.PyTableCode.call(PyTableCode.java:165)
  org.python.core.PyBaseCode.call(PyBaseCode.java:301)
  org.python.core.PyBaseCode.call(PyBaseCode.java:194)
  org.python.core.PyFunction.__call__(PyFunction.java:387)
  org.python.core.PyMethod.instancemethod___call__(PyMethod.java:220)
  org.python.core.PyMethod.__call__(PyMethod.java:211)
  org.python.core.PyMethod.__call__(PyMethod.java:201)
  org.python.core.PyMethod.__call__(PyMethod.java:196)
  com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)
  com.inductiveautomation.ignition.gateway.script.GatewaySystemUtilities$1.run(GatewaySystemUtilities.java:69)
  java.lang.Thread.run(Unknown Source)

Might be a JDK bug; eg:
https://bugs.openjdk.java.net/browse/JDK-7158947
If it only affects the gateway, you can try updating the runtime on the gateway to the latest Java 8 release, but there’s no guarantee it will be fixed.

2 Likes

So weird. I updated to latest version of Java 8. If I invoke the file watcher on the gateway machine in a designer/client it works. If I invoke it on the same machine but in the gateway, it doesn’t work. In case I didn’t mention it, I’m watching a shared drive I connected to Ignition via the Ignition.conf file.

EDIT
I deleted my mapped drive in the config file \\L: and renamed it to \\K: then stopped and started the gateway and I can still read files from the L drive? How is it still connected?

I cannot get this working so I’m going to try a different method. Anyone with any java skills that can help me out? So far I have this

from java.nio.file import FileSystems,Files
from java.io import File
from org.apache.commons.io.monitor import FileAlterationMonitor
from org.apache.commons.io.monitor import FileAlterationObserver

	
class FileWatcher():
	isRunning = False
	
	def __init__(self):
		self.watcher = None
		self.run()
		
	def _doAsync(self):
		self.isRunning = True
		
		directory = File("MyDirectory").toPath()
		observer = FileAlterationObserver(directory)
		listener = FileListener()
		observer.addListener(listener)
		self.watcher = FileAlterationMonitor(1000, observer)
		self.watcher.start()
		
		while self.isRunning:
			???
			
	def run(self):
		if not self.isRunning:
			system.util.invokeAsynchronous(self._doAsync)
	
	def kill(self):
		self.isRunning = False
		self.watcher.stop()