# Enforcing events to be outputted in order

**URL:** <https://discuss.elastic.co/t/enforcing-events-to-be-outputted-in-order/311552>\
**Category:** Logstash\
**Created:** [August 5, 2022, 3:27pm UTC](https://discuss.elastic.co/t/enforcing-events-to-be-outputted-in-order/311552 "2022-08-05T15:27:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebram96](https://avatars.discourse-cdn.com/v4/letter/e/f6c823/32.png) [@ebram96](https://discuss.elastic.co/u/ebram96)\
**Post date:** [August 5, 2022, 3:27pm UTC](https://discuss.elastic.co/t/enforcing-events-to-be-outputted-in-order/311552/1 "2022-08-05T15:27:07Z")

</div>

I've a pipeline like this:

```auto
input {
  jdbc {
    jdbc_driver_library => "/usr/share/logstash/logstash-core/lib/jars/postgresql-42.2.6.jar"
    jdbc_driver_class => "org.postgresql.Driver"
    jdbc_connection_string => "${JDBC_CONNECTION_STRING}"
    jdbc_user => "${JDBC_USER}"
    jdbc_password => "${JDBC_PASSWORD}"
    statement =>
        # Select everything from the my_table and
        # add a fake record to indicate the end of the results.
        "SELECT * FROM my_table
         UNION ALL
         SELECT 'END_OF_QUERY_RESULTS' AS my_key;
        "
  }
}

filter {
  ruby {
    init => "
      require 'net/http'
      require 'json'
    "
    code => '
       if event.get("some_key") == "END_OF_QUERY_RESULTS"
          uri = URI.parse(ENV["MY_URL"])
          response = Net::HTTP.get_response(uri)
          result = JSON.parse(response.body)

          if response.code == "202"
            puts "Success!"
          else
            puts "ERROR: Couldn\'t start processing."
          end
          
          event.cancel()
          
       end
    '
  }

}

output {
  mongodb {
    bulk => true
    bulk_interval => 2
    collection => "${MONGO_DB_COLLECTION}"
    database => "${MONGO_DB_NAME}"
    generateId => true
    uri => "mongodb://${MONGO_DB_HOST}:${MONGO_DB_PORT}/${MONGO_DB_NAME}"
  }
}

```

I simply grab all the data from a PostreSQL table to a MongoDB collection.

What I'm trying to achieve is: I want to call an API after loading _ALL_ the data into MongoDB collection.

What I tried:

- I tried the above approach to add a fake record at the end of the SQL query results to use as a flag to indicate the last event. The problem with this approach is LogStash does _not_ maintain the order of events, hence, the event with 'END\_OF\_QUERY\_RESULTS' string can become to the filter before it is actually the last one.
- Setting `pipeline.workers: 1` and `pipeline.ordered: true`, both don't seem to work.
- I tried to sleep for a while in the Ruby filter and it works but I don't/can't really know how much time I should sleep.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 2, 2022, 3:27pm UTC](https://discuss.elastic.co/t/enforcing-events-to-be-outputted-in-order/311552/2 "2022-09-02T15:27:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
