server

package module
v2.13.7 Latest Latest
Warning

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

Go to latest
Published: May 18, 2022 License: MIT Imports: 17 Imported by: 1

README

PHP Client

Roadrunner Worker

Configuration

# Application server settings (docs: https://roadrunner.dev/docs/php-worker)
server:
  on_init:
    # Command to execute before the main server's command
    #
    # This option is required if using on_init
    command: "any php or script here"
    
    # Script execute timeout
    #
    # Default: 60s [60m, 60h], if used w/o units its means - NANOSECONDS.
    exec_timeout: 20s
    
    # Environment variables for the worker processes.
    #
    # Default: <empty map>
    env:
      - SOME_KEY: "SOME_VALUE"
      - SOME_KEY2: "SOME_VALUE2" 

  # Worker starting command, with any required arguments.
  #
  # This option is required.
  command: "php psr-worker.php"

  # Username (not UID) for the worker processes. An empty value means to use the RR process user.
  #
  # Default: ""
  user: ""

  # Group name (not GID) for the worker processes. An empty value means to use the RR process user.
  #
  # Default: ""
  group: ""

  # Environment variables for the worker processes.
  #
  # Default: <empty map>
  env:
    - SOME_KEY: "SOME_VALUE"
    - SOME_KEY2: "SOME_VALUE2"

  # Worker relay can be: "pipes", TCP (eg.: tcp://127.0.0.1:6002), or socket (eg.: unix:///var/run/rr.sock).
  #
  # Default: "pipes"
  relay: pipes

  # Timeout for relay connection establishing (only for socket and TCP port relay).
  #
  # Default: 60s
  relay_timeout: 60s

Minimal dependencies:

  1. Logger plugin to show log messages.
  2. Config plugin to read and populate plugin's configuration.

Worker sample:

<?php

require __DIR__ . '/vendor/autoload.php';

// Create a new Worker from global environment
$worker = \Spiral\RoadRunner\Worker::create();

while ($data = $worker->waitPayload()) {
    // Received Payload
    var_dump($data);

    // Respond Answer
    $worker->respond(new \Spiral\RoadRunner\Payload('DONE'));
}

Tips:

  1. If your application uses mostly IO (disk, network, etc), you can allocate as much worker as you have memory for the application. Workers are cheap. Hello-world worker consumes no more than ~26Mb of RSS memory.
  2. For the CPU bound operation, see a CPU avg. load and choose the number of workers to consume 90-95% CPU. Leave few percents for the GO's GC (not necessary btw).
  3. If you have ~const workers latency, you may calculate the number of workers needed to handle target load.

Documentation

Index

Constants

View Source
const (
	// PluginName for the server
	PluginName string = "server"

	// RPCPluginName is the name of the RPC plugin, should be in sync with rpc/config.go
	RPCPluginName string = "rpc"

	// RrRelay env variable key (internal)
	RrRelay string = "RR_RELAY"

	// RrRPC env variable key (internal) if the RPC presents
	RrRPC string = "RR_RPC"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// OnInit configuration
	OnInit *OnInitConfig `mapstructure:"on_init"`

	// Command to run as application.
	Command string `mapstructure:"command"`

	// User to run application under.
	User string `mapstructure:"user"`

	// Group to run application under.
	Group string `mapstructure:"group"`

	// Env represents application environment.
	Env map[string]string `mapstructure:"env"`

	// Relay defines connection method and factory to be used to connect to workers:
	// "pipes", "tcp://:6001", "unix://rr.sock"
	// This config section must not change on re-configuration.
	Relay string `mapstructure:"relay"`

	// RelayTimeout defines for how long socket factory will be waiting for worker connection. This config section
	// must not change on re-configuration. Defaults to 60s.
	RelayTimeout time.Duration `mapstructure:"relay_timeout"`
}

Config All config (.rr.yaml) For other section use pointer to distinguish between `empty` and `not present`

func (*Config) InitDefaults

func (cfg *Config) InitDefaults() error

InitDefaults for the server config

type OnInitConfig

type OnInitConfig struct {
	// Command which is started before worker starts
	Command string `mapstructure:"command"`

	// ExecTimeout is execute timeout for the command
	ExecTimeout time.Duration `mapstructure:"exec_timeout"`

	// Env represents application environment.
	Env map[string]string `mapstructure:"env"`
}

type Plugin

type Plugin struct {
	// contains filtered or unexported fields
}

Plugin manages worker

func (*Plugin) CmdFactory

func (p *Plugin) CmdFactory(env map[string]string) func() *exec.Cmd

CmdFactory provides worker command factory associated with given context

func (*Plugin) Init

func (p *Plugin) Init(cfg config.Configurer, log *zap.Logger) error

Init application provider.

func (*Plugin) Name

func (p *Plugin) Name() string

Name contains service name.

func (*Plugin) NewWorker

func (p *Plugin) NewWorker(ctx context.Context, env map[string]string) (worker.BaseProcess, error)

NewWorker issues new standalone worker.

func (*Plugin) NewWorkerPool

func (p *Plugin) NewWorkerPool(ctx context.Context, cfg interface{}, env map[string]string, _ *zap.Logger) (pool.Pool, error)

NewWorkerPool issues new worker pool.

func (*Plugin) Serve

func (p *Plugin) Serve() chan error

Serve (Start) server plugin (just a mock here to satisfy interface)

func (*Plugin) Stop

func (p *Plugin) Stop() error

Stop used to close chosen in config factory

func (*Plugin) Write

func (p *Plugin) Write(data []byte) (int, error)

type RPCConfig

type RPCConfig struct {
	Listen string `mapstructure:"listen"`
}

RPCConfig should be in sync with rpc/config.go Used to set RPC address env

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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