@@ -8,6 +8,26 @@ var PythonShell = require('python-shell');
8
8
// web content
9
9
var client = fs . readFileSync ( 'client.html' ) ;
10
10
11
+ // ref: http://stackoverflow.com/questions/18052762/remove-directory-which-is-not-empty
12
+ var deleteFolderRecursive = function ( path ) {
13
+ if ( ! ( path == './tmp/del' || path == './tmp/input' || path == './tmp/output' ) ) {
14
+ return console . log ( 'Bad Try with rm -r' ) ;
15
+ }
16
+
17
+ if ( fs . existsSync ( path ) ) {
18
+ fs . readdirSync ( path ) . forEach ( function ( file , index ) {
19
+ var curPath = path + "/" + file ;
20
+ if ( fs . lstatSync ( curPath ) . isDirectory ( ) ) { // recurse
21
+ deleteFolderRecursive ( curPath ) ;
22
+ } else { // delete file
23
+ fs . unlinkSync ( curPath ) ;
24
+ }
25
+ } ) ;
26
+ fs . rmdirSync ( path ) ;
27
+ }
28
+ } ;
29
+
30
+
11
31
// http server: send the web content
12
32
var server = http . createServer ( function ( req , res ) {
13
33
res . writeHead ( 200 , { } ) ;
@@ -70,6 +90,45 @@ ioserver.on('connection', function (socket) {
70
90
} ) ;
71
91
} ) ;
72
92
73
- // run the server
93
+ // create the cron job to clean folder
94
+ var CronJob = require ( 'cron' ) . CronJob ;
95
+ //new CronJob('00 30 12 * * *', function () {
96
+ // console.log('You will see this message every day on 12:30 AM as LAX time.');
97
+ //}, null, true, 'America/Los_Angeles');
98
+ new CronJob ( '00 30 12 * * *' , function ( ) {
99
+
100
+ var clean = function ( path ) {
101
+ if ( ! ( path == './tmp/del' || path == './tmp/input' || path == './tmp/output' ) ) {
102
+ return console . log ( 'Bad Try with clean function()' ) ;
103
+ }
104
+ fs . stat ( path , function ( err , stats ) {
105
+ if ( err ) {
106
+ return console . log ( err ) ;
107
+ }
108
+ if ( stats . isDirectory ( ) ) {
109
+ deleteFolderRecursive ( path ) ;
110
+ console . log ( "rm -r " + path ) ;
111
+ fs . mkdir ( path , function ( ) {
112
+ console . log ( "Re-mkdir" + path ) ;
113
+ } ) ;
114
+ }
115
+ } ) ;
116
+ } ;
117
+ server . close ( )
118
+ //fixme: close socket.io server
119
+ console . log ( 'Server restart' ) ;
120
+ var my_clean_list = function ( ) {
121
+ var folder_1 = './tmp/input' ;
122
+ clean ( folder_1 ) ;
123
+ var folder_2 = './tmp/output' ;
124
+ clean ( folder_2 ) ;
125
+ } ;
126
+
127
+ my_clean_list ( ) ;
128
+
129
+ return server . listen ( port ) ;
130
+ //fixme: re-open socket.io server
131
+ } , null , true , 'America/Los_Angeles' ) ;
74
132
133
+ // run the server
75
134
server . listen ( port ) ;
0 commit comments