-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCronTasks.kt
45 lines (41 loc) · 1.33 KB
/
CronTasks.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.akagiyui.drive.task
import com.akagiyui.common.delegate.LoggerDelegate
import com.akagiyui.drive.entity.FileInfo
import com.akagiyui.drive.service.FileInfoService
import com.akagiyui.drive.service.UserFileService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
/**
* 定时任务
*
* @author AkagiYui
*/
@Component
class CronTasks @Autowired constructor(
private val fileInfoService: FileInfoService,
private val userFileService: UserFileService,
) {
private val log by LoggerDelegate()
init {
log.info("CronTasks initialized")
}
/**
* 删除未使用的文件
*
* 北京时间每天凌晨4点执行
*/
@Scheduled(cron = "0 0 4 * * ? ", zone = "Asia/Shanghai")
@Transactional
fun removeUnusedFile() {
log.info("Start remove unused file")
// 遍历所有文件,如果没有被引用则删除
fileInfoService.getAllFileInfo().forEach { fileInfo: FileInfo ->
if (!userFileService.existByFileId(fileInfo.id)) {
log.info("Remove unused file: ${fileInfo.name}")
fileInfoService.deleteFile(fileInfo.id)
}
}
}
}