- Remove mkdocs files (requirements.txt, CNAME) - Add CLI documentation: build.md, release.md, php.md, run.md - Add configuration.md with full reference - Add examples/ directory with sample configurations: - go-cli-release.yaml - wails-desktop-release.yaml - php-laravel-release.yaml - linuxkit-server.yml - linuxkit-docker-format.yml - full-release.yaml - minimal-release.yaml - official-repos.yaml - Flatten existing framework docs into framework/ - Update index.md as CLI entry point Ready for VitePress integration with core-php/docs. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2 KiB
2 KiB
Service Lifecycle
Core provides lifecycle hooks for services to initialize and clean up resources.
Lifecycle Interfaces
Startable
Called when the application starts:
type Startable interface {
OnStartup(ctx context.Context) error
}
Stoppable
Called when the application shuts down:
type Stoppable interface {
OnShutdown(ctx context.Context) error
}
Implementation Example
type DatabaseService struct {
db *sql.DB
}
func (s *DatabaseService) OnStartup(ctx context.Context) error {
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
return err
}
// Verify connection
if err := db.PingContext(ctx); err != nil {
return err
}
s.db = db
return nil
}
func (s *DatabaseService) OnShutdown(ctx context.Context) error {
if s.db != nil {
return s.db.Close()
}
return nil
}
Lifecycle Order
- Registration: Services registered via
core.New() - Wails Binding: Services bound to Wails app
- Startup:
OnStartup()called for each Startable service - Running: Application runs
- Shutdown:
OnShutdown()called for each Stoppable service
Context Usage
The context passed to lifecycle methods includes:
- Cancellation signal for graceful shutdown
- Deadline for timeout handling
func (s *Service) OnStartup(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-s.initialize():
return nil
}
}
Error Handling
If OnStartup returns an error, the application will fail to start:
func (s *Service) OnStartup(ctx context.Context) error {
if err := s.validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
return nil
}
Best Practices
- Keep startup fast - Defer heavy initialization
- Handle context cancellation - Support graceful shutdown
- Clean up resources - Always implement OnShutdown for services with resources
- Log lifecycle events - Helps with debugging