Skip to content

sleep not working in horizon queue? #1528

Open
@ellermister

Description

@ellermister

Horizon Version

5.30.2

Laravel Version

10.10

PHP Version

8.2.22

Redis Driver

PhpRedis

Redis Version

6.0.2

Database Driver & Version

No response

Description

sleep 30s, actually executed in 2s.(many times)
sleep is invalid in horizon, but normal in queue:work.

Steps To Reproduce

TestLongTimeJob.php

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Sleep;
use Log;

class TestLongTimeJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(public $type = 'default')
    {
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        Log::info('TestLongTimeJob start ' . $this->type);
        echo "TestLongTimeJob start " . $this->type . "\n";

        if ($this->type == 'default') {
            sleep(30);
        } else if ($this->type == 'redis') {
            redis()->blpop('test', 30);
        }

        echo 'TestLongTimeJob end ' . $this->type;
        Log::info('TestLongTimeJob end ' . $this->type);
    }
}

TestJob.php

<?php

namespace App\Console\Commands;

use App\Jobs\TestLongTimeJob;
use Illuminate\Console\Command;

class TestJob extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:job {type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        dispatch(new TestLongTimeJob($this->argument('type')));
    }
}

terminal 1, start horizon

php artisan horizon

terminal 2, dispatch sleep job, simulate long tasks.

php artisan test:job default
...

test output 1

root@1dbba1977a4d:/workspaces# php artisan horizon

   INFO  Horizon started successfully.  

  2025-01-22 14:26:04 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:26:09 App\Jobs\TestLongTimeJob ....................... 5s DONE
^[[D^[[D^C

   INFO  Shutting down.  

root@1dbba1977a4d:/workspaces# php artisan horizon

   INFO  Horizon started successfully.  

  2025-01-22 14:26:22 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:26:52 App\Jobs\TestLongTimeJob ...................... 30s DONE

^C

   INFO  Shutting down.  

root@1dbba1977a4d:/workspaces# php artisan horizon

   INFO  Horizon started successfully.  

  2025-01-22 14:27:09 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:27:39 App\Jobs\TestLongTimeJob ...................... 30s DONE

  2025-01-22 14:27:45 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:28:15 App\Jobs\TestLongTimeJob ...................... 30s DONE
  2025-01-22 14:28:21 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:28:23 App\Jobs\TestLongTimeJob ....................... 2s DONE
  2025-01-22 14:28:30 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:29:00 App\Jobs\TestLongTimeJob ...................... 30s DONE
  2025-01-22 14:30:42 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:30:44 App\Jobs\TestLongTimeJob ....................... 1s DONE
  2025-01-22 14:30:52 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:31:22 App\Jobs\TestLongTimeJob ...................... 30s DONE
^C

   INFO  Shutting down.  

test output 2

root@1dbba1977a4d:/workspaces# php artisan horizon

   INFO  Horizon started successfully.  

  2025-01-22 14:39:40 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:39:42 App\Jobs\TestLongTimeJob ....................... 2s DONE
  2025-01-22 14:39:49 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:39:51 App\Jobs\TestLongTimeJob ....................... 2s DONE
  2025-01-22 14:39:58 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:40:00 App\Jobs\TestLongTimeJob ....................... 2s DONE
  2025-01-22 14:40:07 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:40:37 App\Jobs\TestLongTimeJob ...................... 30s DONE
  2025-01-22 14:40:43 App\Jobs\TestLongTimeJob ....................... RUNNING
TestLongTimeJob start default
TestLongTimeJob end default  2025-01-22 14:41:13 App\Jobs\TestLongTimeJob ...................... 30s DONE
^C

   INFO  Shutting down.  

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions