# Luna Streams v.0.6.2 documentation

## Master instance

Service is needing to a master instance for correct work several mechanics. There are following processes:

> 1. Move streams without any feedback to the status handler_lost. Any stream without feedback during STREAM_STATUS_OBSOLETING_PERIOD (20 seconds by default) will be move to status handler_lost.

> 2. Restart stream after failure. If a stream has the status failure service will attempts to restart this stream. Attempt number is limited, each attempt is made after the specified time.

All running service instance choose the master instance for a correct execution these processes (without race conditions and ect). Only master instance can execute this processes.

## Choice of master

Every service try to get a database lock (update a field in the table single_process_lock). If an instance got a lock it savea state master and send heartbeat message to database further. Other instances observe the heartbeat. If heartbeat is ended any service will try to be new master.

Luna-streams master processor module. By the way, the process will be done in one instance at time.

class luna_streams.app.streams_processes.master.DBJob(func, interval, name)

  Job which work with database

class luna_streams.app.streams_processes.master.MasterProcess(hearbeatInterval, jobs)

  Master process. This class is intended for a periodical jobs execution on only one instance of the service.

  A class instance try to get a database lock periodically. If the instance get the lock it will execute jobs and send heartbeat to the database for being a master, logic entry point _lockMaster.

  logger

    logger

  lockContext

    db lock context

  heartbeatInterval

    heartbeat interval

  scheduler

    scheduler for the process method

  _iAmMaster

    internal state, master or not now

  _masterFuture

    saved get

  sessionId

    master lock session id

  shutdown()

    Shutdown scheduler

    Return type:

      `None`