n0nag0n / simple-job-queue

A simple library for interfacing with other job queue providers that gives you plenty of flexibility. Currently supports MySQL, SQLite, and Beanstalkd.
27 stars 6 forks source link

handling db disconnection issues #6

Closed alansaviolobo closed 3 months ago

alansaviolobo commented 5 months ago

I came across a situation where the db connection would break and that exception/error would cause the worker to crash. to over come that, I have a situation involving a loop inside a loop. its a bit wierd so wanted a second opinion on the same.

the example worker script would also have to be updated accordingly


      try {
        $starttime = microtime(true);
        $PDO = new PDO($dsn);
        $Job_Queue = new Job_Queue('pgsql', ['pgsql' => ['table_name' => 'tankertasks']]);
        $Job_Queue->addQueueConnection($PDO);
        $Job_Queue->watchPipeline('tracker');
        while (true) {
          $job = $Job_Queue->getNextJobAndReserve();

          if (empty($job)) {
            echo '.';
            usleep(500 * 1000);
            continue;
          }

          try {
            $payload = json_decode($job['payload'], true);
            if (class_exists($payload['gpsProvider'])) {
              new $payload['gpsProvider']($payload['tanker'], $payload['cacheKey']);
              $Job_Queue->deleteJob($job);
            }
          } catch (Exception $e) {
            $Job_Queue->buryJob($job);
          }
        }
      } catch (Exception $e) {
       // error handlng here
      }
    }```
alansaviolobo commented 5 months ago

another issue is that all my jobs appear to get burried and not deleted.

n0nag0n commented 5 months ago

In your code, my guess is that the exception is thrown and the traceroute bubbles up to this line? Can you confirm?

$job = $Job_Queue->getNextJobAndReserve();
n0nag0n commented 3 months ago

Ping

alansaviolobo commented 3 months ago

Ping

apologies. yes. the catch was causing the jobs to be buried which was creating a huge backlog. have replaced the line with a $Job_Queue->deleteJob($job); since the job is redundant if not run immediately.

n0nag0n commented 3 months ago

Cool cool, glad it's fixed!