1+ -- This script demonstrates how to use pg_timetable to interact with etcd.
2+ -- It includes tasks to write keys to etcd, read them back, and parse the results
3+ -- into a structured format. The difference from the Etcd.sql example is that
4+ -- it runs etcdctl commands locally. That allows to run pg_timetable on a remote
5+ -- server without etcd installed on a PostgreSQL server.
6+
7+ -- Setup: A table to store the keys and values from etcd
8+ CREATE TABLE IF NOT EXISTS etcd_test_data (
9+ key TEXT ,
10+ value TEXT
11+ );
12+ TRUNCATE public .etcd_test_data ;
13+
14+ -- An enhanced example consisting of three tasks:
15+ -- 1. Write key-value pairs to etcd using CopyToProgram BUILT-IN task
16+ -- 2. Read all keys under a specific prefix from etcd using CopyFromProgram BUILT-IN task
17+ -- 3. Delete all keys under the prefix using a single parameterized task
18+ DO $CHAIN$
19+ DECLARE
20+ v_task_id bigint ;
21+ v_chain_id bigint ;
22+ BEGIN
23+ -- Create the chain with default values executed every minute (NULL == '* * * * *' :: timetable.cron)
24+ INSERT INTO timetable .chain (chain_name, live, self_destruct)
25+ VALUES (' Sync etcd with PostgreSQL' , TRUE, TRUE)
26+ RETURNING chain_id INTO v_chain_id;
27+
28+ -- Step 1. Write key-value pairs to etcd
29+ -- Create the task to write keys to etcd
30+ INSERT INTO timetable .task (chain_id, task_order, kind, command, ignore_error)
31+ VALUES (v_chain_id, 1 , ' BUILTIN' , ' CopyToProgram' , FALSE)
32+ RETURNING task_id INTO v_task_id;
33+
34+ -- Create the parameters for the task
35+ INSERT INTO timetable .parameter (task_id, order_id, value)
36+ VALUES (v_task_id, 1 , jsonb_build_object(
37+ ' sql' , $$COPY (
38+ SELECT encode(' timetable/' || name::bytea , ' base64' ), encode(setting::bytea , ' base64' ) FROM pg_settings
39+ ) TO STDOUT$$,
40+ ' cmd' , ' sh' ,
41+ ' args' , jsonb_build_array(
42+ ' -c' ,
43+ $$while IFS= $' \t ' read key_b64 value_b64; do
44+ [ - z " $key_b64" ] && echo " Skipping empty key" && continue
45+ [ - z " $value_b64" ] && echo " Skipping empty value" && continue
46+ key= $(printf ' %s' " $key_b64" | base64 - di)
47+ value= $(printf ' %s' " $value_b64" | base64 - di)
48+ etcdctl put " $key" " $value"
49+ done$$)
50+ )
51+ );
52+
53+
54+ -- Step 2. Read all keys under the prefix from etcd
55+ -- Create the task to read keys from etcd
56+ INSERT INTO timetable .task (chain_id, task_order, kind, command, ignore_error)
57+ VALUES (v_chain_id, 2 , ' BUILTIN' , ' CopyFromProgram' , FALSE)
58+ RETURNING task_id INTO v_task_id;
59+
60+ -- Create the parameters for the task
61+ INSERT INTO timetable .parameter (task_id, order_id, value)
62+ VALUES (v_task_id, 1 , jsonb_build_object(
63+ ' sql' , ' COPY etcd_test_data FROM STDIN' ,
64+ ' cmd' , ' sh' ,
65+ ' args' , jsonb_build_array(
66+ ' -c' ,
67+ $$etcdctl get -- prefix timetable/ | awk 'NR%2==1{key=$0} NR%2==0{print key "\t" $0}'$$
68+ )
69+ )
70+ );
71+
72+ -- Step 3. Delete all keys under the prefix from etcd
73+ -- Create the task to delete keys from etcd
74+ INSERT INTO timetable .task (chain_id, task_order, kind, command, ignore_error)
75+ VALUES (v_chain_id, 3 , ' PROGRAM' , ' etcdctl' , FALSE)
76+ RETURNING task_id INTO v_task_id;
77+
78+ -- Create the parameters for the task
79+ INSERT INTO timetable .parameter (task_id, order_id, value)
80+ VALUES (v_task_id, 1 , jsonb_build_array(' del' , ' --prefix' , ' timetable/' ));
81+
82+ END; $CHAIN$
0 commit comments