0

TL;DR

Can't dispatch jobs in Laravel with milliseconds delay, since available_at column is in unix timestamp format and doesn't support milliseconds.

I use database driver for jobs and wanted to delay job dispatching. My algorithm requires me to dispatch jobs with milliseconds delay (say 100ms for example). In the documentation it is demonstrated with ->delay(now()->addMinutes(10));. Also i noticed that job table's available at column is an integer column which is designated to be filled with unix timestamp.

When i dispatch my jobs with:

MyJob::dispatch($jobSpecs)->delay(now()->addMilliseconds(1000));

I can see that jobs differ in available_at values. Difference is 1 as in seconds as expected. However when i try to dispatch with 100ms delay like so ->addMilliseconds(100) i do not see any difference in available_at column value. I think it is an expected result since unix timestamp doesn't support milliseconds.

It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970

Therefore i am unable to dispatch with milliseconds delay. It actually dispatches just fine, but delay is not reflected to available_at column.

How can i workaround it ?

Thank you

Skywarth
  • 623
  • 2
  • 9
  • 26
  • 1
    Not sure how accurate you want the delay, but if a job is in a queue - there is no control over when it's executed. It is more a case of it not being executed before that delay. – Nigel Ren Nov 11 '21 at 08:07
  • 1
    Even if there are no database column restrictions (you really should be using a different queue driver by the way). Queued jobs generally not precisely invoked when you want them to because there needs to be an available queue worker which decides to handle a job at that precise moment. Queue workers generally can process one job at a time and tend to also sleep for some time before handling the next job – apokryfos Nov 11 '21 at 08:37
  • @NigelRen AFAIK it is possible to control when a job in queue will be executed through `available_at`. Worker's won't touch the job until job's `available_at`<=now() – Skywarth Nov 11 '21 at 12:13
  • 1
    But this doesn't mean it will be executed then, if the queue is busy it could take minutes or hours to be executed. If it's such a short delay, you may be better off not queueing it. – Nigel Ren Nov 11 '21 at 12:16

0 Answers0