## Class daemon

zeep::http::daemon — A class to create daemon processes easily.

## Synopsis

    // In header: <zeep/http/daemon.hpp>

    class daemon {
    public:
      // types
      typedef std::function< basic_server *()> server_factory_type;  // The factory for creating server instances.

      // construct/copy/destruct
      daemon(server_factory_type &&, const std::string &, const std::string &,
             const std::string &);
      daemon(server_factory_type &&, const char *);

      // public member functions
      void set_max_restarts(int, int);
      int start(const std::string &, uint16_t, size_t, size_t,
                const std::string &);
      int stop();
      int status();
      int reload();
      int run_foreground(const std::string &, uint16_t);

      // private member functions
      void daemonize();
      void open_log_file();
      bool run_main_loop(const std::string &, uint16_t, size_t, size_t,
                         const std::string &);
      bool pid_is_for_executable();
    };

## Description

In UNIX a daemon is a process that runs in the background. In the case of libzeep this is of course serving HTTP requests. stderr and stdout are captured and written to the log files specified and a process ID is store in the pid file which allows checking the status of a running daemon.

### `daemon` public construct/copy/destruct

1.     daemon(server_factory_type && factory, const std::string & pid_file,
              const std::string & stdout_log_file,
              const std::string & stderr_log_file);

   constructor with separately specified files

   Parameters:

   `factory` | The function object that creates server instances
   `pid_file` | The file that will contain the process ID, usually in /var/run/<process_name>
   `stderr_log_file` | The file that will contain the stderr log, usually in /var/log/<process_name>/error.log
   `stdout_log_file` | The file that will contain the stdout log, usually in /var/log/<process_name>/access.log

2.     daemon(server_factory_type && factory, const char * name);

   constructor with default files

   Parameters:

   `factory` | The function object that creates server instances
   `name` | The process name to use, will be used to form default file locations

### `daemon` public member functions

1.     void set_max_restarts(int nr_of_restarts, int within_nr_of_seconds);

   Avoid excessive automatic restart due to failing to start up.

   Parameters:

   `nr_of_restarts` | The max number of attempts to take to start up a daemon process
   `within_nr_of_seconds` | The restart counter will only consider a failed restart if it fails starting up within this period of time.

2.     int start(const std::string & address, uint16_t port, size_t nr_of_procs,
                 size_t nr_of_threads, const std::string & run_as_user);

   Start the daemon, forking off in the background.

   Parameters:

   `address` | The address to bind to
   `nr_of_threads` | The number of threads to pass to the server class
   `port` | The port number to bind to
   `run_as_user` | The user to run the forked process. Daemons are usually started as root and should drop their privileges as soon as possible.

3.     int stop();

   Stop a running daemon process. Returns 0 in case of successfully stopping a process.

4.     int status();

   Returns 0 if the daemon is running.

5.     int reload();

   Force the running daemon to restart.

6.     int run_foreground(const std::string & address, uint16_t port);

   Run the server without forking to the background.

   For debugging purposes it is sometimes useful to start a server without forking so you can see the stdout and stderr. Often this is done by adding a –no-daemon flag to the program options.

### `daemon` private member functions

1.     void daemonize();

2.     void open_log_file();

3.     bool run_main_loop(const std::string & address, uint16_t port,
                          size_t nr_of_procs, size_t nr_of_threads,
                          const std::string & run_as_user);

4.     bool pid_is_for_executable();