@@ -8,12 +8,18 @@ package clone
88import (
99 "encoding/json"
1010 "fmt"
11+ "os"
1112
1213 "github.com/urfave/cli/v2"
1314
1415 "gitlab.com/postgres-ai/database-lab/cmd/cli/commands"
1516 "gitlab.com/postgres-ai/database-lab/pkg/client/dblabapi/types"
1617 "gitlab.com/postgres-ai/database-lab/pkg/models"
18+ "gitlab.com/postgres-ai/database-lab/pkg/observer"
19+ )
20+
21+ const (
22+ errorExitStatus = 1
1723)
1824
1925// list runs a request to list clones of an instance.
@@ -190,3 +196,56 @@ func destroy() func(*cli.Context) error {
190196 return err
191197 }
192198}
199+
200+ // observe runs a request to observe clone.
201+ func observe () func (* cli.Context ) error {
202+ return func (cliCtx * cli.Context ) error {
203+ dblabClient , err := commands .ClientByCLIContext (cliCtx )
204+ if err != nil {
205+ return err
206+ }
207+
208+ cloneID := cliCtx .Args ().First ()
209+
210+ clone , err := dblabClient .GetClone (cliCtx .Context , cloneID )
211+ if err != nil {
212+ return err
213+ }
214+
215+ obsConfig := observer.Config {
216+ Follow : cliCtx .Bool ("follow" ),
217+ IntervalSeconds : cliCtx .Uint64 ("interval-seconds" ),
218+ MaxLockDurationSeconds : cliCtx .Uint64 ("max-lock-duration-seconds" ),
219+ MaxDurationSeconds : cliCtx .Uint64 ("max-duration-seconds" ),
220+ SSLMode : cliCtx .String ("sslmode" ),
221+ }
222+
223+ obs := observer .NewObserver (obsConfig , cliCtx .App .Writer )
224+
225+ clone .DB .Password = cliCtx .String ("password" )
226+
227+ return obs .Start (clone )
228+ }
229+ }
230+
231+ // observeSummary shows observing summary and check satisfaction of performance requirements.
232+ func observeSummary () func (* cli.Context ) error {
233+ return func (cliCtx * cli.Context ) error {
234+ obs := observer .NewObserver (observer.Config {}, cliCtx .App .Writer )
235+
236+ if err := obs .LoadObserverState (); err != nil {
237+ return err
238+ }
239+
240+ if err := obs .PrintSummary (); err != nil {
241+ return err
242+ }
243+
244+ if err := obs .CheckPerformanceRequirements (); err != nil {
245+ // Exit with error status without printing additional error logs.
246+ os .Exit (errorExitStatus )
247+ }
248+
249+ return nil
250+ }
251+ }
0 commit comments