summaryrefslogtreecommitdiffstats
path: root/dcaeapplib/README.md
blob: 81fd8253e79065777c839e4a3efaa166ed26cca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# dcaeapplib

Library for building DCAE analytics applications

# Example use:

```

class myapp:
  def __init__(self):
    # get the environment, and register callbacks for health checks and
    # configuration changes
    self.dcaeenv = dcaeapplib.DcaeEnv(healthCB=self.isHealthy, reconfigCB=self.reconfigure)
    # simulate a configuration change - we want the initial configuration
    self.reconfigure()
    # start the environment (to wait for health checks and config changes)
    self.dcaeenv.start()
    # begin processing loop
    while True:
      if self.configchanged:
	# fetch the updated configuration
        self.conig = self.dcaeenv.getconfig()
	self.configchanged = False
	# do any setup relevant to the configuration
      data = self.dcaeenv.getdata('myinputstream')
      # Data is a UTF-8 string, 'myinputstream' is this application's logical
      # name for this (one of potentially several) data sources.
      # Can also specify timeout_ms and limit as arguments.
      # timeout_ms (default 15,000) is the maximum time getdata() will block
      # limit is the maximum number of records retrieved at a time.
      # If no data is retrieved (timeout) the return will be None.
      if data is not None:
        # do something to process the data
	self.dcaeenv.senddata('myoutputstream', 'somepartitionkey', data)
	# data is a string, 'myoutputstream' is the application's logical
	# name for this (one of potentially several) data sinks.
	# somepartitionkey is used, by Kafka, to assign the data to a partition.

  def isHealthy(self):
    # return the health of the application (True/False)
    return True

  def reconfigure(self):
    # Do whatever needs to be done to handle a configuration change
    self.configchanged = True
```

# Environment Variables

This library uses the onap-dcae-cbs-docker-client library to fetch
configuration.  That library relies on the HOSTNAME and CONSUL_HOST
environment variables to find the configuration.

This library provides an HTTP interface for health checks.  By default this
listens on port 80 but can be overridden to use another port by setting the
DCAEPORT environment variable.  The HTTP interface supports getting 2 URLs:
/healthcheck, which will return a status of 202 (Accepted) for healthy,
and 503 (Service Unavailable) for unhealthy, and /reconfigure, which triggers
the library to check for updated configuration.

# Console Commands

This library provides a single console command "reconfigure.sh" which
performs an HTTP get of the /reconfigure URL