Class: Wolverine::Script

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

Overview

Script represents a lua script in the filesystem. It loads the script from disk and handles talking to redis to execute it. Error handling is handled by LuaError.

Instance Method Summary (collapse)

Constructor Details

- (Script) initialize(file)

Loads the script file from disk and calculates its SHA1 sum.

Parameters:

  • file (Pathname)

    the full path to the indicated file



14
15
16
17
18
# File 'lib/wolverine/script.rb', line 14

def initialize file
  @file = Pathname.new(file)
  @content = load_lua file
  @digest = Digest::SHA1.hexdigest @content
end

Instance Method Details

- (Object) call(redis, *args)

Passes the script and supplied arguments to redis for evaulation. It first attempts to use a script redis has already cached by using the EVALSHA command, but falls back to providing the full script text via EVAL if redis has not seen this script before. Future invocations will then use EVALSHA without erroring.

Parameters:

  • redis (Redis)

    the redis connection to run against

  • args (*Objects)

    the arguments to the script

Returns:

  • (Object)

    the value passed back by redis after script execution

Raises:

  • (LuaError)

    if the script failed to compile of encountered a runtime error



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wolverine/script.rb', line 31

def call redis, *args
  begin
    run_evalsha redis, *args
  rescue => e
    e.message =~ /NOSCRIPT/ ? run_eval(redis, *args) : raise
  end
rescue => e
  if LuaError.intercepts?(e)
    raise LuaError.new(e, @file)
  else
    raise
  end
end