File: interrupt_handler.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 4,112 kB
  • sloc: javascript: 59; sh: 14; makefile: 7
file content (177 lines) | stat: -rw-r--r-- 4,137 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package interrupt_handler

import (
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"

	"github.com/onsi/ginkgo/v2/internal/parallel_support"
)

var ABORT_POLLING_INTERVAL = 500 * time.Millisecond

type InterruptCause uint

const (
	InterruptCauseInvalid InterruptCause = iota
	InterruptCauseSignal
	InterruptCauseAbortByOtherProcess
)

type InterruptLevel uint

const (
	InterruptLevelUninterrupted InterruptLevel = iota
	InterruptLevelCleanupAndReport
	InterruptLevelReportOnly
	InterruptLevelBailOut
)

func (ic InterruptCause) String() string {
	switch ic {
	case InterruptCauseSignal:
		return "Interrupted by User"
	case InterruptCauseAbortByOtherProcess:
		return "Interrupted by Other Ginkgo Process"
	}
	return "INVALID_INTERRUPT_CAUSE"
}

type InterruptStatus struct {
	Channel chan interface{}
	Level   InterruptLevel
	Cause   InterruptCause
}

func (s InterruptStatus) Interrupted() bool {
	return s.Level != InterruptLevelUninterrupted
}

func (s InterruptStatus) Message() string {
	return s.Cause.String()
}

func (s InterruptStatus) ShouldIncludeProgressReport() bool {
	return s.Cause != InterruptCauseAbortByOtherProcess
}

type InterruptHandlerInterface interface {
	Status() InterruptStatus
}

type InterruptHandler struct {
	c                 chan interface{}
	lock              *sync.Mutex
	level             InterruptLevel
	cause             InterruptCause
	client            parallel_support.Client
	stop              chan interface{}
	signals           []os.Signal
	requestAbortCheck chan interface{}
}

func NewInterruptHandler(client parallel_support.Client, signals ...os.Signal) *InterruptHandler {
	if len(signals) == 0 {
		signals = []os.Signal{os.Interrupt, syscall.SIGTERM}
	}
	handler := &InterruptHandler{
		c:                 make(chan interface{}),
		lock:              &sync.Mutex{},
		stop:              make(chan interface{}),
		requestAbortCheck: make(chan interface{}),
		client:            client,
		signals:           signals,
	}
	handler.registerForInterrupts()
	return handler
}

func (handler *InterruptHandler) Stop() {
	close(handler.stop)
}

func (handler *InterruptHandler) registerForInterrupts() {
	// os signal handling
	signalChannel := make(chan os.Signal, 1)
	signal.Notify(signalChannel, handler.signals...)

	// cross-process abort handling
	var abortChannel chan interface{}
	if handler.client != nil {
		abortChannel = make(chan interface{})
		go func() {
			pollTicker := time.NewTicker(ABORT_POLLING_INTERVAL)
			for {
				select {
				case <-pollTicker.C:
					if handler.client.ShouldAbort() {
						close(abortChannel)
						pollTicker.Stop()
						return
					}
				case <-handler.requestAbortCheck:
					if handler.client.ShouldAbort() {
						close(abortChannel)
						pollTicker.Stop()
						return
					}
				case <-handler.stop:
					pollTicker.Stop()
					return
				}
			}
		}()
	}

	go func(abortChannel chan interface{}) {
		var interruptCause InterruptCause
		for {
			select {
			case <-signalChannel:
				interruptCause = InterruptCauseSignal
			case <-abortChannel:
				interruptCause = InterruptCauseAbortByOtherProcess
			case <-handler.stop:
				signal.Stop(signalChannel)
				return
			}
			abortChannel = nil

			handler.lock.Lock()
			oldLevel := handler.level
			handler.cause = interruptCause
			if handler.level == InterruptLevelUninterrupted {
				handler.level = InterruptLevelCleanupAndReport
			} else if handler.level == InterruptLevelCleanupAndReport {
				handler.level = InterruptLevelReportOnly
			} else if handler.level == InterruptLevelReportOnly {
				handler.level = InterruptLevelBailOut
			}
			if handler.level != oldLevel {
				close(handler.c)
				handler.c = make(chan interface{})
			}
			handler.lock.Unlock()
		}
	}(abortChannel)
}

func (handler *InterruptHandler) Status() InterruptStatus {
	handler.lock.Lock()
	status := InterruptStatus{
		Level:   handler.level,
		Channel: handler.c,
		Cause:   handler.cause,
	}
	handler.lock.Unlock()

	if handler.client != nil && handler.client.ShouldAbort() && !status.Interrupted() {
		close(handler.requestAbortCheck)
		<-status.Channel
		return handler.Status()
	}

	return status
}