示例#1
0
def AddToSyncQueue(command):
    """Add a command to the sync queue."""
    _, request_id, _, command_id = command.key.flat()
    payload = json.dumps({
        command_manager.COMMAND_ID_KEY: command_id,
        command_manager.REQUEST_ID_KEY: request_id,
    })
    now = common.Now()
    update_time = command.update_time or now
    timeout_seconds = GetCommandQueueTimeoutSeconds(command)
    if command.state != common.CommandState.QUEUED:
        # Schedule next sync based on when attempt may be cancelled.
        next_sync = now + datetime.timedelta(
            minutes=MAX_COMMAND_EVENT_DELAY_MIN)
    else:
        next_sync = min(
            # Time to next ensure command is leasable
            now + datetime.timedelta(minutes=MAX_COMMAND_EVENT_DELAY_MIN),
            # Time to cancel command
            update_time + datetime.timedelta(seconds=timeout_seconds))
    logging.info('Scheduling request [%s] command [%s] to be synced at %s',
                 request_id, command_id, next_sync)
    task_scheduler.AddTask(queue_name=COMMAND_SYNC_QUEUE,
                           payload=payload,
                           eta=next_sync)
 def Func():
     return task_scheduler.AddTask(queue_name='queue_name',
                                   payload='payload',
                                   name='name',
                                   target='target',
                                   eta=eta,
                                   transactional=True)
def SendRequestStateNotification(request_id, message):
  request = GetRequest(request_id)
  payload = zlib.compress(six.ensure_binary(protojson.encode_message(message)))  # pytype: disable=module-attr
  task_scheduler.AddTask(
      queue_name=common.OBJECT_EVENT_QUEUE, payload=payload, transactional=True)
  request.notify_state_change = False
  request.put()
  return request
 def Func():
     task_scheduler.AddTask(queue_name='queue_name',
                            payload='payload',
                            name='name',
                            target='target',
                            eta=eta,
                            transactional=True)
     raise ValueError()
def EnqueueCommandEvents(events):
  """Enqueue the command events to COMMAND_EVENT_QUEUE.

  Args:
    events: a list of command event dicts.
  """
  logging.info("Received %d command event(s).", len(events))
  for event in events:
    payload = zlib.compress(six.ensure_binary(json.dumps(event)))
    task_scheduler.AddTask(queue_name=COMMAND_EVENT_QUEUE, payload=payload)
def AddToQueue(request):
  """Adds the request to the request queue.

  Args:
    request: request entity, read only
  """
  task_name = str(request.key.id())
  payload = json.dumps({"id": request.key.id()})
  compressed_payload = zlib.compress(six.ensure_binary(payload))
  task_scheduler.AddTask(
      queue_name=REQUEST_QUEUE, name=task_name, payload=compressed_payload)
示例#7
0
def _AddRequestToQueue(request_id, countdown_secs=LONG_SYNC_COUNTDOWN_SECS):
  """Add a request to the sync queue."""
  payload = json.dumps({
      REQUEST_ID_KEY: request_id,
  })

  next_sync = common.Now() + datetime.timedelta(seconds=countdown_secs)
  logging.debug('Queueing request %s to be synced at %s', request_id, next_sync)
  task = task_scheduler.AddTask(
      queue_name=REQUEST_SYNC_QUEUE, payload=payload, eta=next_sync)
  logging.debug('Queued task: %s', task)
    def testAddTask(self):
        mock_task = mock.MagicMock()
        self.mock_add.return_value = mock_task

        task = task_scheduler.AddTask(queue_name='queue_name',
                                      payload='payload')

        self.mock_add.assert_called_once_with(queue_name='queue_name',
                                              payload='payload',
                                              task_name=None,
                                              eta=None,
                                              target=None)
        self.assertEqual(mock_task, task)
示例#9
0
def EnqueueTestRun(test_run_id):
  """Push the exist test run to the task queue.

  Args:
    test_run_id: a test run ID.
  """
  task_name = str(test_run_id)
  payload = json.dumps({
      'test_run_id': test_run_id
  })
  task_scheduler.AddTask(
      queue_name=TEST_KICKER_QUEUE,
      name=task_name,
      payload=payload,
      target='default')
示例#10
0
def ScheduleNextKick(cron_job, next_run_time=None):
  """Schedule a next kick for a cron job.

  Args:
    cron_job: a CronJob object.
    next_run_time: an optional next run time.
  """
  if not next_run_time:
    now = _GetCurrentTime()
    schedule = groctimespecification.GrocTimeSpecification(cron_job.schedule)
    next_run_time = schedule.GetMatches(now, 1)[0]
  cron_job.next_run_time = next_run_time
  task_scheduler.AddTask(
      queue_name=CRON_KICKER_QUEUE,
      payload=cron_job.ToJson(),
      eta=pytz.UTC.localize(cron_job.next_run_time))
def StartHostSync(hostname, current_host_sync_id=None):
  """Start host sync.

  Start host sync, if there is no host sync task or the host sync task is old.

  Args:
    hostname: hostname
    current_host_sync_id: unique id for current host sync that trigger
      this add back.
  Returns:
    the new host_sync_id or None if not added
  """
  host_sync = datastore_entities.HostSync.get_by_id(hostname)
  now = common.Now()
  stale_time = common.Now() - HOST_SYNC_STALE_TIMEOUT
  if (host_sync and host_sync.host_sync_id and
      host_sync.host_sync_id != current_host_sync_id and
      host_sync.update_timestamp and
      host_sync.update_timestamp >= stale_time):
    logging.debug(
        "Another host sync %s is already scheduled.",
        host_sync.host_sync_id)
    return None
  if not host_sync:
    host_sync = datastore_entities.HostSync(id=hostname)
  elif (host_sync.update_timestamp and
        host_sync.update_timestamp < stale_time):
    logging.info(
        "The old sync %s is inactive since %s.",
        host_sync.host_sync_id, host_sync.update_timestamp)
  host_sync.host_sync_id = uuid.uuid4().hex
  payload = json.dumps({
      HOSTNAME_KEY: hostname,
      HOST_SYNC_ID_KEY: host_sync.host_sync_id,
  })
  task = task_scheduler.AddTask(
      queue_name=HOST_SYNC_QUEUE,
      payload=payload,
      eta=common.Now() + HOST_SYNC_INTERVAL)
  # the taskname is for debugging purpose.
  host_sync.taskname = task.name
  host_sync.update_timestamp = now
  host_sync.put()
  logging.debug("Host will sync by task %s with host_sync_id %s.",
                task.name, host_sync.host_sync_id)
  return host_sync.host_sync_id
示例#12
0
def _SendEventMessage(encoded_message, pubsub_topic):
    """Sends a message to the event queue notifying a state change event.

  Args:
    encoded_message: proto-json encoded request or attempt state change message.
    pubsub_topic: pubsub topic to send the message to.
  Returns:
    True is the message was sent successfully, False otherwise
  """
    queue = env_config.CONFIG.event_queue_name
    if env_config.CONFIG.use_google_api:
        data = common.UrlSafeB64Encode(encoded_message)
        _PubsubClient.PublishMessages(pubsub_topic, [{'data': data}])
    elif queue:
        task_scheduler.AddTask(queue_name=queue,
                               payload=zlib.compress(encoded_message))
    else:
        logging.warning(
            'Unabled to notify events: use_google_api=False and queue is null')
示例#13
0
def ScheduleCronKick(test_plan_id, next_run_time=None):
    """Schedule a next cron kick for a test plan.

  Args:
    test_plan_id: a test plan ID.
    next_run_time: next run time, calculated based on cron_exp if not given.
  """
    test_plan, test_plan_status = _GetTestPlanAndStatus(test_plan_id)
    if not test_plan or not test_plan_status:
        return

    if not test_plan.cron_exp:
        logging.warning(
            'Cannot schedule next kick for test plan %s: cron_exp is None.',
            test_plan_id)
        _ClearNextRun(test_plan_status)
        return

    timezone = _ParseTimezone(test_plan.cron_exp_timezone)
    if not timezone:
        logging.warning(
            'Cannot schedule next kick for test plan %s: unknown timezone %s.',
            test_plan_id, test_plan.cron_exp_timezone)
        _ClearNextRun(test_plan_status)
        return

    if not next_run_time:
        next_run_time = _GetNextRunTime(test_plan.cron_exp, timezone)
    test_plan_status.next_run_time = next_run_time
    payload = json.dumps({'test_plan_id': test_plan_id})
    task = task_scheduler.AddTask(queue_name=TEST_PLAN_KICKER_QUEUE,
                                  payload=payload,
                                  eta=pytz.UTC.localize(
                                      test_plan_status.next_run_time))
    test_plan_status.next_run_task_name = task.name
    test_plan_status.put()
    logging.info('Scheduled the next kick task %s (eta=%s)', task.name,
                 task.eta)
示例#14
0
def Log(category, action, **kwargs):
    """Schedules a task to upload an event to GA."""
    payload = json.dumps(dict(category=category, action=action, **kwargs))
    task_scheduler.AddTask(queue_name=QUEUE_NAME, payload=payload)