TSultanov commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1163840136


##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+       "beam.apache.org/playground/backend/internal/constants"
+       "beam.apache.org/playground/backend/internal/db/mapper"
+       "beam.apache.org/playground/backend/internal/logger"
+       "context"
+       "fmt"
+       "math/rand"
+       "net"
+       "os"
+       "os/exec"
+       "syscall"
+       "time"
+)
+
+const (
+       emulatorHost    = "127.0.0.1"
+       portRangeStart  = 10000
+       portRangeEnd    = 20000
+       maxAttempts     = 3
+       startupDuration = 500 * time.Millisecond
+       pauseDuration   = 100 * time.Millisecond
+       waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+       *Datastore
+       emulator *emulator
+}
+
+type emulator struct {
+       Host string
+       Port int
+       cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+       clientCloseErr := ed.Datastore.Client.Close()
+       emulatorStopErr := ed.emulator.Stop()
+       if clientCloseErr != nil {
+               return clientCloseErr
+       }
+       if emulatorStopErr != nil {
+               return emulatorStopErr
+       }
+       return nil
+}
+
+func NewEmulated(ctx context.Context) (EmulatedDatastore, error) {
+       emulator, err := startEmulator()
+       if err != nil {
+               return EmulatedDatastore{}, err
+       }
+
+       if _, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
+               err := os.Unsetenv("GOOGLE_CLOUD_PROJECT")
+               if err != nil {
+                       panic(err)
+               }
+       }
+
+       if err := os.Setenv(constants.EmulatorHostKey, emulator.GetEndpoint()); 
err != nil {
+               panic(err)
+       }
+
+       datastoreDb, err := New(ctx, mapper.NewPrecompiledObjectMapper(), 
constants.EmulatorProjectId)
+       if err != nil {
+               return EmulatedDatastore{}, err
+       }
+
+       return EmulatedDatastore{Datastore: datastoreDb, emulator: emulator}, 
nil
+}
+
+func startEmulator() (*emulator, error) {

Review Comment:
   If I understand you correctly, you basically are suggesting to use context 
cancellation to kill the emulator process, right?
   I've tried this approach, but sadly it doesn't work that well as cancelling 
the context only terminates the first process in the child process tree 
(starting the datastore emulator creates at least three processes), and the 
actual emulator process gets reparented by the `init`. The current code works 
around this problem by creating a process group out of all emulator processes 
and sending the SIGTERM to the whole group.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to