Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for debug mode #122

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/keboola/docker/runtime.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@


(defn log-strings [& strings]
(apply println strings))
(apply println (map
#(if (map? %)
; pretty print maps
(generate-string % {:pretty true})
; else just print
%)
strings)))

(defn log [what]
(println what))
Expand Down
6 changes: 3 additions & 3 deletions src/keboola/facebook/api/exponential_backoff.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns keboola.facebook.api.exponential-backoff
(:require [keboola.docker.runtime :as runtime]))

(def time-slot-ms 100)
(def truncate 5)
(def MAX_WAIT_TIME (* 1000 60 60 10))
(def time-slot-ms 1000)
(def truncate 6) ; sleep for 64 seconds at most (2^6)
(def MAX_WAIT_TIME (* 1000 60 60 24)) ; poll for 24 hours at most
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

este som upravil ex backoff pre pollovanie async jobov, po novom iteruje maximalne po 60 sekundach (doteraz to bolo 0.5 sekundy) a polluje async job maximalne 24 hodin (doteraz to bolo 10 hodin). Slubujem si od toho mensiu zataz na fb api v pripade ze sa dlho polluje job (x hodin). Fb api ma moze uplatnit throtling, tak sa tomu chcem vyhnut.


(defn with-exp-backoff [action!]
(loop [c 0
Expand Down
1 change: 1 addition & 0 deletions src/keboola/facebook/api/request.clj
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
(let [status (-> poll-response :body :async_status)
completed? (= status "Job Completed")
failed? (some (partial = status) ["Job Failed" "Job Skipped"])]
(log-strings "Facebook API async insights job state:" (:body poll-response))
(cond
(not status) (runtime/app-error (str "Polling failed with unknown status" (:body poll-response)))
failed? (runtime/user-error (str "Polling failed with status:" status (:body poll-response)))
Expand Down
5 changes: 5 additions & 0 deletions src/keboola/facebook/extractor/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[keboola.facebook.extractor.query :as query]
[keboola.facebook.extractor.sync-actions :as sync-actions]
[keboola.http.client :refer [fb-requests-count]]
[keboola.http.recording :refer [turn-log-responses-on]]
[keboola.utils.json-to-csv :as csv]
[slingshot.slingshot :refer [throw+ try+]]))

Expand Down Expand Up @@ -64,6 +65,10 @@
app-access-token (docker-config/app-access-token datadir)
out-dir-path (docker-config/out-dir-path datadir)
credentials (docker-config/user-credentials datadir)]
(when (:debug-mode parameters)
(log "Running in debug mode")
(sync-actions/disable-log-token)
(turn-log-responses-on))
(cond
(empty? credentials) (docker-runtime/user-error "Missing facebook credentials")
(empty? (docker-config/get-fb-token credentials)) (docker-runtime/user-error "Missing facebook token"))
Expand Down
25 changes: 24 additions & 1 deletion src/keboola/http/recording.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

(def recording (atom '()))
(def do-recording? (atom false))
(def do-log-responses? (atom false))


(defn reset-recording [] (reset! recording '()))

(defn turn-recording-on [] (reset! do-recording? true))
(defn turn-recording-off [] (reset! do-recording? false))

(defn turn-log-responses-on [] (reset! do-log-responses? true))

(def VALID-CHARS
(map char (concat (range 48 58) ; 0-9
(range 66 91) ; A-Z
Expand All @@ -36,8 +40,27 @@
(defn anonymize-map [m]
(postwalk anonymize-item m))


(defn replace-token-by-regexp [item]
(cond
(string? item)
(clojure.string/replace item #"access_token=[^&]*" "access_token=TOKEN")

(map? item)
(if (contains? item :access_token)
(assoc item :access_token "TOKEN")
item)

:else item))

(defn record-request [response method url request-rest]
#_(println (pr-str request-rest))
(when @do-log-responses?
(let [result-map {:method method
:url url
:response {:status (:status response) :body (:body response)}}
result (postwalk replace-token-by-regexp result-map)]
(println (generate-string result {:pretty true})))
(Thread/sleep 350))
(if @do-recording?
(let [request-base {:method method :address url}
request (merge request-base (apply hash-map request-rest))
Expand Down
Loading