Mercurial Hosting > luan
diff src/luan/modules/http/LuanHandler.java @ 1766:8df0b80e715e
fix scheduled tasks
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 06 Jun 2023 14:33:24 -0600 |
parents | d6ec67fa4a61 |
children | 9157e0d5936e |
line wrap: on
line diff
--- a/src/luan/modules/http/LuanHandler.java Fri May 26 10:29:55 2023 -0600 +++ b/src/luan/modules/http/LuanHandler.java Tue Jun 06 14:33:24 2023 -0600 @@ -10,9 +10,6 @@ import java.net.BindException; import java.util.List; import java.util.ArrayList; -import java.util.Set; -import java.util.Collections; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import goodjava.logging.Logger; @@ -30,32 +27,30 @@ import luan.LuanException; import luan.modules.PackageLuan; import luan.modules.BasicLuan; +import luan.modules.ThreadLuan; import luan.modules.logging.LuanLogger; public final class LuanHandler implements Handler, Closeable { private static final Logger logger = LoggerFactory.getLogger(LuanHandler.class); - private static final Set<LuanHandler> dontGc = Collections.newSetFromMap(new ConcurrentHashMap<LuanHandler,Boolean>()); - private final Luan luanInit; private final String domain; private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); private volatile Luan currentLuan; private volatile boolean isDisabled = false; private volatile boolean didInit; + private final List<Closeable> closeables = new ArrayList<Closeable>(); private static final Method resetLuanMethod; private static final Method evalInRootMethod; private static final Method disableLuanMethod; - private static final Method dontGcMethod; private static final Method testAsInitMethod; static { try { resetLuanMethod = LuanHandler.Fns.class.getMethod( "reset_luan" ); evalInRootMethod = LuanHandler.Fns.class.getMethod( "eval_in_root", String.class ); disableLuanMethod = LuanHandler.Fns.class.getMethod( "disable_luan" ); - dontGcMethod = LuanHandler.Fns.class.getMethod( "dont_gc" ); testAsInitMethod = LuanHandler.Fns.class.getMethod( "test_as_init", String.class, String.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); @@ -65,26 +60,28 @@ public LuanHandler(Luan luanInit,String domain) { this.luanInit = luanInit; this.domain = domain; + Fns fns = new Fns(this); try { - Fns fns = new Fns(this); LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan"); if( Http.get(luanInit,"reset_luan") == null ) Http.put( luanInit, "reset_luan", new LuanJavaFunction(resetLuanMethod,fns) ); Http.put( luanInit, "eval_in_root", new LuanJavaFunction(evalInRootMethod,fns) ); Http.put( luanInit, "disable_luan", new LuanJavaFunction(disableLuanMethod,fns) ); - Http.put( luanInit, "dont_gc", new LuanJavaFunction(dontGcMethod,fns) ); Http.put( luanInit, "test_as_init", new LuanJavaFunction(testAsInitMethod,fns) ); } catch(LuanException e) { throw new RuntimeException(e); } + if( luanInit.registry().get(ThreadLuan.CLOSEABLES) != null ) + throw new RuntimeException(ThreadLuan.CLOSEABLES+" already set"); + luanInit.registry().put(ThreadLuan.CLOSEABLES,fns); if( domain != null ) - logger.info("new "+domain); + logger.warn("new "+domain); newLuan(); } protected void finalize() throws Throwable { if( domain != null ) - logger.info("gc "+domain); + logger.warn("gc "+domain); } private void init(Luan luan) throws LuanException { @@ -146,9 +143,16 @@ } } - public void close() { - Object obj = dontGc.remove(this); - //logger.info("close "+domain+" "+(obj!=null)); + @Override public void close() { + synchronized(currentLuan) { + for( Closeable c : closeables ) { + try { + c.close(); + } catch(IOException e) { + logger.error(c.toString(),e); + } + } + } } public Object call_rpc(String fnName,Object... args) throws LuanException { @@ -215,12 +219,7 @@ } } - private void dont_gc() { - dontGc.add(this); - //logger.info("dont_gc "+domain); - } - - public static final class Fns { + public static final class Fns implements ThreadLuan.Closeables { private final Reference<LuanHandler> ref; private Fns(LuanHandler lh) { @@ -234,6 +233,10 @@ return lh; } + @Override public void addCloseable(Closeable c) throws LuanException { + lh().closeables.add(c); + } + public void reset_luan() throws LuanException { lh().reset_luan(); } @@ -246,10 +249,6 @@ lh().eval_in_root(text); } - public void dont_gc() throws LuanException { - lh().dont_gc(); - } - public void test_as_init(String text,String sourceName) throws LuanException { lh().test_as_init(text,sourceName); }