Module: Moleculer

Extended by:
Moleculer
Included in:
Moleculer
Defined in:
lib/moleculer.rb,
lib/moleculer/node.rb,
lib/moleculer/broker.rb,
lib/moleculer/context.rb,
lib/moleculer/packets.rb,
lib/moleculer/version.rb,
lib/moleculer/registry.rb,
lib/moleculer/packets/req.rb,
lib/moleculer/packets/res.rb,
lib/moleculer/serializers.rb,
lib/moleculer/packets/base.rb,
lib/moleculer/packets/info.rb,
lib/moleculer/service/base.rb,
lib/moleculer/transporters.rb,
lib/moleculer/configuration.rb,
lib/moleculer/packets/event.rb,
lib/moleculer/service/event.rb,
lib/moleculer/service/action.rb,
lib/moleculer/service/remote.rb,
lib/moleculer/packets/discover.rb,
lib/moleculer/serializers/json.rb,
lib/moleculer/packets/heartbeat.rb,
lib/moleculer/support/hash_util.rb,
lib/moleculer/support/log_proxy.rb,
lib/moleculer/transporters/base.rb,
lib/moleculer/transporters/fake.rb,
lib/moleculer/packets/disconnect.rb,
lib/moleculer/transporters/redis.rb,
lib/moleculer/support/open_struct.rb,
lib/moleculer/support/string_util.rb,
lib/moleculer/errors/node_not_found.rb,
lib/moleculer/errors/action_not_found.rb,
lib/moleculer/errors/invalid_action_response.rb,
lib/moleculer/errors/transporter_already_started.rb,
lib/moleculer/errors/local_node_already_registered.rb

Overview

frozen_string_literal: true

Defined Under Namespace

Modules: Errors, Packets, Serializers, Service, Support, Transporters Classes: Broker, Configuration, Context, Node, Registry

Constant Summary collapse

PROTOCOL_VERSION =
"3".freeze
VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#brokerMoleculer::Broker (readonly)

Returns the Moleculer Broker instance. Only one broker can exist per node.

Returns:

  • (Moleculer::Broker)

    the Moleculer Broker instance. Only one broker can exist per node.



35
36
37
# File 'lib/moleculer.rb', line 35

def broker
  @broker ||= Broker.new(config)
end

#service_prefixSymbol

Returns the service prefix. When used will prefix all services name with <service_prefix>., defaults to nil.

Returns:

  • (Symbol)

    the service prefix. When used will prefix all services name with <service_prefix>., defaults to nil



29
30
31
# File 'lib/moleculer.rb', line 29

def service_prefix
  @service_prefix
end

Instance Method Details

#call(action, params = {}, **kwargs) ⇒ Hash

Calls the given action. This method will block until the timeout is hit (default 5 seconds) or the action returns a response

Parameters:

  • action (Symbol)

    the name of the action to call

  • params (Hash) (defaults to: {})

    params to pass to the action

  • kwargs (Hash)

    call options (see Moleculer::Broker#call)

Returns:

  • (Hash)

    the request response



48
49
50
51
52
# File 'lib/moleculer.rb', line 48

def call(action, params = {}, **kwargs)
  return broker.call(action.to_s, kwargs) if params.empty?

  broker.call(action.to_s, params, kwargs)
end

#configObject



54
55
56
# File 'lib/moleculer.rb', line 54

def config
  @config ||= Configuration.new
end

#configure {|self| ... } ⇒ Object

Allows configuration of moleculer. For more information on configuration see the Readme

Yields:

  • (self)


62
63
64
# File 'lib/moleculer.rb', line 62

def configure
  yield config
end

#emit(event, data) ⇒ Object

Emits the given event to the Moleculer network.

Parameters:

  • event (String)

    the name of the event

  • data (Hash)

    the data related to the event



71
72
73
# File 'lib/moleculer.rb', line 71

def emit(event, data)
  broker.emit(event, data)
end

#runObject

Runs the moleculer broker. This is synchronous and blocks.



77
78
79
# File 'lib/moleculer.rb', line 77

def run
  broker.run
end

#startObject

Starts the moleculer broker. This is asynchronous and does not block.



83
84
85
# File 'lib/moleculer.rb', line 83

def start
  broker.start
end

#stopObject

Stops the moleculer broker.



89
90
91
# File 'lib/moleculer.rb', line 89

def stop
  broker.stop
end

#wait_for_services(*services) ⇒ Object

Blocks the processes until the requested services have been registered on the network.

Examples:

Moleculer.wait_for_services("some.service", "someother.service")

Parameters:

  • services (Array<String>)

    a list of service to wait for.



100
101
102
# File 'lib/moleculer.rb', line 100

def wait_for_services(*services)
  broker.wait_for_services(*services)
end