Class: AlWorker::Ipc

Inherits:
Object
  • Object
show all
Defined in:
lib/al_worker_ipc.rb

Overview

IPCサーバ

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socketfile = nil) ⇒ Ipc

constructor

Parameters:

  • socketfile (String) (defaults to: nil)

    IPCソケットファイル



58
59
60
61
# File 'lib/al_worker_ipc.rb', line 58

def initialize( socketfile = nil )
  @socketfile = socketfile
  @cb_prefix = "ipc"
end

Instance Attribute Details

Returns 接続後のバナー表示.

Returns:

  • (String)

    接続後のバナー表示



50
51
52
# File 'lib/al_worker_ipc.rb', line 50

def banner
  @banner
end

#cb_prefixString

Returns コールバックメソッドのプレフィックス.

Returns:

  • (String)

    コールバックメソッドのプレフィックス



47
48
49
# File 'lib/al_worker_ipc.rb', line 47

def cb_prefix
  @cb_prefix
end

#chmodInteger

Returns IPCソケットのread/writeモード (chmodの値).

Returns:

  • (Integer)

    IPCソケットのread/writeモード (chmodの値)



44
45
46
# File 'lib/al_worker_ipc.rb', line 44

def chmod
  @chmod
end

#socketfileString (readonly)

Returns IPCソケットファイル.

Returns:

  • (String)

    IPCソケットファイル



41
42
43
# File 'lib/al_worker_ipc.rb', line 41

def socketfile
  @socketfile
end

Class Method Details

.finalizer(socketfile) ⇒ Object

object finalizer



20
21
22
23
24
# File 'lib/al_worker_ipc.rb', line 20

def self.finalizer( socketfile )
  proc {
    File.unlink( socketfile ) rescue 0
  }
end

.open(socketfile = nil, &block) ⇒ Object

クライアント接続ソケットの作成

Parameters:

  • socketfile (String) (defaults to: nil)

    IPCソケットファイル



32
33
34
35
36
# File 'lib/al_worker_ipc.rb', line 32

def self.open( socketfile = nil, &block )
  # ソケットファイル、デフォルト値を使う
  socketfile ||= File.join( AlWorker::DEFAULT_WORKDIR, AlWorker::DEFAULT_NAME )
  AlWorker::IpcClient.open( socketfile, &block )
end

Instance Method Details

#run(obj) ⇒ Object

実行開始

Parameters:

  • obj (AlWorker)

    ワーカオブジェクト



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/al_worker_ipc.rb', line 69

def run( obj )
  @me = obj
  @socketfile ||= File.join( @me.workdir, @me.name )

  # ソケットオープン
  begin
    UNIXSocket.open( @socketfile ) {}
    raise "IPC socket file '#{@socketfile}' was already in use."

  rescue Errno::ENOENT
    # ok continue.
  rescue Errno::ECONNREFUSED
    # file exist but not use.
    File.unlink( @socketfile ) rescue 0
  end

  server = UNIXServer.new( @socketfile )
  File.chmod( @chmod, @socketfile )  if @chmod

  ObjectSpace.define_finalizer( self, self.class.finalizer( @socketfile ) )

  # サービス開始
  Thread.start {
    loop do
      Thread.start( server.accept ) { |sock|
        _start_service( sock )
      }
    end
  }
end