<commit_before>(ns circle.workers
  (:require [circle.backend.build.run :as run])
  (:require [circle.backend.project.circle :as circle])
  (:require [circle.backend.build.config :as config])
  (:use [clojure.tools.logging :only (infof errorf error)])
  (:require [circle.ruby :as ruby])
  (:use midje.sweet)
  (:require [clj-airbrake.core :as airbrake])
  (:require [circle.env :as env])
  (:require [fs]))

; TODO: find another home for this
(defn run-build-from-jruby
  [project-name job-name]
  (let [build (config/build-from-name project-name :job-name (keyword job-name))]
    (run/run-build build)))


;;; Workers. Handles starting workers, checking if they're done, and getting the
;;; result.
(defn call-clojure-from-ruby [f args]
  (ruby/->ruby (apply f args)))

(def worker-store (ref {}))

(defmacro log-future
  "Takes a function to be called in a future. Starts the future and logs the result at the end"
  [& body]
  `(future
     (try
       (let [result# (do ~@body)]
         (infof "%s returned %s" (quote  ~@body) result#)
         result#)
       (catch Exception e#
         (error e# "%s threw" (quote ~@body))
         (airbrake/notify "3ccfad1720b6acd8c82e9b30d4b95a30" env/env (fs/cwd) e# {:body (quote ~@body) :future true :url "http://fakeurl.com"})
         (throw e#)))))

(fact "log-future returns futures"
  ;; A normal future will return a future object here, even if the
  ;; code obviously throws an exception. Make sure that's the case
  ;; here as well.
  (log-future (/ 1 0)) => future?)

(fact "log-future works"
  @(log-future (apply + [1 1])) => 2)

(defn start-worker [f & args]
  "Call fn with args as a worker. Returns the id of the worker"
  (let [fut (log-future
             (call-clojure-from-ruby f args))]<commit_msg>Refactor airbrake into its own function, don't send airbrake notifies when running tests<commit_after>(ns circle.workers
  (:require [circle.backend.build.run :as run])
  (:require [circle.backend.project.circle :as circle])
  (:require [circle.backend.build.config :as config])
  (:use [clojure.tools.logging :only (infof errorf error)])
  (:require [circle.ruby :as ruby])
  (:use midje.sweet)
  (:use [circle.airbrake :only (airbrake)])
  (:require [circle.env :as env])
  (:require [fs]))

; TODO: find another home for this
(defn run-build-from-jruby
  [project-name job-name]
  (let [build (config/build-from-name project-name :job-name (keyword job-name))]
    (run/run-build build)))


;;; Workers. Handles starting workers, checking if they're done, and getting the
;;; result.
(defn call-clojure-from-ruby [f args]
  (ruby/->ruby (apply f args)))

(def worker-store (ref {}))

(defmacro log-future
  "Takes a function to be called in a future. Starts the future and logs the result at the end"
  [& body]
  `(future
     (try
       (let [result# (do ~@body)]
         (infof "%s returned %s" (quote  ~@body) result#)
         result#)
       (catch Exception e#
         (error e# "%s threw" (quote ~@body))
         (airbrake {:body (quote ~@body) :future true})
         (throw e#)))))

(fact "log-future returns futures"
  ;; A normal future will return a future object here, even if the
  ;; code obviously throws an exception. Make sure that's the case
  ;; here as well.
  (log-future (/ 1 0)) => future?)

(fact "log-future works"
  @(log-future (apply + [1 1])) => 2)

(defn start-worker [f & args]
  "Call fn with args as a worker. Returns the id of the worker"
  (let [fut (log-future
             (call-clojure-from-ruby f args))]
{'fraction_non_alphanumeric': 0.15061096902529128, 'fraction_numerical': 0.0076726342710997444, 'mean_word_length': 3.334975369458128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5487689', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1185, 'n_words': 407}