Skip to content

Commit

Permalink
Improve docs and call queue when dispatching now (resolves #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Heffley committed Mar 20, 2020
1 parent eb9f813 commit b6504d6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
42 changes: 42 additions & 0 deletions docs/advanced.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
# Advanced Usage

## Multiple Operation Classes, One Database Table

Depending on the complexity of your operations, you may have multiple operation classes that have the exact same columns as another operation class.
Instead of having multiple tables that are exactly the same, you can opt to create one database table with the necessary columns and then set the
`protected $table` property on your `Operation` classes to point at that table.

```php
<?php

namespace App\Operations;

use DealerInspire\Operations\Operation;

class FirstTable extends Operation
{
protected $table = 'operations_table';

public function run()
{
//
}
}
```

```php
<?php

namespace App\Operations;

use DealerInspire\Operations\Operation;

class SecondTable extends Operation
{
protected $table = 'operations_table';

public function run()
{
//
}
}
```

## `queue` Property

If you have multiple queues in your app, some operations may need to run on a few of these queues. You can specify a `public $queue` property on your operation,
Expand Down
2 changes: 1 addition & 1 deletion docs/queueing.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ _If you do use Laravel's scheduling function, don't forget to [set it up](https:

If you ever need to do some logic or action while an event is being queued, you can use the `queue` hook function. This function is easy to abuse, so you can read more about what it is and how to avoid some potential pitfalls in [Queue Hook Function](/docs/queue-hook.md).

Next: [Handling Failures Gracefully](/docs/failing.md)
Next: [Ways to Schedule Operations](/docs/scheduling.md)
23 changes: 23 additions & 0 deletions docs/scheduling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Ways to Schedule Operations

There are three ways you can run an operation. All of these exist as static functions on your operation classes, and should be called statically.

Currently it is not supported to create an instance of your operation and run it by calling these functions directly from the instance.

## `schedule()`

We've already talked about using `schedule()` in [Creating Your First Operation](/docs/first-operation.md) adn [How to Queue Operations](/docs/queueing.md).
Whenever call `schedule()`, you need to pass in a Carbon object with the time you want the operation to run, and optionally you can pass in some additional
data for the operation to use.

## `dispatch()`

If you want to quickly create an operation that will run the very next time your Operator runs, you can use `dispatch()`. It doesn't accept a Carbon instance
and instead creates your operation with a timestamp with the current time. You can still pass in an array of attributes.

## `dispatchNow()`

If you do not want to queue an operation, you can use the `dispatchNow()` function which will run your operation synchronously. It will not run on queue or a
connection, so those properties will be ignored if they are set on the operation class.

Next: [Handling Failures Gracefully](/docs/failing.md)
20 changes: 20 additions & 0 deletions src/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,47 @@ public function cancel()
throw new OperationCanceledException();
}

/**
* @param Carbon|string $shouldRunAt
* @param array $attributes
* @return static
*/
public static function schedule($shouldRunAt, $attributes = [])
{
return static::create(array_merge(['should_run_at' => $shouldRunAt], $attributes));
}

/**
* @param array $attributes
* @return static
*/
public static function dispatch($attributes = [])
{
return static::schedule(Carbon::now(), $attributes);
}

/**
* @param array $attributes
* @return static
*/
public static function dispatchNow($attributes = [])
{
$operation = static::dispatch($attributes);

$operation->started_run_at = Carbon::now();

if (method_exists($operation, 'queue')) {
$operation->queue();
}

(new OperationJob($operation))->handle();

return $operation;
}

/**
* @return array
*/
public function tags()
{
return [
Expand Down
1 change: 1 addition & 0 deletions tests/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function it_can_dispatch_itself_concurrently()
$operation = ExampleOperation::dispatchNow();

$this->assertTrue($operation->hasRun());
$this->assertTrue($operation->fresh()->hookedIntoQueue());

// Ensure that the database record has all of the timestamps set correctly.
$operation = ExampleOperation::first();
Expand Down
4 changes: 4 additions & 0 deletions tests/Operations/ExampleOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class ExampleOperation extends Operation
{
protected $hasRun = false;

protected $casts = [
'hooked_into_queue' => 'bool',
];

public function queue()
{
$this->hooked_into_queue = true;
Expand Down

0 comments on commit b6504d6

Please sign in to comment.