Implement graceful shutdown of agent.DataAccess (#1008)

* Implements graceful shutdown of agent.DataAccess and underlying Datastore/Logstore/MessageQueue

* adds tests for closing agent.DataAccess and Datastore
This commit is contained in:
Gerardo Viedma
2018-05-21 11:28:21 +01:00
committed by GitHub
parent 105d9b8f1d
commit ea1f94253f
19 changed files with 180 additions and 12 deletions

View File

@@ -116,3 +116,8 @@ func (m *metricds) GetLog(ctx context.Context, appName, callID string) (io.Reade
// instant & no context ;)
func (m *metricds) GetDatabase() *sqlx.DB { return m.ds.GetDatabase() }
// Close calls Close on the underlying Datastore
func (m *metricds) Close() error {
return m.ds.Close()
}

View File

@@ -198,3 +198,7 @@ func (m *mock) batchDeleteRoutes(ctx context.Context, appID string) error {
func (m *mock) GetDatabase() *sqlx.DB {
return nil
}
func (m *mock) Close() error {
return nil
}

View File

@@ -910,3 +910,8 @@ func buildFilterCallQuery(filter *models.CallFilter) (string, []interface{}) {
func (ds *sqlStore) GetDatabase() *sqlx.DB {
return ds.db
}
// Close closes the database, releasing any open resources.
func (ds *sqlStore) Close() error {
return ds.db.Close()
}

View File

@@ -127,4 +127,23 @@ func TestDatastore(t *testing.T) {
both(u)
}
}
func TestClose(t *testing.T) {
ctx := context.Background()
defer os.RemoveAll("sqlite_test_dir")
u, err := url.Parse("sqlite3://sqlite_test_dir")
if err != nil {
t.Fatal(err)
}
os.RemoveAll("sqlite_test_dir")
ds, err := newDS(ctx, u)
if err != nil {
t.Fatal(err)
}
if err := ds.Close(); err != nil {
t.Fatalf("Failed to close datastore: %v", err)
}
}