I'm tasked with creating a log of active memory us...
# cfml-general
c
I'm tasked with creating a log of active memory usage. I'm struggeling with terminating threds. I can't seem to get the terminate() function in my CFC to stop the thredded filewrite. Is this even possible? I'm leaning towards not possbile for this task. What are your thoughts/suggestions?
Code will append a line o the file every second. Again, I can't seem to terminate to stop the filewrite. Just doesn't work. I've been doing CFML 20+ years but haven't worked with threads much and so I'm courious if anyone else here has suggestion
component {
function terminate(){ for(thread in application.memoryUsage.threadNames){ try { thread action="terminate" name=thread; writeOutput('terminated #thread#<br>'); } catch (any e) { writeOutput(e.message & '<br>'); } } } function logMemory(threadName){ if(!isDefined('application.memoryUsage.threadNames')) { application.memoryUsage.threadNames = []; } arrayPrepend(application.memoryUsage.threadNames,arguments.threadName); if(arrayLen(application.memoryUsage.threadNames) > 3){ application.memoryUsage.threadNames = arraySlice(application.memoryUsage.threadNames, 1, 3); }; thread action="run" name=application.memoryUsage.threadNames[1] { application.memoryUsage.mem = memUsage('MEMLOGINTERVAL'); local.fileName = ExpandPath('logs\memLog_#DateFormat(now(),'yyyy-mm-dd')#.csv'); if(!fileExists(local.fileName)) FileWrite(local.fileName, 'total,max,used,free,used_pct,timestamp' & Chr(13) & Chr(10)); local.fileObj = FileOpen(local.fileName, "append"); FileWriteLine(local.fileObj,application.memoryUsage.mem.total & ',' & application.memoryUsage.mem.max & ',' & application.memoryUsage.mem.used & ',' & application.memoryUsage.mem.free & ',' & application.memoryUsage.mem.used_pct & ',' & application.memoryUsage.mem.timestamp); FileClose(local.fileObj); } sleep(1000); logMemory(threadName=CreateUUID()); thread action="join" name=ArrayToList(application.memoryUsage.threadNames); } // Returns total/free memory returned from java.lang.Runtime function memUsage(systemvar){ application.memoryUsage.javaObj = { system: createObject("java", "java.lang.System"), runtime: createObject("java", "java.lang.Runtime").getRuntime() }; return { total: application.memoryUsage.javaObj.runtime.totalMemory(), // Returns the total amount of memory in the Java virtual machine. max: application.memoryUsage.javaObj.runtime.maxMemory(), // Returns the maximum amount of memory that the Java virtual machine will attempt to use. used: application.memoryUsage.javaObj.runtime.totalMemory() - application.memoryUsage.javaObj.runtime.freeMemory(), free: application.memoryUsage.javaObj.runtime.freeMemory(), // Returns the amount of free memory in the Java Virtual Machine. used_pct: round(application.memoryUsage.javaObj.runtime.maxMemory() / ( application.memoryUsage.javaObj.runtime.totalMemory() - application.memoryUsage.javaObj.runtime.freeMemory() )), // % of max memory used, timestamp: DateFormat(now(),"yyyy-MM-dd HHmmss") }; }
s
sorry to give an annoying non-answer, but surely Fusionreactor would be a better fit for this than trying to roll your own?
c
Choice not within my control. Employer request
@bdw429s??
b
@C. Jason Wilson I THINK cfthread action=terminate interrupts the thread, I'm fairly sure of that in Lucee at least.
And native code (like writing a file) can't be interrupted by design (In Java)
So the question is now long the write is taking
If the write takes a long time, then you just have to wait (unless you want to write the file via Java which is quite messy)
If the writes are fast, but happen often, then usually putting some code that is easily interruptible like
Copy code
sleep( 100 )
will help. A sleep will throw an interuptedException when the thread gets interrupted.
But as Samuel said, there are probably several existing tools out there which will do this much better.
s
yeah this assignment would cause me to question my life choices
a
@bdw429s thanks for pitching in. @C. Jason Wilson, just a coupla etiquette things: • if you need to show ppl a bunch of code can you please at a minimum format it as code. Better yet if it's more than like a dozen lines, consider creating a gist and posting a link to it instead. • please do not
@
people who are not already in the thread for help. It's great that Brad took the time to look-in, but it's not the best form to actively ping ppl without solicitation. We should leave it up to the ppl themselves if they want to pitch in. You kinda put ppl on the spot by
@
-ing them. No harm done.
c
@bdw429s Thanks for the response Brad. I'll try that. Hope all is well with you. take care.