Run daily job in transaction

This commit is contained in:
M66B 2018-10-24 09:29:17 +00:00
parent 688a661c5d
commit 88c034b610
1 changed files with 39 additions and 30 deletions

View File

@ -61,42 +61,51 @@ public class JobDaily extends JobService {
executor.submit(new Runnable() { executor.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
Log.i(Helper.TAG, "Start daily job"); try {
db.beginTransaction();
// Cleanup message files Log.i(Helper.TAG, "Start daily job");
Log.i(Helper.TAG, "Cleanup message files");
File[] messages = new File(getFilesDir(), "messages").listFiles(); // Cleanup message files
if (messages != null) Log.i(Helper.TAG, "Cleanup message files");
for (File file : messages) File[] messages = new File(getFilesDir(), "messages").listFiles();
if (file.isFile()) { if (messages != null)
long id = Long.parseLong(file.getName()); for (File file : messages)
if (db.message().countMessage(id) == 0) { if (file.isFile()) {
Log.i(Helper.TAG, "Cleanup message id=" + id); long id = Long.parseLong(file.getName());
if (!file.delete()) if (db.message().countMessage(id) == 0) {
Log.w(Helper.TAG, "Error deleting " + file); Log.i(Helper.TAG, "Cleanup message id=" + id);
if (!file.delete())
Log.w(Helper.TAG, "Error deleting " + file);
}
} }
}
// Cleanup attachment files // Cleanup attachment files
Log.i(Helper.TAG, "Cleanup attachment files"); Log.i(Helper.TAG, "Cleanup attachment files");
File[] attachments = new File(getFilesDir(), "attachments").listFiles(); File[] attachments = new File(getFilesDir(), "attachments").listFiles();
if (attachments != null) if (attachments != null)
for (File file : attachments) for (File file : attachments)
if (file.isFile()) { if (file.isFile()) {
long id = Long.parseLong(file.getName()); long id = Long.parseLong(file.getName());
if (db.attachment().countAttachment(id) == 0) { if (db.attachment().countAttachment(id) == 0) {
Log.i(Helper.TAG, "Cleanup attachment id=" + id); Log.i(Helper.TAG, "Cleanup attachment id=" + id);
if (!file.delete()) if (!file.delete())
Log.w(Helper.TAG, "Error deleting " + file); Log.w(Helper.TAG, "Error deleting " + file);
}
} }
}
Log.i(Helper.TAG, "Cleanup log"); Log.i(Helper.TAG, "Cleanup log");
long before = new Date().getTime() - 24 * 3600 * 1000L; long before = new Date().getTime() - 24 * 3600 * 1000L;
int logs = db.log().deleteLogs(before); int logs = db.log().deleteLogs(before);
Log.i(Helper.TAG, "Deleted logs=" + logs); Log.i(Helper.TAG, "Deleted logs=" + logs);
Log.i(Helper.TAG, "End daily job"); db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
db.endTransaction();
Log.i(Helper.TAG, "End daily job");
}
} }
}); });