Beispiel #1
0
func (m *WorkerManager) RegisterWorker(info WorkerInfo) {
	m.mutex.Lock()
	defer m.mutex.Unlock()
	log := bitwrk.Root().Newf("Worker %#v", info.Id)
	if s, ok := m.workers[info.Id]; ok {
		log.Printf("Reported idle: %v", info)
		s.cond.L.Lock()
		defer s.cond.L.Unlock()
		s.Info = info
		if !s.Idle {
			s.Idle = true
			s.Blockers--
		}
		s.cond.Broadcast()
	} else {
		log.Printf("Registered: %v", info)
		s = &WorkerState{
			m:    m,
			cond: sync.NewCond(new(sync.Mutex)),
			Info: info,
			Idle: true,
		}
		m.workers[info.Id] = s
		go s.offer(log)
	}
}
Beispiel #2
0
func (m *WorkerManager) UnregisterWorker(id string) {
	m.mutex.Lock()
	defer m.mutex.Unlock()
	if s, ok := m.workers[id]; ok {
		delete(m.workers, id)
		s.cond.L.Lock()
		defer s.cond.L.Unlock()
		s.Unregistered = true
		s.cond.Broadcast()
		bitwrk.Root().Newf("Worker %#v", id).Printf("Unregistered: %v", s.Info)
	}
}
Beispiel #3
0
func handleBuy(w http.ResponseWriter, r *http.Request) {
	article := r.URL.Path[5:]

	log.Printf("Handling buy for %#v from %v", article, r.RemoteAddr)

	if r.Method != "POST" {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}

	var buy *client.BuyActivity
	if _buy, err := client.GetActivityManager().NewBuy(bitwrk.ArticleId(article)); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Printf("Error creating buy activity: %v", err)
		return
	} else {
		buy = _buy
	}
	defer buy.Dispose()

	log := bitwrk.Root().Newf("Buy #%v", buy.GetKey())

	var reader io.Reader
	if multipart, err := r.MultipartReader(); err != nil {
		// read directly from body
		reader = r.Body
	} else {
		// Iterate through parts of multipart body, find the one called "data"
		for {
			if part, err := multipart.NextPart(); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				log.Printf("Error iterating through multipart content: %v", err)
				return
			} else {
				if part.FormName() == "data" {
					reader = part
					break
				} else {
					log.Printf("Skipping form part %v", part)
				}
			}
		}
	}

	workTemp := client.GetActivityManager().GetStorage().Create(fmt.Sprintf("buy #%v: work", buy.GetKey()))
	defer workTemp.Dispose()
	if _, err := io.Copy(workTemp, reader); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Printf("Error receiving work data from client: %v", err)
		return
	} else {
		if err := workTemp.Close(); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			log.Printf("Error writing work data to storage: %v", err)
			return
		}
	}

	// Listen for close notfications
	interrupt := w.(http.CloseNotifier).CloseNotify()

	workFile := workTemp.File()
	defer workFile.Dispose()
	var result cafs.File
	if res, err := buy.PerformBuy(log, interrupt, workFile); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Printf("Error receiving result from BitWrk network: %v", err)
		return
	} else {
		result = res
	}

	http.Redirect(w, r, "/file/"+result.Key().String(), http.StatusSeeOther)
}