mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Removes bad destinations from array
This commit is contained in:
@@ -30,7 +30,7 @@ This is just a simple prototype. To get to production would need:
|
|||||||
## Testing for reals
|
## Testing for reals
|
||||||
|
|
||||||
- start router.go on remote server (there's a test project on SD already). if logged in, go build ./src/router; sudo ./router
|
- start router.go on remote server (there's a test project on SD already). if logged in, go build ./src/router; sudo ./router
|
||||||
- iron_worker upload app_worker
|
- iron_worker upload sinatra
|
||||||
- iron_worker queue app_worker
|
- iron_worker queue sinatra
|
||||||
- ruby client.rb
|
- ruby client.rb
|
||||||
- BOOM!
|
- BOOM!
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
runtime "ruby"
|
|
||||||
|
|
||||||
gem 'rest'
|
|
||||||
gem 'sinatra'
|
|
||||||
|
|
||||||
exec 'worker.rb'
|
|
||||||
@@ -32,6 +32,7 @@ func init() {
|
|||||||
|
|
||||||
type Route struct {
|
type Route struct {
|
||||||
// TODO: Change destinations to a simple cache so it can expire entries after 55 minutes (the one we use in common?)
|
// TODO: Change destinations to a simple cache so it can expire entries after 55 minutes (the one we use in common?)
|
||||||
|
Host string
|
||||||
Destinations []string
|
Destinations []string
|
||||||
ProjectId string
|
ProjectId string
|
||||||
Token string // store this so we can queue up new workers on demand
|
Token string // store this so we can queue up new workers on demand
|
||||||
@@ -71,14 +72,15 @@ func ProxyFunc(w http.ResponseWriter, req *http.Request) {
|
|||||||
// 3) This host has no active workers so we queue one (or more) up and return a 503 or something with message that says "try again in a minute"
|
// 3) This host has no active workers so we queue one (or more) up and return a 503 or something with message that says "try again in a minute"
|
||||||
route := routingTable[host]
|
route := routingTable[host]
|
||||||
// choose random dest
|
// choose random dest
|
||||||
if len(route.Destinations) == 0 {
|
if route.Host == "" {
|
||||||
fmt.Fprintln(w, "No matching routes!")
|
fmt.Fprintln(w, "Host not configured!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
destUrls := route.Destinations[rand.Intn(len(route.Destinations))]
|
destIndex := rand.Intn(len(route.Destinations))
|
||||||
|
destUrlString := route.Destinations[destIndex]
|
||||||
// todo: should check if http:// already exists.
|
// todo: should check if http:// already exists.
|
||||||
destUrls = "http://" + destUrls
|
destUrlString2 := "http://" + destUrlString
|
||||||
destUrl, err := url.Parse(destUrls)
|
destUrl, err := url.Parse(destUrlString2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error!", err)
|
fmt.Println("error!", err)
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -93,7 +95,12 @@ func ProxyFunc(w http.ResponseWriter, req *http.Request) {
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
// can't figure out how to compare types so comparing strings.... lame.
|
// can't figure out how to compare types so comparing strings.... lame.
|
||||||
if strings.Contains(etype.String(), "net.OpError") { // == reflect.TypeOf(net.OpError{}) { // couldn't figure out a better way to do this
|
if strings.Contains(etype.String(), "net.OpError") { // == reflect.TypeOf(net.OpError{}) { // couldn't figure out a better way to do this
|
||||||
fmt.Println("It's a network error, so we're going to start new task.")
|
if len(route.Destinations) > 1 {
|
||||||
|
fmt.Println("It's a network error, removing this destination from routing table.")
|
||||||
|
route.Destinations = append(route.Destinations[:destIndex], route.Destinations[destIndex+1:]...)
|
||||||
|
} else {
|
||||||
|
fmt.Println("It's a network error and no other destinations available so we're going to start new task.")
|
||||||
|
}
|
||||||
// start new worker
|
// start new worker
|
||||||
payload := map[string]interface{}{
|
payload := map[string]interface{}{
|
||||||
"token": route.Token,
|
"token": route.Token,
|
||||||
@@ -121,7 +128,6 @@ func ProxyFunc(w http.ResponseWriter, req *http.Request) {
|
|||||||
fmt.Println("Couldn't queue up worker!", err)
|
fmt.Println("Couldn't queue up worker!", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// start new worker if it's a connection error
|
// start new worker if it's a connection error
|
||||||
return
|
return
|
||||||
@@ -151,6 +157,7 @@ func AddWorker(w http.ResponseWriter, req *http.Request) {
|
|||||||
// todo: one cache entry per host domain
|
// todo: one cache entry per host domain
|
||||||
route := routingTable[r2.Host]
|
route := routingTable[r2.Host]
|
||||||
fmt.Println("ROUTE:", route)
|
fmt.Println("ROUTE:", route)
|
||||||
|
route.Host = r2.Host
|
||||||
route.Destinations = append(route.Destinations, r2.Dest)
|
route.Destinations = append(route.Destinations, r2.Dest)
|
||||||
route.ProjectId = projectId
|
route.ProjectId = projectId
|
||||||
route.Token = token
|
route.Token = token
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ puts "port: #{port}"
|
|||||||
|
|
||||||
project_id = params[:project_id] || "4fd2729368a0197d1102056b" # // this is my Default project
|
project_id = params[:project_id] || "4fd2729368a0197d1102056b" # // this is my Default project
|
||||||
token = params[:token] || "MWx0VfngzsCu0W8NAYw7S2lNrgo"
|
token = params[:token] || "MWx0VfngzsCu0W8NAYw7S2lNrgo"
|
||||||
code_name = params[:code_name] || "app_worker"
|
code_name = params[:code_name] || "sinatra"
|
||||||
|
|
||||||
response = rest.post(
|
response = rest.post(
|
||||||
# "http://localhost:8080/",
|
# "http://localhost:8080/",
|
||||||
@@ -23,12 +23,13 @@ response = rest.post(
|
|||||||
body: {"host"=>"routertest.irondns.info", "dest"=>"#{public_dns}:#{port}"})
|
body: {"host"=>"routertest.irondns.info", "dest"=>"#{public_dns}:#{port}"})
|
||||||
puts "body:"
|
puts "body:"
|
||||||
puts response.body
|
puts response.body
|
||||||
|
puts "\n\n=======\n\n"
|
||||||
|
|
||||||
STDOUT.flush
|
STDOUT.flush
|
||||||
|
|
||||||
# Now we start the actual worker
|
# Now we start the actual worker
|
||||||
##################################################################3
|
##################################################################3
|
||||||
|
|
||||||
ENV['PORT'] = port.to_s # for sinatra
|
ENV['PORT'] = port.to_s # for sinatra
|
||||||
my_app = Sinatra.new do
|
my_app = Sinatra.new do
|
||||||
set :port, port
|
set :port, port
|
||||||
|
|||||||
Reference in New Issue
Block a user