Strategy for server 'old file' delete

A

Andrew Thompson

Now that I have released the JOLC
( http://www.physci.org/javac.jsp ),
I need a way to clear the directory of old
directories (each session gets it's own).

What would be the best strategy for deleting
any directories more than 24 hrs old?

My initial thoughts are to have a thread
running constantly (which, I am not sure
how to achieve), have it Thread.sleep for
24hrs, when it wakes up, get the creation
dates of all the target directories and delete
those more than 24hrs old.

Any better ways to approach the problem?
[ It's just that my approach seems 'hackish',
and I am not even sure how to keep the
'deleter' class loaded.. ]
 
M

Michael Borgwardt

Andrew said:
Any better ways to approach the problem?
[ It's just that my approach seems 'hackish',
and I am not even sure how to keep the
'deleter' class loaded.. ]

Why keep it "loaded". Sounds to me like a task
to be started periodically through OS-specific
means (cron in unix, scheduled tasks in Windows).
In fact, I wouldn't even write it in Java; that's
a one-line shell script.
 
A

Andrew Thompson

message | Andrew Thompson wrote:
| > Any better ways to approach the problem?
.....
| Why keep it "loaded". Sounds to me like a task
| to be started periodically through OS-specific
| means (cron in unix, scheduled tasks in Windows).
| In fact, I wouldn't even write it in Java; that's
| a one-line shell script.

I think you've hit the nail on the head Michael.
'Overkill' - just because it may be possible
to program it in Java, does not mean it makes
sense to do so.

I am running under Unix (and yes, I've seen
'Cron jobs' mentioned in the control panel)
I know my server will run .cgi scripts, would
a cgi script do the job?
 
M

Michael Borgwardt

Andrew said:
I am running under Unix (and yes, I've seen
'Cron jobs' mentioned in the control panel)
I know my server will run .cgi scripts, would
a cgi script do the job?

CGI is a web-specific thing, i.e. an ad-hoc program
(can be a shell script, can be anything) that processes
a WWW request to generate dynamic output.

What you need is a simple shell script using
the "find" utility. It'll probably look like this:

#! /bin/sh
find /var/www/jloc -type d -ctime +1 -ecex rm -r '{}' ';'

OK, that's not exactly "simple" to someone new to shell scripts.
But the admin of the machine should be familiar with them and
able to get a correct one running and added to the cron jobs
in a minute or two.
 
A

Andrew Thompson

...
| I know my server will run .cgi scripts, would
| a cgi script do the job?

Wait, no.. found a perl script
'for(<*.*>){unlink if -M>1}'

I'll toddle off now and figure
how to apply it..

Now.. muttering.. if I put..
for(</public_compile/*>){unlink if -M>1}
that might just do it, ..might also delete
every directory on my site, but hey,
that could be exciting too.. ;-)
 
G

Gregory A. Swarthout

Andrew Thompson said:
Now that I have released the JOLC
( http://www.physci.org/javac.jsp ),
I need a way to clear the directory of old
directories (each session gets it's own).

What would be the best strategy for deleting
any directories more than 24 hrs old?

My initial thoughts are to have a thread
running constantly (which, I am not sure
how to achieve), have it Thread.sleep for
24hrs, when it wakes up, get the creation
dates of all the target directories and delete
those more than 24hrs old.

Any better ways to approach the problem?
[ It's just that my approach seems 'hackish',
and I am not even sure how to keep the
'deleter' class loaded.. ]

If you are using servlets, you can tell the servlet container to
do the cleanup for you when the session is destroyed. You register
an HttpSessionListener via the web.xml file like this:

<web-app>
<display-name>Access Reports</display-name>
<description>Reports on user accesses</description>
<listener>
<listener-class>com.domain.DirCleaner</listener-class>
</listener>
etc.
</web-app>

And then your DirCleaner can look similar to:

import java.io.File;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSession;

/**
* Deletes report files that a session generated
*/
public class DirCleaner implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent event) {
// Nothing to do
}

public void sessionDestroyed(HttpSessionEvent event) {
// Delete files generated by this session
System.out.println("Session destroyed. Cleaning files...");
HttpSession session = event.getSession();
File dir = new File(session.getServletContext().getRealPath("/"));
File[] files = dir.listFiles();
for (int x = 0; x < files.length; x++) {
// Only delete files with session ID in it.
if (files[x].getName().indexOf(session.getId()) == -1) {
continue;
}

if (files[x].isDirectory()) {
File[] subFiles = files[x].listFiles();
for (int i = 0; i < subFiles.length; i++) {
subFiles.delete();
}
}
System.out.println("Deleting " + files[x].getName());
files[x].delete();
}
}

}

There may be something similar you can do in a JSP enviroment since JSPs are
so closely related to servlets.

Greg
 
T

Tony Morris

You might also want to look at java.util.Timer and javax.swing.Timer.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
A

Andrew Thompson

Thanks Greg! I've saved that for close study later.

I have decided to go with the much simpler
'one line script' suggested by Michael on
this one, but I do need a more general
solution for later projects, so that'll come in
real handy.. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top