Exemple #1
0
// DPNWorkItemSave saves a DPNWorkItem record to Pharos. If the WorkItems's
// ID is zero, this performs a POST to create a new record. For non-zero
// IDs, this performs a PUT to update the existing record. The response object
// will include a new copy of the WorkItem if it was saved successfully.
func (client *PharosClient) DPNWorkItemSave(obj *models.DPNWorkItem) *PharosResponse {
	// Set up the response object
	resp := NewPharosResponse(PharosDPNWorkItem)
	resp.dpnWorkItems = make([]*models.DPNWorkItem, 1)

	// URL and method
	relativeUrl := fmt.Sprintf("/api/%s/dpn_item/", client.apiVersion)
	httpMethod := "POST"
	if obj.Id > 0 {
		// URL should look like /api/v2/items/46956/
		relativeUrl = fmt.Sprintf("%s%d/", relativeUrl, obj.Id)
		httpMethod = "PUT"
	}
	absoluteUrl := client.BuildUrl(relativeUrl)

	// Prepare the JSON data
	postData, err := obj.SerializeForPharos()
	if err != nil {
		resp.Error = err
	}

	// Run the request
	client.DoRequest(resp, httpMethod, absoluteUrl, bytes.NewBuffer(postData))
	if resp.Error != nil {
		return resp
	}

	// Parse the JSON from the response body
	dpnWorkItem := &models.DPNWorkItem{}
	resp.Error = json.Unmarshal(resp.data, dpnWorkItem)
	if resp.Error == nil {
		resp.dpnWorkItems[0] = dpnWorkItem
	}
	return resp
}
Exemple #2
0
// queueTransfer adds a transfer task to NSQ and records info about
// when the item was queued in DPNWorkItem.QueuedAt, which is saved to Pharos.
func (dpnQueue *DPNQueue) queueTransfer(dpnWorkItem *apt_models.DPNWorkItem, taskType string) {
	queueTopic := ""
	if taskType == constants.DPNTaskReplication {
		// Copy is first step of replication
		queueTopic = dpnQueue.Context.Config.DPN.DPNCopyWorker.NsqTopic
	} else if taskType == constants.DPNTaskRestore {
		queueTopic = dpnQueue.Context.Config.DPN.DPNRestoreWorker.NsqTopic
	} else {
		dpnQueue.Context.MessageLog.Error("Illegal taskType '%s'", taskType)
		return
	}
	// Put the item into NSQ
	err := dpnQueue.Context.NSQClient.Enqueue(queueTopic, dpnWorkItem.Id)
	if err != nil {
		dpnQueue.err("Error queueing DPNWorkItem %d, %s %s: %v",
			dpnWorkItem.Id, taskType, dpnWorkItem.Identifier, err)
	} else {
		// Let Pharos know this item has been queued
		dpnQueue.Context.MessageLog.Info("Added %s %s (DPNWorkItem %d) to NSQ topic %s",
			taskType, dpnWorkItem.Identifier, dpnWorkItem.Id, queueTopic)
		utcNow := time.Now().UTC()
		dpnWorkItem.QueuedAt = &utcNow
		resp := dpnQueue.Context.PharosClient.DPNWorkItemSave(dpnWorkItem)
		if resp.Error != nil {
			dpnQueue.err("Error updating DPNWorkItem %d for %s %s to %s: %v",
				dpnWorkItem.Id, taskType, dpnWorkItem.Identifier, resp.Error)
			return
		}
		dpnWorkItem = resp.DPNWorkItem()
		dpnQueue.Context.MessageLog.Info("Set QueuedAt for %s %s (DPNWorkItem %d) to %s",
			taskType, dpnWorkItem.Identifier, dpnWorkItem.Id,
			dpnWorkItem.QueuedAt.Format(time.RFC3339))
	}
}