1

Here is the senario:

We have a site running on NodeJS. Periodically, we pull some data from internet, analyze it, and update a MySQL Database.

My questions are:

  1. What is the best practice to create a Linux daemon? gcc? Can I do it in PHP or other languages?
  2. Since NodeJs will be accessing to the same Database, how can we create mutex?
  3. How can we manage the daemon? For example If the daemon crashes, we want to restart it automatically.
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Xi 张熹
  • 10,492
  • 18
  • 58
  • 86

3 Answers3

2

You can use forever.js ... see How does one start a node.js server as a daemon process?. It answers your 1st and 3rd question. I guess you should have searched stack overflow or just have googled a bit !!

Community
  • 1
  • 1
Samyak Bhuta
  • 1,256
  • 7
  • 20
0

You can code a daemon in any language: C, C++, Ocaml, Haskell, ... (but I won't code it in PHP).

The most important in coding a daemon is to be sure the code is robust and fault-detecting.

Concurrent access to the database should be handled by the MySQL server.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

If you only share resources by a shared database, you can use its transaction isolation guarantees to stop other processes seeing incomplete data.

This means that you need to either do your operation atomically in SQL (a single statement) or use a transaction.

In any case, it means you need to use a transactional engine in MySQL (probably InnoDB) and your application needs to be aware of and handle deadlocks correctly.

MarkR
  • 62,604
  • 14
  • 116
  • 151