public ITask getTask(String taskId) { String name = "getTask"; int waittime = 2; while (true) { synchronized (service) { try { HIT hit = service.getHIT(taskId); // LOGGER.info(String.format("Retrieved HIT %s", hit.getHITId())); return new MturkTask(hit); } catch (InternalServiceException ise) { if (overTime(name, waittime)) { LOGGER.error(String.format("%s ran over time", name)); return null; } LOGGER.warn(format("{0} {1}", name, ise)); chill(waittime); waittime *= 2; } catch (ObjectDoesNotExistException odnee) { LOGGER.warn(format("{0} {1}", name, odnee)); } } } }
private List<Assignment> getAllAssignmentsForHIT( HIT hit) { String name = "getAllAssignmentsForHIT"; int waittime = 2; while (true) { synchronized (service) { try { Assignment[] hitAssignments = service.getAllAssignmentsForHIT(hit.getHITId()); List<Assignment> assignments = new LinkedList<>(); boolean addAll = assignments.addAll(Arrays.asList(hitAssignments)); if (addAll) LOGGER.info(String.format("Retrieved %d assignments for HIT %s", hitAssignments.length, hit.getHITId())); return assignments; } catch (InternalServiceException ise) { LOGGER.warn(format("{0} {1}", name, ise)); chill(waittime); waittime *= 2; } } } }
@Override public boolean makeTaskAvailable( String taskId, Record record) { String name = "makeTaskAvailable"; int waitTime = 1; while (true){ try { MturkTask task = (MturkTask) getTask(taskId); int currentMaxAssignments = service.getAllAssignmentsForHIT(task.getTaskId()).length; int maxAssignmentsIncrement = Integer.parseInt(record.library.props.getProperty(Parameters.NUM_PARTICIPANTS)) - currentMaxAssignments; long expirationIncrementMillis = task.hit.getExpiration().getTimeInMillis() - System.currentTimeMillis(); service.extendHIT(taskId, maxAssignmentsIncrement, expirationIncrementMillis / 1000); return true; } catch (InternalServiceException ise) { LOGGER.warn(format("{0} {1}", name, ise)); if (overTime(name, waitTime)) return false; chill(waitTime); waitTime = 2 * waitTime; } } }
@Override public boolean makeTaskUnavailable( ITask task) { String name = "expireHIT"; while (true){ synchronized (service) { try{ service.forceExpireHIT(task.getTaskId()); return true; }catch(InternalServiceException ise){ LOGGER.warn(MessageFormat.format("{0} {1}", name, ise)); chill(1); }catch(ObjectDoesNotExistException odne) { LOGGER.warn(MessageFormat.format("{0} {1}", name, odne)); return false; } } } }
int numAvailableAssignments(ITask task) { String name = "availableAssignments"; while (true){ synchronized (service) { try{ HIT hit = service.getHIT(task.getTaskId()); return hit.getNumberOfAssignmentsAvailable(); }catch(InternalServiceException ise){ LOGGER.warn(MessageFormat.format("{0} {1}", name, ise)); chill(1); }catch(ObjectDoesNotExistException odne) { LOGGER.warn(MessageFormat.format("{0} {1}", name, odne)); return 0; } } } }
private boolean approveAssignment(String assignmentId) { Class clz = new Object(){}.getClass(); int waittime = 1; while (true) { synchronized (service) { try { service.approveAssignment(assignmentId, "Thank you."); return true; } catch (InternalServiceException ise) { if (waittime > maxWaitTimeInSeconds) { String msg = String.format("WARNING: Exceeded max wait time in %s.%s..." , clz.getEnclosingClass().getName() , clz.getEnclosingMethod().getName()); LOGGER.warn(msg); System.err.println(msg); } chill(waittime); waittime *= 2; } catch(ObjectDoesNotExistException odne) { LOGGER.warn(MessageFormat.format("{0} {1}", clz, odne)); return false; } } } }
/** * Handles/logs throttling errors in the sandbox environment * @param ex */ private void handleException(Exception ex) throws InternalServiceException { if (ex instanceof InternalServiceException && service.getConfig().getServiceURL().equalsIgnoreCase(ClientConfig.SANDBOX_SERVICE_URL)) { logFailure(ex); throw (InternalServiceException)ex; } }
/** * * @param e - exception caught * @param requests - the requests which caused the exception * @return true if the request can be retried. i.e. check the retriable error set */ protected boolean shouldRetry(ServiceException e, Object requests) { if (e instanceof InternalServiceException) { InternalServiceException exception = (InternalServiceException) e; for (String errorCode : exception.getErrorCodes()) { if (this.retriableErrors.contains(errorCode)) return true; } return false; } return false; }
String getWebsiteURL() { String name = "getWebsiteURL"; synchronized (service) { while(true) { try { return service.getWebsiteURL(); } catch (InternalServiceException ise) { LOGGER.warn(MessageFormat.format("{0} {1}", name, ise)); chill(3); } } } }
ITask addAssignments(ITask task, int n) { Class name = new Object(){}.getClass(); int waittime = 1; while (true){ synchronized (service) { try { String id = task.getTaskId(); service.extendHIT(id, n, minExpirationIncrementInSeconds); return task; } catch(InternalServiceException ise){ LOGGER.warn(MessageFormat.format("{0} {1}; error codes: {2}", name, ise, StringUtils.join(ise.getErrorCodes(), ","))); if (waittime > maxWaitTimeInSeconds) { String msg = String.format("WARNING: Exceeded max wait time in %s.%s..." , name.getEnclosingClass().getName() , name.getEnclosingMethod().getName()); LOGGER.warn(msg); System.err.println(msg); } chill(waittime); waittime *= 2; }catch(ObjectDoesNotExistException odne) { LOGGER.warn(MessageFormat.format("{0} {1}", name, odne)); return null; } } } }
@Override public void awardBonus( double amount, edu.umass.cs.surveyman.analyses.SurveyResponse sr, Survey survey) { String name = "awardBonus"; int waitTime = 1; while (true){ try { Record r = getRecord(survey); Runner.LOGGER.info("All tasks for this record:" + r.getAllTasks().length); if (r.getAllTasks().length==0){ Runner.LOGGER.info("No tasks for record " + r.rid); return; } for (ITask task : r.getAllTasks()) { Assignment[] assignments = service.getAllAssignmentsForHIT(task.getTaskId()); Runner.LOGGER.info("all assignments for this record:" + assignments.length); String assignmentId = ""; for (Assignment a : assignments) { if (a.getWorkerId().equals(sr.getSrid())) { service.grantBonus(sr.getSrid(), amount, assignmentId, "For partial work completed."); Runner.LOGGER.info(String.format("Granted worker %s bonus %f for assignment %s in survey %s" , sr.getSrid(), amount, assignmentId, survey.sourceName)); } } } } catch (InternalServiceException ise) { LOGGER.warn(format("{0} {1}", name, ise)); if (overTime(name, waitTime)){ return; } chill(waitTime); waitTime = 2 * waitTime; } catch (SurveyException | IOException e) { e.printStackTrace(); } } }