Documentation
¶
Overview ¶
Package daemon prevents macOS App Nap, timer coalescing, and idle system sleep for long-running background services, training jobs, and mesh workers.
It wraps three macOS facilities using purego with zero cgo:
- Foundation NSProcessInfo activities (BeginActivity, Activity) to opt out of App Nap, timer coalescing, idle sleep, and automatic termination.
- IOKit power management assertions (AcquireSleepAssertion, Assertion) via IOPMAssertionCreateWithName and IOPMAssertionRelease to register a visible power assertion in pmset.
- ServiceManagement SMAppService status queries (ServiceStatus, AgentStatus, DaemonStatus) to inspect LaunchAgent/LaunchDaemon registration state.
Most callers should use AcquireLatencyCritical to hold both an NSProcessInfo latency-critical activity and an IOKit sleep assertion under a single Guard:
g, err := daemon.AcquireLatencyCritical("inference server loop")
if err != nil {
log.Fatal(err)
}
defer g.Close()
fmt.Printf("assertion id=%d service=%s\n", g.AssertionID(), daemon.ServiceStatus())
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/apple/x/daemon"
)
func main() {
g, err := daemon.AcquireLatencyCritical("example background worker")
if err != nil {
log.Fatal(err)
}
defer g.Close()
fmt.Printf("assertion active: %v\n", g.AssertionID() != 0)
}
Output: assertion active: true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Activity ¶
type Activity struct {
// contains filtered or unexported fields
}
Activity represents an active NSProcessInfo activity token. Call Activity.End when the protected work finishes. A zero Activity is not usable; construct one with BeginActivity.
func BeginActivity ¶
func BeginActivity(opts ActivityOptions, reason string) (*Activity, error)
BeginActivity starts an NSProcessInfo activity with the given options and human-readable reason. The returned Activity holds a retained Objective-C token until Activity.End is called.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/apple/x/daemon"
)
func main() {
act, err := daemon.BeginActivity(
daemon.UserInitiated|daemon.LatencyCritical|daemon.IdleSystemSleepDisabled,
"example inference loop",
)
if err != nil {
log.Fatal(err)
}
fmt.Println(act.Reason())
if err := act.End(); err != nil {
log.Fatal(err)
}
}
Output: example inference loop
func (*Activity) End ¶
End ends the NSProcessInfo activity and releases its underlying token. End is safe to call multiple times or concurrently.
func (*Activity) Options ¶
func (a *Activity) Options() ActivityOptions
Options returns the options used to begin the activity.
func (*Activity) Reason ¶
Reason returns the reason string passed to BeginActivity.
type ActivityOptions ¶
type ActivityOptions uint64
ActivityOptions is a bitmask of options passed to BeginActivity. Values correspond to Foundation's NSActivityOptions.
const ( // UserInitiated indicates the process is performing a user-requested // action and should not be throttled by App Nap. UserInitiated ActivityOptions = ActivityOptions(foundation.NSActivityUserInitiated) // LatencyCritical indicates the activity requires the highest timer // and I/O precision available, disabling timer coalescing. LatencyCritical ActivityOptions = ActivityOptions(foundation.NSActivityLatencyCritical) // IdleSystemSleepDisabled prevents the system from entering idle sleep // while the activity is active. IdleSystemSleepDisabled ActivityOptions = ActivityOptions(foundation.NSActivityIdleSystemSleepDisabled) // AutomaticTerminationDisabled prevents macOS automatic termination // while the activity is active. AutomaticTerminationDisabled ActivityOptions = ActivityOptions(foundation.NSActivityAutomaticTerminationDisabled) )
type Assertion ¶
type Assertion struct {
// contains filtered or unexported fields
}
Assertion represents an active IOKit power management assertion created with IOPMAssertionCreateWithName. Call Assertion.Release when the assertion is no longer needed. A zero Assertion is not usable; construct one with AcquireSleepAssertion.
func AcquireSleepAssertion ¶
func AcquireSleepAssertion(kind SleepAssertionKind, name string) (*Assertion, error)
AcquireSleepAssertion creates an active IOKit sleep assertion of the given kind and human-readable name. The assertion appears in "pmset -g assertions" until Assertion.Release is called or the process exits.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/apple/x/daemon"
)
func main() {
assn, err := daemon.AcquireSleepAssertion(daemon.PreventUserIdleSystemSleep, "example training job")
if err != nil {
log.Fatal(err)
}
fmt.Printf("kind=%s active=%v\n", assn.Kind(), assn.ID() != 0)
if err := assn.Release(); err != nil {
log.Fatal(err)
}
}
Output: kind=PreventUserIdleSystemSleep active=true
func (*Assertion) Kind ¶
func (a *Assertion) Kind() SleepAssertionKind
Kind returns the SleepAssertionKind requested when acquiring the assertion.
type Guard ¶
type Guard struct {
// contains filtered or unexported fields
}
Guard holds both an NSProcessInfo Activity and an IOKit sleep Assertion. Call Guard.Close when finished; Close is idempotent and safe to call concurrently from signal handlers and deferred cleanup.
func AcquireLatencyCritical ¶
AcquireLatencyCritical begins a latency-critical NSProcessInfo activity and acquires a PreventUserIdleSystemSleep IOKit assertion under the given reason. If acquiring the assertion fails, the activity is ended before returning the error.
func (*Guard) Activity ¶
Activity returns the underlying NSProcessInfo Activity, or nil if g is nil.
func (*Guard) Assertion ¶
Assertion returns the underlying IOKit sleep Assertion, or nil if g is nil.
func (*Guard) AssertionID ¶
AssertionID returns the IOKit IOPMAssertionID held by the guard, or 0 if g is nil.
func (*Guard) Close ¶
Close ends the NSProcessInfo activity and releases the IOKit power assertion. Close is idempotent and safe to call concurrently from signal handlers and deferred cleanup.
func (*Guard) CloseOnSignal ¶
CloseOnSignal arranges for g.Close to be called when any of the given signals are received. Call the returned stop function to unregister the signal handler and release the listener goroutine.
type SleepAssertionKind ¶
type SleepAssertionKind string
SleepAssertionKind selects the IOKit power management assertion type passed to IOPMAssertionCreateWithName.
const ( // PreventUserIdleSystemSleep prevents the system from sleeping due to // user inactivity while allowing the display to dim or sleep. PreventUserIdleSystemSleep SleepAssertionKind = "PreventUserIdleSystemSleep" // PreventSystemSleep prevents the system from entering sleep. PreventSystemSleep SleepAssertionKind = "PreventSystemSleep" )
type Status ¶
type Status int
Status describes the ServiceManagement registration state of a launch service or login item, corresponding to SMAppService.Status.
const ( // StatusNotRegistered indicates the service has not been registered. StatusNotRegistered Status = Status(sm.SMAppServiceStatusNotRegistered) // StatusEnabled indicates the service is registered and eligible to run. StatusEnabled Status = Status(sm.SMAppServiceStatusEnabled) // StatusRequiresApproval indicates the service is registered but requires // user approval in System Settings. StatusRequiresApproval Status = Status(sm.SMAppServiceStatusRequiresApproval) // StatusNotFound indicates ServiceManagement could not locate the service // (for example, when running as an unbundled CLI binary). StatusNotFound Status = Status(sm.SMAppServiceStatusNotFound) )
func AgentStatus ¶
AgentStatus returns the SMAppService registration status for a LaunchAgent property list name under Contents/Library/LaunchAgents.
func DaemonStatus ¶
DaemonStatus returns the SMAppService registration status for a LaunchDaemon property list name under Contents/Library/LaunchDaemons.
func ServiceStatus ¶
func ServiceStatus() Status
ServiceStatus returns the SMAppService registration status of the current process's main application service. For unbundled CLI binaries, macOS typically reports StatusNotFound.
Example ¶
package main
import (
"fmt"
"github.com/tmc/apple/x/daemon"
)
func main() {
st := daemon.ServiceStatus()
fmt.Println(st)
}
Output: not-found