Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Commit 79a2ac6

Browse files
committed
V0.6.0 --single-sync flag
1 parent 050de7a commit 79a2ac6

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ Often you don't want to open up another port just for OAuth of a small sync tool
8787
To work around this you can run `java -jar googlesheets-sql-sync.jar --auth-only` on your local machine, then copy the generated `googlesheets_sql_sync.auth.json` file to your server and on the server run `java -jar googlesheets-sql-sync.jar --no-server`
8888

8989

90+
### Single Sync
91+
92+
If you don't want to keep the syncing your data in an interval but want to sync only once,
93+
you can do so by passing the `single-sync` like this: `java -jar googlesheets-sql-sync.jar --single-sync`
94+
95+
96+
97+
## Metrics
98+
99+
By default a simple Prometheus counter `googlesheets_sql_sync_count` is provided at `/metrics`.
100+
101+
90102
### Customization
91103

92104
The program can be configured using command line flags. To see available options, run:

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject googlesheets-sql-sync "0.5.2"
1+
(defproject googlesheets-sql-sync "0.6.0"
22
:description "Keep your SQL database in sync with Google Sheets"
33
:url "https://github.com/jorinvo/googlesheets-sql-sync"
44
:license {:name "MIT"

src/googlesheets_sql_sync/core.clj

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@
88
[googlesheets-sql-sync.metrics :as metrics]
99
[googlesheets-sql-sync.worker :as worker]))
1010

11+
(defn stop
12+
"Stops system. If system is not running, nothing happens."
13+
[{:keys [timeout> work> stop-server]}]
14+
(log/info "\nShutting down")
15+
(when stop-server
16+
(stop-server))
17+
(when timeout>
18+
(close! timeout>))
19+
(when work>
20+
(close! work>)))
21+
1122
(defn start
1223
"Build a system from options, start it and return it."
13-
[{:as options :keys [api-rate-limit auth-only no-metrics no-server]}]
24+
[{:as options :keys [api-rate-limit auth-only no-metrics no-server single-sync]}]
1425
(try
1526
(let [ctx (assoc options
1627
:work> (chan)
@@ -23,32 +34,30 @@
2334
(<! c))
2435
(stop-server))))
2536
(cond-> ctx
26-
true
27-
(assoc :timeout>
28-
(interval/create-timeout> (:work> ctx) [:sync]))
37+
(not single-sync)
38+
(assoc :timeout>
39+
(interval/create-timeout> (:work> ctx) [:sync]))
40+
41+
single-sync
42+
(assoc :timeout>
43+
(chan))
2944

30-
(not (or no-server no-metrics))
31-
(metrics/init)
45+
(not (or no-server no-metrics))
46+
(metrics/init)
3247

33-
(not no-server)
34-
(#(assoc % :stop-server (web/start %)))
48+
(not no-server)
49+
(#(assoc % :stop-server (web/start %)))
3550

36-
true
37-
(#(assoc % :worker> (worker/start %))))))
51+
true
52+
(#(assoc % :worker> (worker/start %)))
53+
54+
single-sync
55+
(#(do (go (<! (:timeout> %))
56+
(stop %))
57+
%)))))
3858
(catch Exception e (do (log/error "Error while starting:" (.getMessage e))
3959
:not-ok))))
4060

41-
(defn stop
42-
"Stops system. If system is not running, nothing happens."
43-
[{:keys [timeout> work> stop-server]}]
44-
(log/info "\nShutting down")
45-
(when stop-server
46-
(stop-server))
47-
(when timeout>
48-
(close! timeout>))
49-
(when work>
50-
(close! work>)))
51-
5261
(defn trigger-sync
5362
"Tell system to sync, but doesn't wait for sync to complete."
5463
[{:keys [work>]}]

src/googlesheets_sql_sync/options.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
:default "https://www.googleapis.com/oauth2/v4/token"
2727
:validate [valid-url? "Must be a valid URL"]}
2828
:auth-only {:desc "Setup authentication, then quit, don't sync"}
29+
:single-sync {:desc "Sync once only, then quit"}
2930
:no-server {:desc "Disable server, disables authentication and metrics"}
3031
:no-metrics {:desc "Disable metrics"}])
3132

src/googlesheets_sql_sync/worker.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
(defn- do-sync
2323
"Authenticate, fetch data and update DB"
24-
[{:keys [config-file no-server timeout> throttler user-oauth-url] :as ctx}]
24+
[{:as ctx :keys [config-file no-server timeout> throttler user-oauth-url single-sync]}]
2525
(try
2626
(let [cfg (config/get config-file)]
2727
(try
@@ -38,7 +38,8 @@
3838
:not-ok)
3939
(show-init-message (:google_credentials cfg) user-oauth-url)))
4040
(catch Exception e (log/error (.getMessage e) "\nSync failed")))
41-
(log/info "Next sync in" (interval/->string (:interval cfg)))
41+
(when-not single-sync
42+
(log/info "Next sync in" (interval/->string (:interval cfg))))
4243
(async/put! timeout> (:interval cfg)))
4344
(catch Exception e (do (log/error "Failed reading config file" (.getMessage e))
4445
:not-ok))))

0 commit comments

Comments
 (0)