# Lab 6: Event sourcing using event log

In this exercise you will start a script which will poll the Open Content event log every 5th second. If any events are found the script will print information of the event.

The script will persist the last event to a file (`lastevent`) which holds the last event id processed by the script. When started next time and `lastevent` file exists the script will start processing events with id larger than the `lastevent` processed . This means that even if the listener is off. It will continue from last event next time the listener is started. This way no event is missed.

This is how to get the `/eventlog/` endpoint response:

```
 curl -s -u admin:admin localhost:8080/opencontent/eventlog?event=0| jq .
```

Response

```
{
  "events": [
    {
      "id": 1,
      "uuid": "8c7437ce-a7ca-414d-8bfc-7bf2d1054fc3",
      "eventType": "ADD",
      "created": "2019-06-14T09:32:23.000Z",
      "content": {
        "uuid": "8c7437ce-a7ca-414d-8bfc-7bf2d1054fc3",
        "version": 1,
        "created": "2019-06-14T09:32:22.000Z",
        "source": "lab-hans.bringert",
        "contentType": "Concept",
        "batch": false
      }
    },
    {
      "id": 2,
      "uuid": "db09e859-43d4-42f8-a6ca-c810b653ec6a",
      "eventType": "ADD",
      "created": "2019-06-14T09:32:23.000Z",
      "content": {
        "uuid": "db09e859-43d4-42f8-a6ca-c810b653ec6a",
        "version": 1,
        "created": "2019-06-14T09:32:23.000Z",
        "source": "lab-hans.bringert",
        "contentType": "Concept",
        "batch": false
      }
    }
  ]
}
```

Get the last event id use event=-1

```
curl -s -u admin:admin localhost:8080/opencontent/eventlog?event=-1| jq .
```

```
{
  "events": [
    {
      "id": 20,
      "uuid": "0a18480e-1486-4ce5-8f61-ebb67d3d8938",
      "eventType": "DELETE",
      "created": "2019-06-14T09:35:01.000Z",
      "content": {
        "uuid": "0a18480e-1486-4ce5-8f61-ebb67d3d8938",
        "version": 1,
        "created": "2019-06-14T09:32:37.000Z",
        "source": "lab-hans.bringert",
        "contentType": "Article",
        "batch": false
      }
    }
  ]
}
```

In folder `6-event-sourcing`is an example of a bash script which will poll the event log every 5 seconds and print information about what is happening. To try this :

```
./listen.sh
Url [http://127.0.0.1:8080] :
Username  [admin]:
Password  [admin]:
0
'lastevent' file is missing last event in Open Content is:  20
```

Now the script polls the event log every 5 second and print out the events.

Have the listener in a terminal window and add/modify/delete the items in Open Content and see the events.[<br>](https://bringert.gitbook.io/open-content/upload-lab/lab-4-deletion)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.navigaglobal.com/opencontent/use-and-explore/lab-upload/lab-6-event-sourcing-using-event-log.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
