public static void Run([QueueTrigger("hit-disposal")] string myQueueItem, TraceWriter log) { bool logging = false; if (logging) { log.Info($"C# Queue trigger function processed: {myQueueItem}"); } SatyamAmazonHITTableAccessEntry hitEntryToBeRemoved = JSonUtils.ConvertJSonToObject <SatyamAmazonHITTableAccessEntry>(myQueueItem); //check if it's still in the DB SatyamAmazonHITTableAccess hitDB = new SatyamAmazonHITTableAccess(); bool exist = hitDB.EntryExistByID(hitEntryToBeRemoved.ID.ToString()); hitDB.close(); if (!exist) { return; } //AmazonHITManagement.SafeDeleteHit(hitEntryToBeRemoved); AmazonMTurkHIT hit = new AmazonMTurkHIT(); hit.setAccount(hitEntryToBeRemoved.AmazonAccessKeyID, hitEntryToBeRemoved.AmazonSecretAccessKeyID, false); if (hit.DeleteHIT(hitEntryToBeRemoved.HITID)) { hitDB = new SatyamAmazonHITTableAccess(); hitDB.DeleteEntry(hitEntryToBeRemoved.ID); hitDB.close(); } else { try { hit.expireHIT(hitEntryToBeRemoved.HITID); } catch { Console.WriteLine("HIT already expired?"); } hitDB = new SatyamAmazonHITTableAccess(); hitDB.UpdateStatus(hitEntryToBeRemoved.ID, HitStatus.expired); hitDB.close(); } }
public static bool SafeDeleteHit(SatyamAmazonHITTableAccessEntry hitEntryToBeRemoved) { // first expire AmazonMTurkHIT hit = new AmazonMTurkHIT(); hit.setAccount(hitEntryToBeRemoved.AmazonAccessKeyID, hitEntryToBeRemoved.AmazonSecretAccessKeyID, false); hit.expireHIT(hitEntryToBeRemoved.HITID); SatyamAmazonHITTableAccess hitDB = new SatyamAmazonHITTableAccess(); hitDB.UpdateStatus(hitEntryToBeRemoved.ID, HitStatus.expired); hitDB.close(); // then try dispose if (!hit.DeleteHIT(hitEntryToBeRemoved.HITID)) { // can't dispose due to invalid status: reviewable but payment not cleared, or disposed. // because payment not cleared // find all results of this hitID and mark for rejection if (!resultCache.ContainsKey(hitEntryToBeRemoved.JobGUID)) { List <SatyamResultsTableEntry> ret = new List <SatyamResultsTableEntry>(); SatyamResultsTableAccess resDB = new SatyamResultsTableAccess(); List <SatyamResultsTableEntry> tempres = resDB.getEntriesByGUID(hitEntryToBeRemoved.JobGUID); resDB.close(); resultCache.Add(hitEntryToBeRemoved.JobGUID, tempres); } List <SatyamResultsTableEntry> results = resultCache[hitEntryToBeRemoved.JobGUID]; List <SatyamResultsTableEntry> res = new List <SatyamResultsTableEntry>(); foreach (SatyamResultsTableEntry r in results) { SatyamResult rr = JSonUtils.ConvertJSonToObject <SatyamResult>(r.ResultString); if (rr.amazonInfo.HITID == hitEntryToBeRemoved.HITID) { res.Add(r); } } if (res.Count == 0) { // the hit can't be disposed because there are pending assignments, yet we don't have any on records, // so delete it. AWS will automatically handle it after 120 days of inactivity. hitDB = new SatyamAmazonHITTableAccess(); hitDB.DeleteEntry(hitEntryToBeRemoved.ID); hitDB.close(); Console.WriteLine("Delete Success\n"); return(true); } //bool HITDone = true; foreach (SatyamResultsTableEntry r in res) { //SatyamResult rr = JSonUtils.ConvertJSonToObject<SatyamResult>(r.ResultString); //string assignmentID = rr.amazonInfo.AssignmentID; //hit.RejectAssignment(assignmentID, "Sorry! Your work was not within acceptable parameters!"); if (r.Status == ResultStatus.inconclusive || r.Status == ResultStatus.outdated) { // reject all for now... TODO: check if there is an aggregated results. //Console.WriteLine("Rejecting result ID {0}", r.ID); //SatyamResultsTableAccess resultsDB = new SatyamResultsTableAccess(); //resultsDB.UpdateStatusByID(r.ID, ResultStatus.rejected); //resultsDB.close(); //HITDone = false; } else { Console.WriteLine("Result ID {0} is of status {1}", r.ID, r.Status); if (r.Status == ResultStatus.accepted || r.Status == ResultStatus.rejected) { // need to be handled by payment function //HITDone = false; } // just in case last payment was not made successfully, // mark it to be paid again.... SatyamResultsTableAccess resultsDB = new SatyamResultsTableAccess(); if (ResultStatus.acceptedStatusList.Contains(r.Status)) { Console.WriteLine("Marking as Accepted so Next Cycle will Pay"); resultsDB.UpdateStatusByID(r.ID, ResultStatus.accepted); } if (ResultStatus.rejectedStatusList.Contains(r.Status)) { Console.WriteLine("Marking as Rejected so Next Cycle will Reject"); resultsDB.UpdateStatusByID(r.ID, ResultStatus.rejected); } resultsDB.close(); } } // Commented out cuz the case where the inconsistency status happen is rare, // so don't delete the hit too aggressively //if (HITDone) //{ // hitDB = new SatyamAmazonHITTableAccess(); // hitDB.DeleteEntry(hitEntryToBeRemoved.ID); // hitDB.close(); // Console.WriteLine("Delete Success\n"); // return true; //} Console.WriteLine("Delete Failure\n"); return(false); } else { hitDB = new SatyamAmazonHITTableAccess(); hitDB.DeleteEntry(hitEntryToBeRemoved.ID); hitDB.close(); Console.WriteLine("Delete Success\n"); return(true); } }