<commit_before>;; ## Message Queue utilities
;;
(ns com.puppetlabs.mq
  (:import [org.apache.activemq.broker BrokerService]
           [org.apache.activemq ScheduledMessage]
           [org.apache.activemq.usage SystemUsage])
  (:require [cheshire.core :as json]
            [clamq.activemq :as activemq]
            [clamq.protocol.connection :as mq-conn]
            [clamq.protocol.consumer :as mq-consumer]
            [clamq.protocol.seqable :as mq-seq]
            [clamq.protocol.producer :as mq-producer]
            [clojure.tools.logging :as log])
  (:use [cheshire.custom :only (JSONable)]))

(defn- set-store-usage!
   "Configures the `StoreUsage` setting for an instance of `BrokerService`.

   `broker`     - the `BrokerService` to configure
   `megabytes ` - the maximum amount of disk usage to allow for persistent messages"
  [broker megabytes]
  {:pre  [(instance? BrokerService broker)
          ((some-fn nil? integer?) megabytes)]}
  (when megabytes
    (log/info "Setting ActiveMQ StoreUsage limit to " megabytes " MB")
    (-> broker
      (.getSystemUsage)
      (.getStoreUsage)
      (.setLimit (* megabytes 1024 1024)))))

(defn- set-temp-usage!
  "Configures the `TempUsage` setting for an instance of `BrokerService`.

  `broker`     - the `BrokerService` to configure
  `megabytes ` - the maximum amount of disk usage to allow for temporary messages"
  [broker megabytes]
  {:pre  [(instance? BrokerService broker)
          ((some-fn nil? integer?) megabytes)]}
  (when megabytes
    (log/info "Setting ActiveMQ TempUsage limit to " megabytes " MB")
    (-> broker
      (.getSystemUsage)
      (.getTempUsage)
      (.setLimit (* megabytes 1024 1024)))))<commit_msg>(#17108) Introduce a helper function to DRY up mq settings<commit_after>;; ## Message Queue utilities
;;
(ns com.puppetlabs.mq
  (:import [org.apache.activemq.broker BrokerService]
           [org.apache.activemq ScheduledMessage]
           [org.apache.activemq.usage SystemUsage])
  (:require [cheshire.core :as json]
            [clamq.activemq :as activemq]
            [clamq.protocol.connection :as mq-conn]
            [clamq.protocol.consumer :as mq-consumer]
            [clamq.protocol.seqable :as mq-seq]
            [clamq.protocol.producer :as mq-producer]
            [clojure.tools.logging :as log])
  (:use [cheshire.custom :only (JSONable)]))

(defn- set-usage!*
  "Internal helper function for setting `SystemUsage` values on a `BrokerService`
  instance.

  `broker`    - the `BrokerService` instance
  `megabytes` - the value to set as the limit for the desired `SystemUsage` setting
  `usage-fn`  - a function that accepts a `SystemUsage` instance and returns
                the child object whose limit we are configuring.
  `desc`      - description of the setting we're configuring, to be used in a log message
  "
  [broker megabytes usage-fn desc]
  {:pre  [(instance? BrokerService broker)
          ((some-fn nil? integer?) megabytes)
          (fn? usage-fn)
          (string? desc)]}
  (when megabytes
    (log/info "Setting ActiveMQ " desc " limit to " megabytes " MB")
    (-> broker
      (.getSystemUsage)
      (usage-fn)
      (.setLimit (* megabytes 1024 1024))))
  broker)

(defn- set-store-usage!
   "Configures the `StoreUsage` setting for an instance of `BrokerService`.

   `broker`     - the `BrokerService` to configure
   `megabytes ` - the maximum amount of disk usage to allow for persistent messages,
                  or `nil` to use the default value of 100GB.

   Returns the (potentially modified) `broker` object."
  [broker megabytes]
  (set-usage!* broker megabytes #(.getStoreUsage %) "StoreUsage"))

(defn- set-temp-usage!
  "Configures the `TempUsage` setting for an instance of `BrokerService`.

  `broker`     - the `BrokerService` to configure
  `megabytes ` - the maximum amount of disk usage to allow for temporary messages,
                 or `nil` to use the default value of 50GB.

  Returns the (potentially modified) `broker` object."
  [broker megabytes]
  (set-usage!* broker megabytes #(.getTempUsage %) "TempUsage"))
{'fraction_non_alphanumeric': 0.11298606016140866, 'fraction_numerical': 0.008314991440449988, 'mean_word_length': 2.9326923076923075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1429908', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1262, 'n_words': 392}