'Handle post request.'

def post(self):

    spec = model.MapreduceSpec.from_json_str(self.request.get('mapreduce_spec'))
    logging.debug('post:    id=%s    headers=%s', spec.mapreduce_id, self.request.headers)
    (state, control) = db.get([model.MapreduceState.get_key_by_job_id(spec.mapreduce_id), model.MapreduceControl.get_key_by_job_id(spec.mapreduce_id)])
    if (not state):
        logging.error("State    not    found    for    mapreduce_id    '%s';    skipping", spec.mapreduce_id)
        return
    shard_states = model.ShardState.find_by_mapreduce_id(spec.mapreduce_id)
    if (state.active and (len(shard_states) != spec.mapper.shard_count)):
        logging.error("Incorrect    number    of    shard    states:    %d    vs    %d;    aborting    job    '%s'", len(shard_states), spec.mapper.shard_count, spec.mapreduce_id)
        state.active = False
        state.result_status = model.MapreduceState.RESULT_FAILED
        model.MapreduceControl.abort(spec.mapreduce_id)
    active_shards = [s for s in shard_states if s.active]
    failed_shards = [s for s in shard_states if (s.result_status == model.ShardState.RESULT_FAILED)]
    aborted_shards = [s for s in shard_states if (s.result_status == model.ShardState.RESULT_ABORTED)]
    if state.active:
        state.active = bool(active_shards)
        state.active_shards = len(active_shards)
        state.failed_shards = len(failed_shards)
        state.aborted_shards = len(aborted_shards)
    if ((not state.active) and control and (control.command == model.MapreduceControl.ABORT)):
        logging.info("Abort    signal    received    for    job    '%s'", spec.mapreduce_id)
        state.result_status = model.MapreduceState.RESULT_ABORTED
    if (not state.active):
        state.active_shards = 0
        if (not state.result_status):
            if [s for s in shard_states if (s.result_status != model.ShardState.RESULT_SUCCESS)]:
                state.result_status = model.MapreduceState.RESULT_FAILED
            else:
                state.result_status = model.MapreduceState.RESULT_SUCCESS
            logging.info("Final    result    for    job    '%s'    is    '%s'", spec.mapreduce_id, state.result_status)
    self.aggregate_state(state, shard_states)
    poll_time = state.last_poll_time
    state.last_poll_time = datetime.datetime.utcfromtimestamp(self._time())
    if (not state.active):
        def put_state(state):
            state.put()
            done_callback = spec.params.get(model.MapreduceSpec.PARAM_DONE_CALLBACK)
            if done_callback:
                taskqueue.Task(url=done_callback, headers={'Mapreduce-Id': spec.mapreduce_id}).add(spec.params.get(model.MapreduceSpec.PARAM_DONE_CALLBACK_QUEUE, 'default'), transactional=True)
        db.run_in_transaction(put_state, state)
        return
    else:
        state.put()
    processing_rate = int((spec.mapper.params.get('processing_rate') or model._DEFAULT_PROCESSING_RATE_PER_SEC))
    self.refill_quotas(poll_time, processing_rate, active_shards)
    ControllerCallbackHandler.reschedule(self.base_path(), spec, (self.serial_id() + 1))
