App.scala 1.74 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
/*
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
2
3
 * Import Process Delete
 * Copyright (C) 2020 Memoriav
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
4
5
 *
 * This program is free software: you can redistribute it and/or modify
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
6
7
8
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
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
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package ch.memobase

import ch.memobase.models.DeleteMessage
import org.apache.logging.log4j.scala.Logging

import scala.collection.immutable.HashSet

object App
  extends KProducer
    with scala.App
    with ArgParser
    with AppSettings
    with KConsumer
    with MsgFilter
    with Logging {

  parse(args) match {
    case Some((sessionId, filters)) =>
      try {
        Stream
          .continually(poll)
          .takeWhile(records => records.nonEmpty)
          .flatMap(records =>
            records
              .filter(record => filters.forall(f => f(record)))
              .foldLeft(HashSet[DeleteMessage]())((agg, record) => agg + DeleteMessage(record, sessionId))
          )
          .foreach(id => sendDelete(id))
      } catch {
        case e: Exception =>
          logger.error(e)
          sys.exit(1)
      } finally {
        logger.info("Shutting down application")
        closeConsumer()
        closeProducer()
      }
    case None =>
      logger.error("Invalid command-line arguments!")
      sys.exit(1)
  }
}