1.0.0 • Published 5 months ago

cbridge v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Hybrid Child Process Orchestration with Persistent C Daemon

This project demonstrates the concept of Hybrid Child Process Orchestration with a Persistent C Daemon, allowing efficient execution of tasks from a Node.js environment. By maintaining a long-running C daemon, we avoid the overhead of repeatedly spawning new processes, leading to improved performance and resource utilization.

Table of Contents

Introduction

Hybrid Child Process Orchestration with Persistent Daemon is a technique used to optimize the performance and efficiency of applications that require frequent interaction between different programming languages or environments. This project includes a Node.js module (index.js) that interacts with a persistent C daemon (daemon.c), allowing seamless execution of tasks from a Node.js environment.

Features

  • Persistent C Daemon: Maintains a long-running C process to handle incoming tasks, reducing overhead.
  • Error Handling: Detects and handles errors, ensuring the daemon is restarted if it exits unexpectedly.
  • Manual Refresh: Allows manual restarting of the daemon if needed.
  • Parallel Task Execution: Utilizes pthreads to handle multiple tasks concurrently.

Directory Structure

plaintext my-c-daemon-package/ ├── src/ │ ├── daemon.c │ ├── index.js ├── test/ │ ├── test.js ├── .gitignore ├── LICENSE ├── package.json ├── README.md

  • src/: Contains the source code files for the C daemon and Node.js client.
    • daemon.c: The persistent C daemon that executes received tasks in parallel.
    • index.js: The Node.js module that interacts with the C daemon.
  • test/: Contains the test files.
    • test-suite.js: The test suite for the Node.js client.
  • .gitignore: Specifies files and directories to be ignored by Git.
  • LICENSE: Contains the license information for the package.
  • package.json: Contains metadata about the package and its dependencies.
  • README.md: Provides documentation for the package.

Installation

  1. Clone the Repository: bash git clone https://github.com/kunaalhere/c-daemon-bridge.git cd cDaemonBridge

  1. Install Dependencies: bash npm install

  1. Compile the C Daemon: bash gcc -o src/daemon src/daemon.c -lpthread

Usage

Starting the Daemon

The C daemon is automatically started when you require the index.js module in your Node.js code.

javascript const CDaemonBridge = require('./src/index.js');

Executing Tasks

Use the exec method to send tasks to the daemon and receive the results.

javascript CDaemonBridge.exec('TASK 1') .then(result => { console.log(Result: ${result}); // Output: Result: Processing task 1: TASK 1\nTask 1 completed\n }) .catch(err => { console.error(Error: ${err}); });

Refreshing the Daemon

You can manually restart the C daemon using the refresh method.

javascript CDaemonBridge.refresh();

Testing

Run the tests to ensure the system behaves as expected.

Running Tests: 1. Update the package.json to include the test script: json "scripts": { "test": "mocha test/test .js" }

  1. Execute the tests: bash npm test

Error Handling

The module includes comprehensive error handling to ensure the C daemon is restarted if it encounters an error or exits unexpectedly. Errors from the C daemon are logged to the console, and the process is restarted as needed.

Contributing

Contributions are welcome! Please follow these steps: 1. Fork the repository. 2. Create a new branch (git checkout -b feature/your-feature). 3. Commit your changes (git commit -am 'Add your feature'). 4. Push to the branch (git push origin feature/your-feature). 5. Create a new Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.