rpt_tmp_file_sweep.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Created by Tony on 2018/7/27.
  3. */
  4. let fs = require('fs');
  5. let cron = require('node-schedule');
  6. let remove_file_types = [".xlsx", ".pdf", ".jsp"];
  7. function chkIsRemoveType(file) {
  8. let rst = false;
  9. for (let fType of remove_file_types) {
  10. if (file.indexOf(fType) === (file.length - fType.length)) {
  11. rst = true;
  12. break;
  13. }
  14. }
  15. return rst;
  16. }
  17. let jobObj = {
  18. createJob: function (rule, rootPath) {
  19. let localRule = rule;
  20. if (!localRule) {
  21. // setup schedule rule
  22. localRule = new cron.RecurrenceRule();
  23. // 3:15AM, everyday
  24. localRule.dayOfWeek = [1,2,3,4,5,6,0];
  25. localRule.hour = 3;
  26. localRule.minute = 15;
  27. }
  28. cron.scheduleJob(localRule, function(){
  29. let path = rootPath;
  30. fs.exists(path, function (exists) {
  31. if (exists) {
  32. fs.readdir(path, function (err, files) {
  33. let currentTime = (new Date()).valueOf();
  34. let timeGap = currentTime - (1000 * 60 * 60 * 24 * 0.5); //half day before
  35. files.forEach(function(file,index){
  36. if (chkIsRemoveType(file)) {
  37. let curPath = path + "/" + file;
  38. fs.stat(curPath,function(err,data){
  39. if (timeGap > data.mtime) {
  40. fs.unlink(curPath);
  41. }
  42. });
  43. }
  44. });
  45. })
  46. }
  47. })
  48. });
  49. }
  50. };
  51. module.exports = { rptCronJob: jobObj };