package cn.hmsoft.frame.thread;

import java.io.File;
import java.time.LocalDate;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.frame.data.dao.FrameThreadDao;
import cn.hmsoft.log.LogHelper;

/**********************
 * 自动清理数据
 * 
 * @author zxq
 * @create 2019-06-30 21:36:57
 * @version 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public class FrameAutoClearThread implements Runnable {

	@Override
	public void run() {
		FrameThreadDao daoThread = SpringHelper.getBean(FrameThreadDao.class);
		try {
			// 第一次启动时候清理线程
			daoThread.initThread();
		}catch(Exception e) {
			
		}
		while (true) {
			LocalDate date = LocalDate.now().minusDays(1);
			// 清理线程
			try {
				daoThread.clearThread(date.atStartOfDay());
				daoThread.clearRunnningThread();
			} catch (Exception e) {
				LogHelper.error(e);
			}
			try {
				// 清理缓存
				File path = new File(SpringHelper.getWebTemporaryPath(date));
				if (path.exists()) {
					for(File f: path.listFiles()) {
						try {
							f.delete();
						}catch(Exception e) {
							
						}
					}
				}
				try {
					path.delete();
				}catch(Exception e) {
					
				}

			} catch (Exception e) {
					LogHelper.error(e);
			}
			

			try {
				Thread.sleep(1000 * 60 * 60 * 24);
			} catch (Exception e) {

			}
		}

	}

}
