# sync-basalam/1.8.6/JobsRunner.php

ووسلام – همگام سازی ووکامرس و باسلام, version 1.8.6. 88 lines.

- Page: https://pluginprobe.com/plugins/sync-basalam/1.8.6/code/JobsRunner.php
- Raw: https://pluginprobe.com/plugins/sync-basalam/1.8.6/raw/JobsRunner.php
- Modified: 2026-05-10T15:06:26+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sync-basalam/1.8.6/code/JobsRunner.php#L10-L20`.

```php
<?php

namespace SyncBasalam;

use SyncBasalam\Admin\Settings;
use SyncBasalam\Services\Api\CircuitBreaker;

defined('ABSPATH') || exit;

class JobsRunner
{
    private $jobExecutor;
    private $jobManager;
    private $discountScheduler;
    private $CheckHttpBlockService;

    public function __construct(
        $jobManager,
        $jobExecutor,
        $discountScheduler,
        $CheckHttpBlockService
    )
    {
        add_action('init', [$this, 'checkAndRunJobs']);
        $this->jobManager = $jobManager;
        $this->jobExecutor = $jobExecutor;
        $this->discountScheduler = $discountScheduler;
        $this->CheckHttpBlockService = $CheckHttpBlockService;
    }

    public function checkAndRunJobs(): void
    {
        if($this->CheckHttpBlockService->SyncBasalamHttpBlock()) return;
        $this->jobManager->ConvertStaleProcessingJobs(120);
        $this->discountScheduler->process();

        $circuitBreaker = new CircuitBreaker();
        if ($circuitBreaker->getState() === CircuitBreaker::STATE_OPEN) {
            return;
        }

        $tasksPerMinute = max(1, intval(Settings::getEffectiveTasksPerMinute()));
        $thresholdSeconds = 60.0 / $tasksPerMinute;

        $sortedJobTypes = $this->jobExecutor->getSortedJobTypes();

        foreach ($sortedJobTypes as $jobType => $jobExecutor) {
            if (!$this->jobExecutor->acquireLock($jobType, 0)) continue;
            try {
                $lastRun = floatval(get_option($jobType . '_last_run', 0));
                $now = microtime(true);

                if (($now - $lastRun) >= $thresholdSeconds) {
                    if (!$this->jobExecutor->canRun($jobType)) {
                        continue;
                    }

                    $job = $this->jobManager->getNextEligibleJob($jobType);
                    $processingJob = $this->jobManager->getJob(['job_type' => $jobType, 'status' => 'processing']);

                    if ($job && !$processingJob) {
                        update_option($jobType . '_last_run', microtime(true), false);

                        $this->jobManager->updateJob(
                            ['status' => 'processing', 'started_at' => time()],
                            ['id' => $job->id]
                        );

                        $this->jobExecutor->releaseLock($jobType);

                        $this->executeJob($job);

                        break;
                    }
                }
            } finally {
                $this->jobExecutor->releaseLock($jobType);
            }
        }
    }

    private function executeJob(object $job): void
    {
        $jobType = $job->job_type;
        $this->jobExecutor->execute($jobType, $job);
    }
}

```
