-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add graphql-server to the cartesi-node binary
- Loading branch information
Showing
5 changed files
with
132 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package services | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
) | ||
|
||
const ( | ||
serviceName = "graphql-server" | ||
binaryName = "cartesi-rollups-graphql-server" | ||
) | ||
|
||
type GraphQLService struct{} | ||
|
||
func (g GraphQLService) Start(ctx context.Context) error { | ||
cmd := exec.Command(binaryName) | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdout = os.Stdout | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
fmt.Printf("%v: %v\n", g.String(), ctx.Err()) | ||
cmd.Process.Signal(syscall.SIGTERM) | ||
}() | ||
|
||
err := cmd.Wait() | ||
if err != nil && cmd.ProcessState.ExitCode() != int(syscall.SIGTERM) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (g GraphQLService) String() string { | ||
return serviceName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
// Contains the several internal services that comprises the cartesi-node and | ||
// the interfaces and functions used to interact with them | ||
package services | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Defines the interface of a service | ||
type Service interface { | ||
fmt.Stringer | ||
|
||
// Start a service that will run until completion or until the context is | ||
// canceled | ||
Start(ctx context.Context) error | ||
} | ||
|
||
const DefaultServiceTimeout = 1 * time.Minute | ||
|
||
// The Run function serves as a very simple supervisor: it will start all the | ||
// services provided to it and will run until the first of them finishes. Next | ||
// it will try to stop the remaining services or timeout if they take too long | ||
func Run(services []Service) { | ||
if len(services) == 0 { | ||
panic("there are no services to run") | ||
} | ||
|
||
// start services | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
exit := make(chan struct{}) | ||
for _, service := range services { | ||
service := service | ||
go func() { | ||
if err := service.Start(ctx); err != nil { | ||
msg := "main: service '%v' exited with error: %v\n" | ||
fmt.Printf(msg, service.String(), err) | ||
} else { | ||
msg := "main: service '%v' exited successfully\n" | ||
fmt.Printf(msg, service.String()) | ||
} | ||
exit <- struct{}{} | ||
}() | ||
} | ||
|
||
// wait for first service to exit | ||
<-exit | ||
|
||
// send stop message to all other services and wait for them to finish | ||
// or timeout | ||
wait := make(chan struct{}) | ||
go func() { | ||
cancel() | ||
for i := 0; i < len(services)-1; i++ { | ||
<-exit | ||
} | ||
wait <- struct{}{} | ||
}() | ||
|
||
select { | ||
case <-wait: | ||
fmt.Println("main: all services exited") | ||
case <-time.After(DefaultServiceTimeout): | ||
fmt.Println("main: exited after timeout") | ||
} | ||
} |