<commit_before>(ns appengine-magic.services.task-queues
  (:require [appengine-magic.services.datastore :as ds])
  (:import java.util.Date
           [com.google.appengine.api.taskqueue Queue QueueFactory
            TaskOptions$Builder TaskOptions$Method]))


(defonce *default-queue* (atom nil))
(defonce *named-queues* (atom {}))


(defonce *task-http-methods*
  {:post TaskOptions$Method/POST
   :delete TaskOptions$Method/DELETE
   :get TaskOptions$Method/GET
   :head TaskOptions$Method/HEAD
   :put TaskOptions$Method/PUT})


(defn get-task-queue [& {:keys [queue]}]
  (if (nil? queue)
      (do (when (nil? @*default-queue*)
            (reset! *default-queue* (QueueFactory/getDefaultQueue)))
          @*default-queue*)
      (let [q (@*named-queues* queue)]
        (if-not (nil? q)
            q
            ((swap! *named-queues* assoc
                    queue (QueueFactory/getQueue queue))
             queue)))))


(defn add! [& {:keys [queue url join-current-transaction? params headers payload method
                      countdown-ms eta-ms eta]
               :or {join-current-transaction? false
                    params {}
                    headers {}
                    method :post}}]
  (when (or (nil? url) (not (string? url)) (= "" (.trim url)))
    (throw (IllegalArgumentException. "add! requires a :url argument")))
  (when-not (map? params)
    (throw (IllegalArgumentException. "add! :params must be a map")))
  (let [queue-obj (get-task-queue :queue queue)
        opts (TaskOptions$Builder/withUrl url)]
    ;; headers
    (doseq [[header-name header-value] headers]
      (.header opts header-name header-value))
    ;; params
    (doseq [[param-name param-value] params]
      (.param opts param-name (cond
                               (string? param-value) param-value
                               (instance? (class (byte-array 0)) param-value) param-value
                               :else (str param-value))))
    ;; HTTP method for hitting task
    (.method opts (*task-http-methods* method))
    ;; payload
    (cond
     ;; nothing, no problem
     (nil? payload) nil
     ;; just a string
     (string? payload)
     (.payload opts payload)
     ;; string with a charset, or a byte array with a Content-Type
     (and (sequential? payload) (= 2 (count payload)))
     (.payload opts (first payload) (second payload))
     ;; something's wrong
     :else (throw (IllegalArgumentException. "add! :payload invalid")))
    ;; scheduling
    (cond<commit_msg>Task parameters must be strings, not keywords. Thanks to <NAME> for pointing out the bug.<commit_after>(ns appengine-magic.services.task-queues
  (:require [appengine-magic.services.datastore :as ds])
  (:import java.util.Date
           [com.google.appengine.api.taskqueue Queue QueueFactory
            TaskOptions$Builder TaskOptions$Method]))


(defonce *default-queue* (atom nil))
(defonce *named-queues* (atom {}))


(defonce *task-http-methods*
  {:post TaskOptions$Method/POST
   :delete TaskOptions$Method/DELETE
   :get TaskOptions$Method/GET
   :head TaskOptions$Method/HEAD
   :put TaskOptions$Method/PUT})


(defn get-task-queue [& {:keys [queue]}]
  (if (nil? queue)
      (do (when (nil? @*default-queue*)
            (reset! *default-queue* (QueueFactory/getDefaultQueue)))
          @*default-queue*)
      (let [q (@*named-queues* queue)]
        (if-not (nil? q)
            q
            ((swap! *named-queues* assoc
                    queue (QueueFactory/getQueue queue))
             queue)))))


(defn add! [& {:keys [queue url join-current-transaction? params headers payload method
                      countdown-ms eta-ms eta]
               :or {join-current-transaction? false
                    params {}
                    headers {}
                    method :post}}]
  (when (or (nil? url) (not (string? url)) (= "" (.trim url)))
    (throw (IllegalArgumentException. "add! requires a :url argument")))
  (when-not (map? params)
    (throw (IllegalArgumentException. "add! :params must be a map")))
  (let [queue-obj (get-task-queue :queue queue)
        opts (TaskOptions$Builder/withUrl url)]
    ;; headers
    (doseq [[header-name header-value] headers]
      (.header opts header-name header-value))
    ;; params
    (doseq [[param-name param-value] params]
      (.param opts
              (name param-name)
              (cond
               (string? param-value) param-value
               (instance? (class (byte-array 0)) param-value) param-value
               :else (str param-value))))
    ;; HTTP method for hitting task
    (.method opts (*task-http-methods* method))
    ;; payload
    (cond
     ;; nothing, no problem
     (nil? payload) nil
     ;; just a string
     (string? payload)
     (.payload opts payload)
     ;; string with a charset, or a byte array with a Content-Type
     (and (sequential? payload) (= 2 (count payload)))
     (.payload opts (first payload) (second payload))
     ;; something's wrong
     :else (throw (IllegalArgumentException. "add! :payload invalid")))
    ;; scheduling
    (cond
{'fraction_non_alphanumeric': 0.1339759984261263, 'fraction_numerical': 0.0007869368483179224, 'mean_word_length': 2.38031914893617, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '7548514', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1450, 'n_words': 424}