Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit293d310

Browse files
committed
Document the use of agent units
1 parent804bd54 commit293d310

File tree

1 file changed

+11
-89
lines changed

1 file changed

+11
-89
lines changed

‎agent/unit.go‎

Lines changed: 11 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ import (
99
)
1010

1111
// A Unit (named to represent its resemblance to the systemd concept) is a kind of lock that encodes metadata
12-
// about the state of a resource. Units areprimarilymeant to be sections of processes such as coder scripts
12+
// about the state of a resource. Units areprimarily meant to be sections of processes such as coder scripts
1313
// that encapsulate a contended resource, such as a database lock or a socket.
1414
//
15-
// In most cases, `coder_script` resources will create and manage units by invocation of `coder agent lock <unit>`.
16-
// Locks may be acquired with no intention of releasing them as a signal to other scripts that
15+
// In most cases, `coder_script` resources will create and manage units by invocations of
16+
// * `coder agent unit start <unit> [--wants <unit>]`
17+
// * `coder agent unit complete <unit>`
18+
// * `coder agent unit fail <unit>`
19+
// Units may be acquired with no intention of releasing them as a signal to other scripts that
1720
// a contended resource has been provided and is available. For example, a script that installs curl
18-
// might acquire alock called "curl-install" to signal to other scripts that curl has been installed
19-
// and is available. In this case, thelock will bereleased when the agent is stopped.
21+
// might acquire aunit called "curl-install" to signal to other scripts that curl has been installed
22+
// and is available. In this case, theunit will becompleted when the agent is stopped.
2023
typeUnitstruct {
2124
Namestring
2225
history []Event
26+
Wants []*Unit
2327
}
2428

2529
typeEventstruct {
@@ -134,16 +138,11 @@ func (s *memoryUnitCoordinator) acquireUnitInternal(unitName string, ttl *time.D
134138
s.mu.Lock()
135139
defers.mu.Unlock()
136140

137-
// Check iflock is already held and not expired
138-
ifs.hasUnitHeld(unitName)&&!s.isUnitExpired(unitName){
141+
// Check ifunit is already held
142+
ifs.hasUnitHeld(unitName) {
139143
returnfalse
140144
}
141145

142-
// Clean up expired lock if it exists
143-
ifs.hasUnitHeld(unitName)&&s.isUnitExpired(unitName) {
144-
s.expireUnitInternal(unitName)
145-
}
146-
147146
if_,exists:=s.units[unitName];!exists {
148147
s.units[unitName]=Unit{
149148
Name:unitName,
@@ -210,11 +209,6 @@ func (s *memoryUnitCoordinator) IsUnitHeld(unitName string) bool {
210209
returnfalse
211210
}
212211

213-
// Check if unit has expired
214-
ifs.isUnitExpired(unitName) {
215-
returnfalse
216-
}
217-
218212
returntrue
219213
}
220214

@@ -234,85 +228,13 @@ func (s *memoryUnitCoordinator) hasUnitHeld(unitName string) bool {
234228
returnlastEvent.Type==UnitEventTypeAcquired
235229
}
236230

237-
// isLockExpired checks if a lock has expired based on its TTL (must be called with lock held)
238-
func (s*memoryUnitCoordinator)isUnitExpired(unitNamestring)bool {
239-
unit,exists:=s.units[unitName]
240-
if!exists {
241-
returnfalse
242-
}
243-
244-
// No TTL means no expiration
245-
ifunit.ttl==nil||unit.acquiredAt==nil {
246-
returnfalse
247-
}
248-
249-
// Check if TTL has passed
250-
returntime.Since(*unit.acquiredAt)>*unit.ttl
251-
}
252-
253-
// expireLockInternal marks a lock as expired (must be called with write lock held)
254-
func (s*memoryUnitCoordinator)expireUnitInternal(unitNamestring) {
255-
unit,exists:=s.units[unitName]
256-
if!exists {
257-
return
258-
}
259-
260-
// Cancel timer if it exists
261-
ifunit.timer!=nil {
262-
unit.timer.Stop()
263-
unit.timer=nil
264-
}
265-
266-
// Add expiration event to history
267-
unit.history=append(unit.history,Event{
268-
Type:UnitEventTypeExpired,
269-
Timestamp:time.Now(),
270-
})
271-
// Clear TTL and acquiredAt to prevent further expiration checks
272-
unit.ttl=nil
273-
unit.acquiredAt=nil
274-
s.units[unitName]=unit
275-
276-
// Notify listeners of expiration
277-
iflisteners,exists:=s.listeners[unitName];exists {
278-
for_,listener:=rangelisteners {
279-
golistener(context.Background(),UnitEventTypeExpired)
280-
}
281-
}
282-
}
283-
284-
// expireUnitWithTimer is called by the TTL timer to expire a unit
285-
func (s*memoryUnitCoordinator)expireUnitWithTimer(unitNamestring) {
286-
ifatomic.LoadInt32(&s.closed)==1 {
287-
return
288-
}
289-
290-
s.mu.Lock()
291-
defers.mu.Unlock()
292-
293-
// Double-check the unit still exists and hasn't been released
294-
if!s.hasUnitHeld(unitName) {
295-
return
296-
}
297-
298-
// Expire the unit
299-
s.expireUnitInternal(unitName)
300-
}
301-
302231
// Close shuts down the state coordinator
303232
func (s*memoryUnitCoordinator)Close()error {
304233
atomic.StoreInt32(&s.closed,1)
305234

306235
s.mu.Lock()
307236
defers.mu.Unlock()
308237

309-
// Stop all TTL timers before clearing locks
310-
for_,unit:=ranges.units {
311-
ifunit.timer!=nil {
312-
unit.timer.Stop()
313-
}
314-
}
315-
316238
// Clear all listeners and events
317239
s.listeners=make(map[string]map[uint64]Listener)
318240
s.units=make(map[string]Unit)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp