@@ -16,6 +16,7 @@ const (
16
16
scriptCommandsAttr = "commands"
17
17
scriptTriesAttr = "tries"
18
18
scriptBackoffDelayAttr = "backoff_delay"
19
+ scriptTimeoutAttr = "timeout"
19
20
scriptShasumAttr = "shasum"
20
21
)
21
22
@@ -47,6 +48,12 @@ func resourcePostgreSQLScript() *schema.Resource {
47
48
Default : 1 ,
48
49
Description : "Number of seconds between two tries of the batch of commands" ,
49
50
},
51
+ scriptTimeoutAttr : {
52
+ Type : schema .TypeInt ,
53
+ Optional : true ,
54
+ Default : 5 * 60 ,
55
+ Description : "Number of seconds for a batch of command to timeout" ,
56
+ },
50
57
scriptShasumAttr : {
51
58
Type : schema .TypeString ,
52
59
Computed : true ,
@@ -60,6 +67,7 @@ func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnectio
60
67
commands , err := toStringArray (d .Get (scriptCommandsAttr ).([]any ))
61
68
tries := d .Get (scriptTriesAttr ).(int )
62
69
backoffDelay := d .Get (scriptBackoffDelayAttr ).(int )
70
+ timeout := d .Get (scriptTimeoutAttr ).(int )
63
71
64
72
if err != nil {
65
73
return diag.Diagnostics {diag.Diagnostic {
@@ -71,7 +79,7 @@ func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnectio
71
79
72
80
sum := shasumCommands (commands )
73
81
74
- if err := executeCommands (ctx , db , commands , tries , backoffDelay ); err != nil {
82
+ if err := executeCommands (ctx , db , commands , tries , backoffDelay , timeout ); err != nil {
75
83
return diag.Diagnostics {diag.Diagnostic {
76
84
Severity : diag .Error ,
77
85
Summary : "Commands execution failed" ,
@@ -99,9 +107,9 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er
99
107
return nil
100
108
}
101
109
102
- func executeCommands (ctx context.Context , db * DBConnection , commands []string , tries int , backoffDelay int ) error {
110
+ func executeCommands (ctx context.Context , db * DBConnection , commands []string , tries int , backoffDelay int , timeout int ) error {
103
111
for try := 1 ; ; try ++ {
104
- err := executeBatch (ctx , db , commands )
112
+ err := executeBatch (ctx , db , commands , timeout )
105
113
if err == nil {
106
114
return nil
107
115
} else {
@@ -113,10 +121,12 @@ func executeCommands(ctx context.Context, db *DBConnection, commands []string, t
113
121
}
114
122
}
115
123
116
- func executeBatch (ctx context.Context , db * DBConnection , commands []string ) error {
124
+ func executeBatch (ctx context.Context , db * DBConnection , commands []string , timeout int ) error {
125
+ timeoutContext , timeoutCancel := context .WithTimeout (ctx , time .Duration (timeout )* time .Second )
126
+ defer timeoutCancel ()
117
127
for _ , command := range commands {
118
128
log .Printf ("[DEBUG] Executing %s" , command )
119
- _ , err := db .ExecContext (ctx , command )
129
+ _ , err := db .ExecContext (timeoutContext , command )
120
130
log .Printf ("[DEBUG] Result %s: %v" , command , err )
121
131
if err != nil {
122
132
log .Println ("[DEBUG] Error catched:" , err )
0 commit comments