daemon

package
v0.6.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Rendered for darwin/amd64

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

func (a *Activity) End() error

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

func (a *Activity) Reason() string

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) ID

func (a *Assertion) ID() uint32

ID returns the IOKit IOPMAssertionID assigned to this assertion.

func (*Assertion) Kind

func (a *Assertion) Kind() SleepAssertionKind

Kind returns the SleepAssertionKind requested when acquiring the assertion.

func (*Assertion) Name

func (a *Assertion) Name() string

Name returns the human-readable assertion name.

func (*Assertion) Release

func (a *Assertion) Release() error

Release releases the IOKit power management assertion. Release is safe to call multiple times or concurrently.

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

func AcquireLatencyCritical(reason string) (*Guard, error)

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

func (g *Guard) Activity() *Activity

Activity returns the underlying NSProcessInfo Activity, or nil if g is nil.

func (*Guard) Assertion

func (g *Guard) Assertion() *Assertion

Assertion returns the underlying IOKit sleep Assertion, or nil if g is nil.

func (*Guard) AssertionID

func (g *Guard) AssertionID() uint32

AssertionID returns the IOKit IOPMAssertionID held by the guard, or 0 if g is nil.

func (*Guard) Close

func (g *Guard) Close() error

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

func (g *Guard) CloseOnSignal(sig ...os.Signal) (stop func())

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

func AgentStatus(plistName string) Status

AgentStatus returns the SMAppService registration status for a LaunchAgent property list name under Contents/Library/LaunchAgents.

func DaemonStatus

func DaemonStatus(plistName string) Status

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

func (Status) String

func (s Status) String() string

String returns a lowercase name for the service registration status.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL