• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

topfreegames / pitaya / 5071265607

26 May 2023 11:31PM UTC coverage: 62.204% (+0.07%) from 62.131%
5071265607

push

github

web-flow
Add handshake validators

Add handshake validators

94 of 94 new or added lines in 4 files covered. (100.0%)

4776 of 7678 relevant lines covered (62.2%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.91
/modules/binary.go
1
// Copyright (c) TFG Co. All Rights Reserved.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a copy
4
// of this software and associated documentation files (the "Software"), to deal
5
// in the Software without restriction, including without limitation the rights
6
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
// copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in all
11
// copies or substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
// SOFTWARE.
20

21
package modules
22

23
import (
24
        "bufio"
25
        "os/exec"
26
        "syscall"
27
        "time"
28

29
        "github.com/topfreegames/pitaya/v2/constants"
30
        "github.com/topfreegames/pitaya/v2/logger"
31
)
32

33
// Binary is a pitaya module that starts a binary as a child process and
34
// pipes its stdout
35
type Binary struct {
36
        Base
37
        binPath                  string
38
        args                     []string
39
        gracefulShutdownInterval time.Duration
40
        cmd                      *exec.Cmd
41
        exitCh                   chan struct{}
42
}
43

44
// NewBinary creates a new binary module with the given path
45
func NewBinary(binPath string, args []string, gracefulShutdownInterval ...time.Duration) *Binary {
1✔
46
        gracefulTime := 15 * time.Second
1✔
47
        if len(gracefulShutdownInterval) > 0 {
2✔
48
                gracefulTime = gracefulShutdownInterval[0]
1✔
49
        }
1✔
50
        return &Binary{
1✔
51
                binPath:                  binPath,
1✔
52
                args:                     args,
1✔
53
                gracefulShutdownInterval: gracefulTime,
1✔
54
                exitCh:                   make(chan struct{}),
1✔
55
        }
1✔
56
}
57

58
// GetExitChannel gets a channel that is closed when the binary dies
59
func (b *Binary) GetExitChannel() chan struct{} {
×
60
        return b.exitCh
×
61
}
×
62

63
// Init initializes the binary
64
func (b *Binary) Init() error {
1✔
65
        b.cmd = exec.Command(b.binPath, b.args...)
1✔
66
        stdout, _ := b.cmd.StdoutPipe()
1✔
67
        stdOutScanner := bufio.NewScanner(stdout)
1✔
68
        stderr, _ := b.cmd.StderrPipe()
1✔
69
        stdErrScanner := bufio.NewScanner(stderr)
1✔
70
        go func() {
2✔
71
                for stdOutScanner.Scan() {
1✔
72
                        logger.Log.Info(stdOutScanner.Text())
×
73
                }
×
74
        }()
75
        go func() {
2✔
76
                for stdErrScanner.Scan() {
1✔
77
                        logger.Log.Error(stdErrScanner.Text())
×
78
                }
×
79
        }()
80
        err := b.cmd.Start()
1✔
81
        go func() {
2✔
82
                b.cmd.Wait()
1✔
83
                close(b.exitCh)
1✔
84
        }()
1✔
85
        return err
1✔
86
}
87

88
// Shutdown shutdowns the binary module
89
func (b *Binary) Shutdown() error {
1✔
90
        err := b.cmd.Process.Signal(syscall.SIGTERM)
1✔
91
        if err != nil {
1✔
92
                return err
×
93
        }
×
94
        timeout := time.After(b.gracefulShutdownInterval)
1✔
95
        select {
1✔
96
        case <-b.exitCh:
1✔
97
                return nil
1✔
98
        case <-timeout:
×
99
                b.cmd.Process.Kill()
×
100
                return constants.ErrTimeoutTerminatingBinaryModule
×
101
        }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc