compose.lua 4.15 KB
Newer Older
Yu Gan's avatar
Yu Gan committed
1
local _M = {}
2
local xtracer = require "luaxtrace"
Yu Gan's avatar
Yu Gan committed
3 4 5 6 7

local function _StrIsEmpty(s)
  return s == nil or s == ''
end

8
local function _UploadUserId(req_id, post, carrier, baggage)
9
  xtracer.SetBaggage(baggage)
Yu Gan's avatar
Yu Gan committed
10 11
  local GenericObjectPool = require "GenericObjectPool"
  local UserServiceClient = require 'media_service_UserService'
12
  carrier["baggage"] = xtracer.BranchBaggage()
Yu Gan's avatar
Yu Gan committed
13 14
  local user_client = GenericObjectPool:connection(
    UserServiceClient,"user-service",9090)
15 16
  local status, err = user_client:UploadUserWithUsername(req_id, post.username, carrier)
  xtracer.JoinBaggage(err.baggage)
Yu Gan's avatar
Yu Gan committed
17 18 19
  GenericObjectPool:returnConnection(user_client)
end

20
local function _UploadText(req_id, post, carrier, baggage)
21
  xtracer.SetBaggage(baggage)
Yu Gan's avatar
Yu Gan committed
22 23
  local GenericObjectPool = require "GenericObjectPool"
  local TextServiceClient = require 'media_service_TextService'
24
  carrier["baggage"] = xtracer.BranchBaggage()
Yu Gan's avatar
Yu Gan committed
25 26
  local text_client = GenericObjectPool:connection(
    TextServiceClient,"text-service",9090)
27 28
  local status, err = text_client:UploadText(req_id, post.text, carrier)
  xtracer.JoinBaggage(err.baggage)
Yu Gan's avatar
Yu Gan committed
29 30 31
  GenericObjectPool:returnConnection(text_client)
end

32
local function _UploadMovieId(req_id, post, carrier, baggage)
33
  xtracer.SetBaggage(baggage)
Yu Gan's avatar
Yu Gan committed
34 35
  local GenericObjectPool = require "GenericObjectPool"
  local MovieIdServiceClient = require 'media_service_MovieIdService'
36
  carrier["baggage"] = xtracer.BranchBaggage()
Yu Gan's avatar
Yu Gan committed
37 38
  local movie_id_client = GenericObjectPool:connection(
    MovieIdServiceClient,"movie-id-service",9090)
39 40
  local status, err = movie_id_client:UploadMovieId(req_id, post.title, tonumber(post.rating), carrier)
  xtracer.JoinBaggage(err.baggage)
Yu Gan's avatar
Yu Gan committed
41 42 43
  GenericObjectPool:returnConnection(movie_id_client)
end

44 45
local function _UploadUniqueId(req_id, carrier, baggage)
  xtracer.SetBaggage(baggage)
Yu Gan's avatar
Yu Gan committed
46 47
  local GenericObjectPool = require "GenericObjectPool"
  local UniqueIdServiceClient = require 'media_service_UniqueIdService'
48
  carrier["baggage"] = xtracer.BranchBaggage()
Yu Gan's avatar
Yu Gan committed
49 50
  local unique_id_client = GenericObjectPool:connection(
    UniqueIdServiceClient,"unique-id-service",9090)
51 52
  status, err = unique_id_client:UploadUniqueId(req_id, carrier)
  xtracer.JoinBaggage(err.baggage)
Yu Gan's avatar
Yu Gan committed
53 54 55 56 57 58 59
  GenericObjectPool:returnConnection(unique_id_client)
end

function _M.ComposeReview()
  local bridge_tracer = require "opentracing_bridge_tracer"
  local ngx = ngx

60 61
  xtracer.StartLuaTrace("NginxWebServer5", "ComposeReview");
  xtracer.LogXTrace("Processing Request")
Yu Gan's avatar
Yu Gan committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  local req_id = tonumber(string.sub(ngx.var.request_id, 0, 15), 16)
  local tracer = bridge_tracer.new_from_global()
  local parent_span_context = tracer:binary_extract(ngx.var.opentracing_binary_context)
  local span = tracer:start_span("ComposeReview", {["references"] = {{"child_of", parent_span_context}}})
  local carrier = {}
  tracer:text_map_inject(span:context(), carrier)

  ngx.req.read_body()
  local post = ngx.req.get_post_args()

  if (_StrIsEmpty(post.title) or _StrIsEmpty(post.text) or
      _StrIsEmpty(post.username) or _StrIsEmpty(post.password) or
      _StrIsEmpty(post.rating)) then
    ngx.status = ngx.HTTP_BAD_REQUEST
    ngx.say("Incomplete arguments")
    ngx.log(ngx.ERR, "Incomplete arguments")
    ngx.exit(ngx.HTTP_BAD_REQUEST)
  end

81
  local userid_baggage = xtracer.BranchBaggage()
82
  local movieid_baggage = xtracer.BranchBaggage()
83 84
  local text_baggage = xtracer.BranchBaggage()
  local uuid_baggage = xtracer.BranchBaggage()
Yu Gan's avatar
Yu Gan committed
85
  local threads = {
86 87 88 89
    ngx.thread.spawn(_UploadUserId, req_id, post, carrier, userid_baggage),
    ngx.thread.spawn(_UploadMovieId, req_id, post, carrier, movieid_baggage),
    ngx.thread.spawn(_UploadText, req_id, post, carrier, text_baggage),
    ngx.thread.spawn(_UploadUniqueId, req_id, carrier, uuid_baggage)
Yu Gan's avatar
Yu Gan committed
90 91
  }

92 93 94 95 96 97 98
  local baggages = {
    userid_baggage,
    movieid_baggage,
    text_baggage,
    uuid_baggage
  }

Yu Gan's avatar
Yu Gan committed
99 100 101
  local status = ngx.HTTP_OK
  for i = 1, #threads do
    local ok, res = ngx.thread.wait(threads[i])
102
    xtracer.JoinBaggage(baggages[i])
Yu Gan's avatar
Yu Gan committed
103 104
    if not ok then
      status = ngx.HTTP_INTERNAL_SERVER_ERROR
105
      xtracer.LogXTrace("Internal Server error")
Yu Gan's avatar
Yu Gan committed
106 107 108
    end
  end
  span:finish()
109
  xtrace.LogXTrace("Request processing finished")
Yu Gan's avatar
Yu Gan committed
110
  ngx.exit(status)
111
  xtrace.DeleteBaggage()
Yu Gan's avatar
Yu Gan committed
112 113 114
  
end

115
return _M