DirWatcher is a Node.js application that monitors a specified directory at a scheduled interval, counts occurrences of a configured magic string in all files, and logs the results. It provides a REST API to manage the task and fetch task details.
- Prerequisites
- Installation
- Database Schema
- API Documentation
- Running the Application
- Additional Improvements
- Node.js (>= 14.x.x)
- npm (>= 6.x.x)
- A running instance of MongoDB
-
Clone the repository:
git clone https://github.com/yourusername/dirwatcher.git cd dirwatcher -
Install dependencies:
npm install
-
Configure environment variables: Create a
.envfile in the root directory and add the following variables:DB_CONNECTION_STRING=mongodb://localhost:27017/dirwatcher DIRECTORY_TO_WATCH=/path/to/directory MAGIC_STRING=your_magic_string INTERVAL=60000 # interval in milliseconds
-
Start the application:
npm start
import mongoose from "mongoose";
const taskSchema = new mongoose.Schema({
startTime: { type: Date, required: true },
endTime: { type: Date },
duration: { type: Number },
filesAdded: { type: [String], default: [] },
filesDeleted: { type: [String], default: [] },
magicStringCount: { type: Number, default: 0 },
status: { type: String, enum: ['in progress', 'success', 'failed'], default: 'in progress' },
directory: { type: String },
interval: { type: String },
magicString: { type: String }
});
export default mongoose.model('TaskRun', taskSchema);
Config Schema
javascript
Copy code
import mongoose from 'mongoose';
const ConfigSchema = new mongoose.Schema({
directory: { type: String },
interval: { type: String },
magicString: { type: String }
});
export default mongoose.model('Config', ConfigSchema);
**API Documentation**
**Start Task**
URL: /start
Method: POST
Description: Starts the background task manually.
Response:
json
Copy code
{
"message": "Task started successfully."
}
**Stop Task**
URL: /stop
Method: POST
Description: Stops the background task manually.
Response:
json
Copy code
{
"message": "Task stopped successfully."
}
**Update Configuration**
URL: /config
Method: PUT
Description: Updates the configuration for the directory to watch, interval, and magic string.
Body:
json
Copy code
{
"directory": "/new/path/to/directory",
"interval": 30000,
"magicString": "new_magic_string"
}
Response:
json
Copy code
{
"message": "Configuration updated successfully."
}
**Get Task Runs**
URL: /tasks
Method: GET
Description: Retrieves the details of all task runs.
Response:
json
Copy code
[
{
"startTime": "2023-05-27T12:00:00Z",
"endTime": "2023-05-27T12:05:00Z",
"duration": 300000,
"filesAdded": ["file1.txt"],
"filesDeleted": ["file2.txt"],
"magicStringCount": 42,
"status": "success"
},
...
]
Running the Application
Ensure MongoDB is running.
Start the application using npm start.
Use the provided API endpoints to manage and monitor the background task.