Makes leased cron jobs available for leasing again.
975218
def ReturnLeasedCronJobs(self, jobs, cursor=None):
    """"""
    if not jobs:
      return

    unleased_jobs = []

    conditions = []
    args = []
    for job in jobs:
      if not job.leased_by or not job.leased_until:
        unleased_jobs.append(job)
        continue

      conditions.append("""
      (job_id = %s AND leased_until = FROM_UNIXTIME(%s) AND leased_by = %s)
      """)
      args += [
          job.cron_job_id,
          mysql_utils.RDFDatetimeToTimestamp(job.leased_until), job.leased_by
      ]

    if conditions:
      query = ("UPDATE cron_jobs "
               "SET leased_until=NULL, leased_by=NULL "
               "WHERE ") + " OR ".join(conditions)
      returned = cursor.execute(query, args)

    if unleased_jobs:
      raise ValueError("CronJobs to return are not leased: %s" % unleased_jobs)
    if returned != len(jobs):
      raise ValueError("%d cronjobs in %s could not be returned." %
                       ((len(jobs) - returned), jobs))
1221969
def recurring(self, offset=0, count=25):
        ''''''
        return self.client('jobs', 'recurring', self.name, offset, count)
683760
def EnableCronJob(self, cronjob_id):
    """"""
    job = self.cronjobs.get(cronjob_id)
    if job is None:
      raise db.UnknownCronJobError("Cron job %s not known." % cronjob_id)
    job.enabled = True
788286
def create_tenant(self, tenant_id):
        """"""
        t_res = MechResource(tenant_id, a_const.TENANT_RESOURCE,
                             a_const.CREATE)
        self.provision_queue.put(t_res)
398316
def revoke_lease(self, lease_id):
        
        lease_revoke_request = etcdrpc.LeaseRevokeRequest(ID=lease_id)
        self.leasestub.LeaseRevoke(
            lease_revoke_request,
            self.timeout,
            credentials=self.call_credentials,
            metadata=self.metadata
        )
14639
def ResolveCronJobFlowURN(self, cron_job_id):
    """"""
    if not self._value:
      raise ValueError("Can't call ResolveCronJobFlowURN on an empty "
                       "client id.")

    return cron_job_id.ToURN().Add(self._value)
903701
def LeaseClientActionRequests(self,
                                client_id,
                                lease_time=None,
                                limit=None,
                                cursor=None):
    """"""

    now = rdfvalue.RDFDatetime.Now()
    now_str = mysql_utils.RDFDatetimeToTimestamp(now)
    expiry = now + lease_time
    expiry_str = mysql_utils.RDFDatetimeToTimestamp(expiry)
    proc_id_str = utils.ProcessIdString()
    client_id_int = db_utils.ClientIDToInt(client_id)

    query = ("UPDATE client_action_requests "
             "SET leased_until=FROM_UNIXTIME(%s), leased_by=%s, "
             "leased_count=leased_count+1 "
             "WHERE client_id=%s AND "
             "(leased_until IS NULL OR leased_until < FROM_UNIXTIME(%s)) "
             "LIMIT %s")
    args = [expiry_str, proc_id_str, client_id_int, now_str, limit]

    num_leased = cursor.execute(query, args)
    if num_leased == 0:
      return []

    query = ("SELECT request, leased_count FROM client_action_requests "
             "WHERE client_id=%s AND leased_until=FROM_UNIXTIME(%s) "
             "AND leased_by=%s")

    cursor.execute(query, [client_id_int, expiry_str, proc_id_str])

    ret = []
    expired = []
    for req, leased_count in cursor.fetchall():
      request = rdf_flows.ClientActionRequest.FromSerializedString(req)
      request.leased_by = proc_id_str
      request.leased_until = expiry
      request.ttl = db.Database.CLIENT_MESSAGES_TTL - leased_count
      # > comparison since this check happens after the lease.
      if leased_count > db.Database.CLIENT_MESSAGES_TTL:
        expired.append((request.client_id, request.flow_id, request.request_id))
      else:
        ret.append(request)

    if expired:
      self._DeleteClientActionRequest(expired, cursor=cursor)

    return sorted(ret, key=lambda req: (req.flow_id, req.request_id))
1123047
private Lease lease(@Nonnull FilePath p) {
        return new Lease(p) {
            final AtomicBoolean released = new AtomicBoolean();
            public void release() {
                _release(path);
            }
            @Override public void close() {
                if (released.compareAndSet(false, true)) {
                    release();
                }
            }
        };
    }
1222997
def create(self, key, value, lease='1h'):
        
        return self._client.write(key, value, lease=lease)
821689
def with_source(self, lease):
        
        super().with_source(lease)
        self.offset = lease.offset
        self.sequence_number = lease.sequence_number
277480
def run_once(self):
        ''''''

        self._update_projects()
        self._check_task_done()
        self._check_request()
        while self._check_cronjob():
            pass
        self._check_select()
        self._check_delete()
        self._try_dump_cnt()
1030546
public function initCronJobs()
    {
        //Create duplciate users cron
        add_action('admin_init', function () {
            if (is_main_site() && !wp_next_scheduled('ad_integration_cleaning_duplicate_users')) {
                wp_schedule_event((strtotime("midnight") + (60*60*4)), 'daily', 'ad_integration_cleaning_duplicate_users');
            }
        });

        //Create orphan meta cron
        add_action('admin_init', function () {
            if (is_main_site() && !wp_next_scheduled('ad_integration_cleaning_orphan_meta')) {
                wp_schedule_event((strtotime("midnight") + (60*60*5)), 'daily', 'ad_integration_cleaning_orphan_meta');
            }
        });

        //Create empty cap cron
        add_action('admin_init', function () {
            if (!wp_next_scheduled('ad_integration_cleaning_capabilities')) {
                wp_schedule_event((strtotime("midnight") + (60*60*6)), 'daily', 'ad_integration_cleaning_capabilities');
            }
        });
    }
574199
def attempt_renew_lease(self, lease_task, owned_by_others_q, lease_manager):
        
        loop = asyncio.new_event_loop()
        loop.run_until_complete(self.attempt_renew_lease_async(lease_task, owned_by_others_q, lease_manager))
859454
def requeue(rq, ctx, all, job_ids):
    ""
    return ctx.invoke(
        rq_cli.requeue,
        all=all,
        job_ids=job_ids,
        **shared_options(rq)
    )
297756
async def run_loop_async(self):
        
        while not self.cancellation_token.is_cancelled:
            lease_manager = self.host.storage_manager
            # Inspect all leases.
            # Acquire any expired leases.
            # Renew any leases that currently belong to us.
            getting_all_leases = await lease_manager.get_all_leases()
            leases_owned_by_others_q = Queue()
            renew_tasks = [
                self.attempt_renew_lease_async(
                    get_lease_task,
                    owned_by_others_q=leases_owned_by_others_q,
                    lease_manager=lease_manager)
                for get_lease_task in getting_all_leases]
            await asyncio.gather(*renew_tasks)

            # Extract all leasees leases_owned_by_others and our_lease_count from the
            all_leases = {}
            leases_owned_by_others = []
            our_lease_count = 0
            while not leases_owned_by_others_q.empty():
                lease_owned_by_other = leases_owned_by_others_q.get()
                # Check if lease is owned by other and append
                if lease_owned_by_other[0]:
                    leases_owned_by_others.append(lease_owned_by_other[1])
                else:
                    our_lease_count += 1
                all_leases[lease_owned_by_other[1].partition_id] = lease_owned_by_other[1]

            # Grab more leases if available and needed for load balancing
            leases_owned_by_others_count = len(leases_owned_by_others)
            if leases_owned_by_others_count > 0:
                steal_this_lease = self.which_lease_to_steal(
                    leases_owned_by_others, our_lease_count)
                if steal_this_lease:
                    try:
                        _logger.info("Lease to steal %r", steal_this_lease.serializable())
                        if await lease_manager.acquire_lease_async(steal_this_lease):
                            _logger.info("Stole lease sucessfully %r %r",
                                         self.host.guid, steal_this_lease.partition_id)
                        else:
                            _logger.info("Failed to steal lease for partition %r %r",
                                         self.host.guid, steal_this_lease.partition_id)
                    except Exception as err:  # pylint: disable=broad-except
                        _logger.error("Failed to steal lease %r", err)

            for partition_id in all_leases:
                try:
                    updated_lease = all_leases[partition_id]
                    if updated_lease.owner == self.host.host_name:
                        _logger.debug("Attempting to renew lease %r %r",
                                      self.host.guid, partition_id)
                        await self.check_and_add_pump_async(partition_id, updated_lease)
                    else:
                        _logger.debug("Removing pump due to lost lease.")
                        await self.remove_pump_async(partition_id, "LeaseLost")
                except Exception as err:  # pylint: disable=broad-except
                    _logger.error("Failed to update lease %r", err)
            await asyncio.sleep(lease_manager.lease_renew_interval)
422690
public function scheduleWork()
    {
        if (is_null($this->scheduler)) {
            return;
        }

        $this->scheduler->reconnect();

        foreach ($this->scheduler->getJobsFor(new \DateTime()) as $job) {
            $this->pipeline->enqueue($job);
        }
    }
1054701
def _QueueQueryAndOwn(self,
                        subject,
                        lease_seconds=100,
                        limit=1,
                        timestamp=None):
    """"""
    tasks = []

    lease = int(lease_seconds * 1e6)

    # Only grab attributes with timestamps in the past.
    delete_attrs = set()
    serialized_tasks_dict = {}
    for predicate, task, timestamp in DB.ResolvePrefix(
        subject,
        DataStore.QUEUE_TASK_PREDICATE_PREFIX,
        timestamp=(0, timestamp or rdfvalue.RDFDatetime.Now())):
      task = rdf_flows.GrrMessage.FromSerializedString(task)
      task.leased_until = timestamp
      task.leased_by = utils.ProcessIdString()
      # Decrement the ttl
      task.task_ttl -= 1
      if task.task_ttl <= 0:
        # Remove the task if ttl is exhausted.
        delete_attrs.add(predicate)
        stats_collector_instance.Get().IncrementCounter(
            "grr_task_ttl_expired_count")
      else:
        if task.task_ttl != rdf_flows.GrrMessage.max_ttl - 1:
          stats_collector_instance.Get().IncrementCounter(
              "grr_task_retransmission_count")

        serialized_tasks_dict.setdefault(predicate,
                                         []).append(task.SerializeToString())
        tasks.append(task)
        if len(tasks) >= limit:
          break

    if delete_attrs or serialized_tasks_dict:
      # Update the timestamp on claimed tasks to be in the future and decrement
      # their TTLs, delete tasks with expired ttls.
      self.MultiSet(
          subject,
          serialized_tasks_dict,
          replace=True,
          timestamp=int(time.time() * 1e6) + lease,
          to_delete=delete_attrs)

    if delete_attrs:
      logging.info("TTL exceeded for %d messages on queue %s",
                   len(delete_attrs), subject)
    return tasks