4

I have the following back-end with socket.io. Each time we open a connection by localhost:3000 in a new browser tab, a file named with the socket id is created in the folder tmp/ of the server.

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

server.listen(3000);

app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket) {
    var fs = require('fs');
    fs.writeFile("tmp/" + socket.id, "hello", function (err) {
        if (err) { return console.log(err) };
        console.log('a file is saved!');
    });
});

Because sockets may be disconnected, inactive or idle later. I want to set a simple cleaning rule in the server. For example, at 4pm of everyday, I want to delete all the files that were saved before 4pm of yesterday.

So does anyone know how to set up this regular checking+deletion every day?

Edit 1: Following the discussion with @georoot , I realise I need to send a notification message to the connections whose tmp file was just removed. And I would prefer to have all the network code within the website nodejs code (though the nodejs code may call external bash).

SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • `cron job` did you try that? – georoot Feb 24 '17 at 13:36
  • I see... So are you saying I don't have to implement this in my website javascript code? I could well make a separate scheduled job in my server? – SoftTimur Feb 24 '17 at 13:39
  • Exactly .. it is related to `fs` there is no need for any nodejs. Let me write a simple bash script to help you out :) – georoot Feb 24 '17 at 13:40
  • But wait, there is still an advantage of implementing this in the website. Because I may want to send a message to the (eg, idle) connections whose tmp file is just removed. – SoftTimur Feb 24 '17 at 13:42
  • You can do that in bash also.You see in linux everything is files, you in bash it would be something like `echo "message" > /tmp/socketid` Assuming i am following correctly and /tmp/socketid is created but socketio library.... if thats not the case, you can write a node script and invoke that before deleting the file to send message to connection – georoot Feb 24 '17 at 13:46
  • I don't really understand your last comment... How could I send messages to connections **by bash**? – SoftTimur Feb 24 '17 at 13:52
  • in bash you have the connection id from the filename. Simple call `node my_script.js $id`, this would pass $id as a param to node script which you can parse and send message to – georoot Feb 24 '17 at 13:53
  • But this `my_script.js` is separate from my website code, right? I would prefer to manage all the code together. Isn't there a nodejs solution for my question? – SoftTimur Feb 24 '17 at 13:59
  • Sure rather, you can use nodejs to delete those files, pretty sure there should be some alternative to cron job also in nodejs. – georoot Feb 24 '17 at 14:18
  • Thank you, the discussion is helpful... – SoftTimur Feb 24 '17 at 14:19

2 Answers2

2

Have you considered node-cron? EDIT: In combination with the fs.stats documentation you can use the mtime (modified time) to filter each file. And fs.readdir to get the files.

Myonara
  • 1,197
  • 1
  • 14
  • 33
  • ok... then how could i remove all the files whose last save was before 4pm of yesterday? – SoftTimur Feb 26 '17 at 18:47
  • this is best answer among others , and will quickly get you what you need. - You need to setup node-cron, and then actually use `fs` module to do your filesystem checks and jobs. Working at my side. – BlackStork Feb 26 '17 at 21:24
0

This is more of bash related question than nodejs.

Identify files for deletion

First lets write a script that will check for files that were created 24 hours before the current time. To get the time of file

date -r $filename

Run inside loop to find files that needs to be deleted. You can simply pipe to output from ls over here.

Delete files

Simply delete the files using

rm -rf $filename

that were identified in last step.

Add to cron

Make a cron job to invoke this script everyday.

georoot
  • 3,557
  • 1
  • 30
  • 59