Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Add the ability for Jobs to be watched after being dispatched #166

@kharysharpe

Description

@kharysharpe

Ability for jobs to be watched and their state determined while being able to receive basic messages from the running job, independent of the underlying queue system.

Theory of Operation QueueWatcher, QueueWatchable

  • Uses a model to perform actions on the datastore, set in queue config. (could be a database, nosql, etc). job_watch: id:string, termination_requested:bool(F), time_started:int, time_completed:int, message:text, status:string
  • Create simple database based model and migration script
  • Jobs uses a QueueWatchable Trait
  • Jobs can pingWatcher()
  • Jobs can notifyWatcher($message)
  • Use Queue::before to QueueWatchable::markJobStarted()
  • Use Queue::after to QueueWatchable::markJobCompleted()
  • Use InteractsWithQueue to markJob{Status}( ) or implement within QueueWatchable and override the behaviour we want

Trait QueueWatchable:

  • setQueueWatchId($id = null): Returns a unique identifier for job
  • getQueueWatchId(): Returns a unique identifier for job
  • markJobStarted(): sets the time job commenced
  • markJobCompleted(): sets the time job completed
  • markJobReleased(): sets the time job commenced
  • pingWatcher(): Updates the time the job was last seen
  • notifyWatcher($message): Sets a message that can be retrieved
  • terminateJob(): Returns if the job should be terminated....not so sure about this.

class QueueWatcher

  • getLastSeen($id): Returns the last timestamp seen
  • getDuration($id): Returns how long the job has been running.
  • getNotification($id): Returns message set by job
  • getStatus($id, $clearFinished = true): Returns WAITING, RUNNING, FINISHED states. Removes Finished Jobs for Watch List
  • requestTermination($id): Send a kill request ...should we do this?
class FooJob extends Job
{
  use QueueWatchable;

  function __construct($data)
  {
      $this->setQueueWatchId();

      $this->data = $data;

  }

  function handle()
  {

    while($running) {

      //Do whatever

      $this->notifyWatcher('Doing bar in foo job');

      $this->pingWatcher();

      if ($this->terminateJob())
      {
        //Cleanup and exit

      }

    }

  }


}
  $job = new FooJob($data);

  $id = $job->getQueueWatchId();

  dispatch($job);

  $seconds = QueueWatch::getDuration($id)
  $timestamp = QueueWatch::getLastSeen($id);
  $status = QueueWatch::getStatus($id);
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Queue::after(function (JobProcessed $event) {
            $event->job->markJobCompleted();
        });
    }
}

AFAIK there is no queue independent way to do this.

Would this be welcomed to be included in the core?

Please review and give me some feedback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions