Skip to content

Commit

Permalink
Don't delete retainer tasks once they've been accepted.
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisdhaas committed Jun 17, 2015
1 parent dcc9051 commit 818a4f0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ampcrowd/basecrowd/models.py
Expand Up @@ -170,7 +170,7 @@ class AbstractRetainerPool(models.Model):
# The relationship will be auto-generated to the worker class of the
# registered crowd, and can be accessed via the 'workers' attribute.
# The related_name will be 'pools' to enable reverse lookups, e.g.
# workers = models.ManyToManyField(CrowdTask, related_name='pools')
# workers = models.ManyToManyField(CrowdWorker, related_name='pools')

# The status of this pool.
STATUSES = (
Expand Down
24 changes: 16 additions & 8 deletions ampcrowd/basecrowd/tasks.py
Expand Up @@ -41,6 +41,7 @@ def submit_callback_answer(current_task):
urllib2.urlopen(url, urllib.urlencode(params))

# Recruit for retainer pools by auto-posting tasks as necessary.
# TODO: worry about concurrency if multiple of these run at once.
@celery.task
def post_retainer_tasks():
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -90,12 +91,12 @@ def post_retainer_tasks():

# skip interface.task_pre_save because this isn't a real task.
task = crowd_model_spec.task_model.objects.create(
task_type=dummy_config['task_type'],
task_type=task_config['task_type'],
data=dummy_content,
create_time=timezone.now(),
task_id=task_id,
group=pool.task_groups.order_by('created_at')[0],
num_assignments=1
num_assignments=task_config['num_assignments'],
)
logger.info("Created Task %s" % task_id)

Expand Down Expand Up @@ -124,6 +125,9 @@ def post_retainer_tasks():
task_group.save()
pool.save()

else:
logger.info("%s has status %s, nothing to do." % (pool, pool.get_status_display()))

# Delete old retainerTasks to keep the listings fresh
logger.info('Removing old retainer tasks...')
for retainer_task in RetainerTask.objects.filter(active=True):
Expand All @@ -132,12 +136,16 @@ def post_retainer_tasks():
- timedelta(seconds=settings.RETAINER_TASK_EXPIRATION_SECONDS))
if retainer_task.created_at < old_task_cutoff:
try:
# delete the underlying task object
interface, _ = CrowdRegistry.get_registry_entry(
retainer_task.crowd_name)
interface.delete_tasks([retainer_task.task,])
retainer_task.task.delete()
logger.info("Deleted old task %s" % retainer_task.task)
# delete the underlying task object if no one has accepted it.
if not retainer_task.task.workers.exists():
interface, _ = CrowdRegistry.get_registry_entry(
retainer_task.crowd_name)
interface.delete_tasks([retainer_task.task,])
retainer_task.task.delete()
logger.info("Deleted old task %s" % retainer_task.task)
else:
logger.info("Not deleting %s, it has a worker."
% retainer_task.task)

# delete the retainer task
retainer_task.active = False
Expand Down
30 changes: 30 additions & 0 deletions ampcrowd/basecrowd/templates/basecrowd/retainer.html
@@ -0,0 +1,30 @@
{% extends base_template_name %}
<!-- Add HTML to display an interface to the crowd for completing tasks of a new type.
This template will display a single-screen interface to the crowd for gathering labels of one or more items of the same
task type. To define your task type, copy this template, then fill in the blocks below.
Note: Twitter Bootstrap CSS classes will be available for styling.
-->

{% block jslibraries %}
{{ block.super }}
<script type='text/javascript' src='https://raw.githubusercontent.com/uid/realtime-turk/master/retainer/static/retainer.js'></script>
{% endblock jslibraries %}

{% block customjs %}
{{ block.super }}
{% endblock customjs %}

{% block default_instruction %}
<p> This will be a retainer task--we will show you various tasks inside this!</p>
{% endblock %}

{% block item_data %}
<p>Hopefully unnecessary</p>
{% endblock %}

{% block item_labels %}
<p>Hopefully really unnecessary</p>
{% endblock %}
7 changes: 5 additions & 2 deletions ampcrowd/basecrowd/views.py
Expand Up @@ -181,7 +181,7 @@ def get_assignment(request, crowd_name):
template = get_scoped_template(crowd_name, 'unavailable.html')
return HttpResponse(template.render(RequestContext(request, {})))

# Retrieve the tweet based on task_id from the database
# Retrieve the task based on task_id from the database
try:
current_task = model_spec.task_model.objects.get(
task_id=context['task_id'])
Expand All @@ -196,7 +196,7 @@ def get_assignment(request, crowd_name):
worker_id=worker_id)
except model_spec.worker_model.DoesNotExist:
current_worker = model_spec.worker_model(
worker_id=context['worker_id'])
worker_id=worker_id)

# Call the pre-save hook, the save to the database
interface.worker_pre_save(current_worker)
Expand All @@ -211,6 +211,9 @@ def get_assignment(request, crowd_name):
if not current_worker.tasks.filter(task_id=current_task.task_id).exists():
current_worker.tasks.add(current_task)

if current_task.task_type == 'retainer':
current_worker.pools.add(current_task.group.retainer_pool)

# Add task data to the context.
content = json.loads(current_task.data)
group_context = json.loads(current_task.group.group_context)
Expand Down

0 comments on commit 818a4f0

Please sign in to comment.