Remove pending duplicate jobs, no need to run twice the same job

@return void
342276
public function removePendingJobDuplicates()
    {
        $pdo = $this->getEntityManager()->getPDO();

        $query = "
            SELECT scheduled_job_id
            FROM job
            WHERE
                scheduled_job_id IS NOT NULL AND
                `status` = '".CronManager::PENDING."' AND
                execute_time <= '".date('Y-m-d H:i:s')."' AND
                target_id IS NULL AND
                deleted = 0
            GROUP BY scheduled_job_id
            HAVING count( * ) > 1
            ORDER BY MAX(execute_time) ASC
        ";
        $sth = $pdo->prepare($query);
        $sth->execute();

        $duplicateJobList = $sth->fetchAll(PDO::FETCH_ASSOC);

        foreach ($duplicateJobList as $row) {
            if (!empty($row['scheduled_job_id'])) {

                $query = "
                    SELECT id FROM `job`
                    WHERE
                        scheduled_job_id = ".$pdo->quote($row['scheduled_job_id'])."
                        AND `status` = '" . CronManager::PENDING ."'
                        ORDER BY execute_time
                        DESC LIMIT 1, 100000
                    ";
                $sth = $pdo->prepare($query);
                $sth->execute();
                $jobIdList = $sth->fetchAll(PDO::FETCH_COLUMN);

                if (empty($jobIdList)) {
                    continue;
                }

                $quotedJobIdList = [];
                foreach ($jobIdList as $jobId) {
                    $quotedJobIdList[] = $pdo->quote($jobId);
                }

                $update = "
                    UPDATE job
                    SET deleted = 1
                    WHERE
                        id IN (".implode(", ", $quotedJobIdList).")
                ";

                $sth = $pdo->prepare($update);
                $sth->execute();
            }
        }
    }
41594
public function reset()
    {
        $ResqueStatus = $this->ResqueStatus;

        $this->debug(__d('cake_resque', 'Emptying the worker database'));
        $ResqueStatus->clearWorkers();
        $this->debug(__d('cake_resque', 'Unregistering the scheduler worker'));
        $ResqueStatus->unregisterSchedulerWorker();
        $this->out('<success>' . __d('cake_resque', 'CakeResque state has been reseted') . '</success>');
    }
333976
public function process()
    {
        foreach ($this->jobs as $id => $job) {
            // get the job name
            $name = ucfirst($job['type']);
            // process the job
            $this->{"process{$name}"}($job);
            // remove it from the processing queue
            unset($this->jobs[$id]);
        }
    }
314431
def flush_jobs
      # Get all the jobs, one at a time, pulling from the list
      while job = self.cached_jobs.shift
        # Map the array to the right things
        job_id, args = *job
        # Submit! takes care of rescuing, error logging, etc and never caches
        submit! args, job_id
      end
      self
    end
1230693
protected function job(): void
    {
        while (true) {

            $folders = $this->retrieve([
                'corporation_id' => $this->getCorporationId(),
            ]);

            if ($folders->isCachedLoad()) return;

            collect($folders)->each(function ($folder) {

                CorporationBookmarkFolder::firstOrNew([
                    'corporation_id' => $this->getCorporationId(),
                    'folder_id'      => $folder->folder_id,
                ])->fill([
                    'name'       => $folder->name,
                    'creator_id' => $folder->creator_id ?? null,
                ])->save();
            });

            $this->known_folder_ids->push(collect($folders)
                ->pluck('folder_id')->flatten()->all());

            if (! $this->nextPage($folders->pages))
                break;
        }

        // Cleanup removed folders
        CorporationBookmarkFolder::where('corporation_id', $this->getCorporationId())
            ->whereNotIn('folder_id', $this->known_folder_ids->flatten()->all())
            ->delete();
    }
877359
protected function job(): void
    {
        $limit = $this->retrieve([
            'corporation_id' => $this->getCorporationId(),
        ]);

        if ($limit->isCachedLoad()) return;

        if (! property_exists($limit, 'scalar'))
            return;

        CorporationMemberLimits::firstOrNew([
            'corporation_id' => $this->getCorporationId(),
        ])->fill([
            'limit' => $limit->scalar,
        ])->save();
    }
80568
public function clearUnPublishJob()
    {
        // Cancel any in-progress unpublish job
        $job = $this->owner->UnPublishJob();
        if ($job && $job->exists()) {
            $job->delete();
        }
        $this->owner->UnPublishJobID = 0;
    }
409197
public function removeProductDuplicates() {
    $this->output->writeln(dt('Removing duplicates in commerce products, please wait...'));

    $skus_to_sync = [];

    /** @var \Drupal\node\NodeStorageInterface $nodeStorage */
    $nodeStorage = $this->entityTypeManager->getStorage('node');

    $query = $this->connection->select('acm_sku_field_data', 't1');
    $query->addField('t1', 'id', 'id');
    $query->addField('t1', 'sku', 'sku');
    $query->leftJoin('acm_sku_field_data', 't2', 't1.sku = t2.sku');
    $query->where('t1.id != t2.id');
    $result = $query->execute()->fetchAllKeyed(0, 1);

    if (empty($result)) {
      $this->output->writeln(dt('No duplicate skus found.'));
    }
    else {
      $skus = [];

      foreach ($result as $id => $sku) {
        $skus[$sku][$id] = $id;
        $skus_to_sync[$sku] = $sku;
      }

      foreach ($skus as $sku => $ids) {
        $this->output->writeln(dt('Duplicate skus found for sku: @sku with ids: @ids.', [
          '@sku' => $sku,
          '@ids' => implode(', ', $ids),
        ]));

        // Always delete the one with higher id, first one will have more
        // translations.
        sort($ids);

        // Remove the first id which we don't want to delete.
        array_shift($ids);

        foreach ($ids as $id) {
          $this->output->writeln(dt('Deleting sku with id @id for sku @sku.', [
            '@sku' => $sku,
            '@id' => $id,
          ]));

          $sku_entity = SKU::load($id);
          $sku_entity->delete();
        }
      }
    }

    $query = $this->connection->select('node__' . $this->skuFieldName, 't1');
    $query->addField('t1', 'entity_id', 'id');
    $query->addField('t1', $this->skuFieldName . '_value', 'sku');
    $query->leftJoin('node__' . $this->skuFieldName, 't2', 't1.' . $this->skuFieldName . '_value = t2.' . $this->skuFieldName . '_value');
    $query->where('t1.entity_id != t2.entity_id');
    $result = $query->execute()->fetchAllKeyed(0, 1);

    if (empty($result)) {
      $this->output->writeln(dt('No duplicate product nodes found.'));
    }
    else {
      $nids_to_delete = [];
      $skus = [];

      foreach ($result as $id => $sku) {
        $skus[$sku][$id] = $id;
        $skus_to_sync[$sku] = $sku;
      }

      foreach ($skus as $sku => $ids) {
        $this->output->writeln(dt('Duplicate nodes found for sku: @sku with ids: @ids.', [
          '@sku' => $sku,
          '@ids' => implode(', ', $ids),
        ]));

        // Always delete the one with higher nid, first one will have proper
        // url alias.
        sort($ids);

        // Remove the first id which we don't want to delete.
        array_shift($ids);

        foreach ($ids as $id) {
          $this->output->writeln(dt('Deleting node with id @id for sku @sku.', [
            '@sku' => $sku,
            '@id' => $id,
          ]));

          $nids_to_delete[$id] = $id;
        }
      }

      if ($nids_to_delete) {
        $nodeStorage->delete($nodeStorage->loadMultiple($nids_to_delete));
      }
    }

    if ($skus_to_sync) {
      $sku_texts = implode(',', $skus_to_sync);

      $this->output->writeln(dt('Requesting resync for skus @skus.', [
        '@skus' => $sku_texts,
      ]));

      foreach ($this->i18nhelper->getStoreLanguageMapping() as $langcode => $store_id) {
        // Using very small page size to avoid any issues for skus which already
        // had corrupt data.
        $this->ingestApiWrapper->productFullSync($store_id, $langcode, $sku_texts, NULL, 5);
      }
    }

    $this->output->writeln(dt('Done.'));
  }
378178
def clean_jobs(self, recursive=False):
        """"""
        self._interface.clean_jobs(self.scatter_link,
                                   clean_all=recursive)
674
protected function job(): void
    {
        CorporationDivision::where('corporation_id', $this->getCorporationId())->get()
            ->each(function ($division) {

                // retrieve last known entry for the current division and active corporation
                $last_known_entry = CorporationWalletJournal::where('corporation_id', $this->getCorporationId())
                                                            ->where('division', $division->division)
                                                            ->orderBy('date', 'desc')
                                                            ->first();

                $this->last_known_entry_id = is_null($last_known_entry) ? 0 : $last_known_entry->id;

                // Perform a journal walk backwards to get all of the
                // entries as far back as possible. When the response from
                // ESI is empty, we can assume we have everything.
                while (true) {

                    $journal = $this->retrieve([
                        'corporation_id' => $this->getCorporationId(),
                        'division'       => $division->division,
                    ]);

                    if ($journal->isCachedLoad()) return;

                    $entries = collect($journal);

                    // If we have no more entries, break the loop.
                    if ($entries->count() === 0)
                        break;

                    $entries->chunk(1000)->each(function ($chunk) use ($division) {

                        // if we've reached the last known entry - abort the process
                        if ($this->at_last_entry)
                            return false;

                        // if we have reached the last known entry, exclude all entries which are lower or equal to the
                        // last known entry and flag the reached status.
                        if ($chunk->where('id', $this->last_known_entry_id)->isNotEmpty()) {
                            $chunk = $chunk->where('id', '>', $this->last_known_entry_id);

                            $this->at_last_entry = true;
                        }

                        $records = $chunk->map(function ($entry, $key) use ($division) {

                            return [
                                'corporation_id'  => $this->getCorporationId(),
                                'division'        => $division->division,
                                'id'              => $entry->id,
                                'date'            => carbon($entry->date),
                                'ref_type'        => $entry->ref_type,
                                'first_party_id'  => $entry->first_party_id ?? null,
                                'second_party_id' => $entry->second_party_id ?? null,
                                'amount'          => $entry->amount ?? null,
                                'balance'         => $entry->balance ?? null,
                                'reason'          => $entry->reason ?? null,
                                'tax_receiver_id' => $entry->tax_receiver_id ?? null,
                                'tax'             => $entry->tax ?? null,
                                // introduced in v4
                                'description'     => $entry->description,
                                'context_id'      => $entry->context_id ?? null,
                                'context_id_type' => $entry->context_id_type ?? null,
                                'created_at'      => carbon(),
                                'updated_at'      => carbon(),
                            ];
                        });

                        CorporationWalletJournal::insertIgnore($records->toArray());
                    });

                    // in case the last known entry has been reached or we non longer have pages, terminate the job.
                    if (! $this->nextPage($journal->pages) || $this->at_last_entry)
                        break;
                }

                // Reset the page for the next wallet division.
                $this->page = 1;

                // Reset the last known entry for the next wallet division.
                $this->last_known_entry_id = 0;

                // Reset the last known entry status for the next wallet division.
                $this->at_last_entry = false;
            });
    }
488285
public function clean()
    {
        $jobFiles = glob($this->getLocation() . '*.js');
        foreach ($jobFiles as $file) {
            unlink($file);
        }
    }
81455
public function delete()
    {
        parent::delete();

        if (isset($this->job->pushed)) {
            return;
        }

        $this->iron->deleteMessage($this->getQueue(), $this->job->id, $this->job->reservation_id);
    }
737460
def stop_jobs(self, job_ids=None):
    """"""
    self.lock()

    jobs = self.get_jobs(job_ids)
    for job in jobs:
      if job.status in ('executing', 'queued', 'waiting') and job.queue_name == 'local':
        logger.info("Reset job '%s' (%s) in the database", job.name, self._format_log(job.id))
        job.submit()

    self.session.commit()
    self.unlock()
840233
protected function job(): void
    {
        $divisions = $this->retrieve([
            'corporation_id' => $this->getCorporationId(),
        ]);

        if ($divisions->isCachedLoad()) return;

        if (property_exists($divisions, 'hangar'))

            collect($divisions->hangar)->each(function ($hangar) {

                CorporationDivision::firstOrNew([
                    'corporation_id' => $this->getCorporationId(),
                    'type'           => 'hangar',
                    'division'       => $hangar->division,
                ])->fill([
                    'name' => $hangar->name ?? null,
                ])->save();
            });

        if (property_exists($divisions, 'wallet'))

            collect($divisions->wallet)->each(function ($wallet) {

                CorporationDivision::firstOrNew([
                    'corporation_id' => $this->getCorporationId(),
                    'type'           => 'wallet',
                    'division'       => $wallet->division,
                ])->fill([
                    'name' => $wallet->name ?? null,
                ])->save();
            });
    }
570616
public function reserve()
    {
        $queues = $this->queues();
        if(!is_array($queues)) {
            return;
        }

        foreach($queues as $queue) {
            $this->log('Checking ' . $queue, self::LOG_VERBOSE);
            $job = Job::reserve($queue);
            if($job) {
                $this->log('Found job on ' . $queue, self::LOG_VERBOSE);
                return $job;
            }
        }

        return false;
    }
1094008
protected function _duplicate()
    {
        $this->dupDocComment();
        $this->setReturnType($this->node->returnType);
        $this->addParams($this->node->params);
        $this->addStmts($this->node->stmts);
    }
14374
public function flush()
    {
        //todo bugfix batch record
        foreach ($this->deferLogs as $k => $deferLog) {
            $this->record($deferLog['message'], $deferLog['context'], $deferLog['level'], $deferLog['role']);
            $this->removeDeferLog($k);
        }
    }