vincent15000's avatar

Observer working locally but not in production

Hello,

That's strange : I have an observer working fine locally, but not in production.

Is there any reason why it could happen so ?

Thanks for your help.

V

0 likes
9 replies
Braunson's avatar
  • What type of observer (model, event, other?)
  • How are you registering it?
  • What differs between environments (locally vs production -- Queue driver, queue configuration, etc)
  • Are you seeing any errors on prod or just silently not firing?
  • Caching issues potentially? Is your config/event cached, if so clear it.
  • Are you queueing the observers or the logic within the observer method?

I'd first look at your .env setup, specifically are observer events queued on prod (and no queue worker is running) but locally they are being run sync?

1 like
vincent15000's avatar

@Braunson

  • model observer

  • registration with the ObservedBy attribute placed on the model class

  • no queue drive, it's a basic application

  • silently not firing

  • I have cleared the cache and recreated it

  • no queue at all

  • I have exactly the same .env file, except for the database credentials

Glukinho's avatar

Please provide your code and tell exactly what is "working fine locally" and "not working in production". What you do and what goes not as you expect?

1 like
vincent15000's avatar

@Glukinho

#[ObservedBy([TransactionObserver::class])]
class Transaction extends Model
private $contributionService;

public function __construct(ContributionService $contributionService)
{
    $this->contributionService = $contributionService;
}

/**
 * Handle the Transaction "updated" event.
 */
public function updated(Transaction $transaction): void
{
    if ($transaction->income && $transaction->payment_date) {
        $this->contributionService->calculateFromTransaction($transaction);
    }
}
public function update(UpdateTransactionRequest $request, Transaction $transaction): RedirectResponse
{
    $transaction->fill($request->all());

    $transaction->taxable = $request->boolean('taxable');

    $transaction->update();

    return redirect()->route('transactions.index');
}

When I update a transaction in production, the updated function from the observer isn't triggered (checked with logs).

Whereas locally, when I update a transaction, the updated function from the observer is triggered.

Glukinho's avatar

@vincent15000 show TransactionObserver class?

Are logging options the same in both environments? Maybe you look at wrong log file?

If you put dd() instead of logging, will it execute in prod?

1 like
vincent15000's avatar

The application has been developed with Laravel 10 / PHP 8.1.

Locally I'm using PHP 8.3, which shouldn't be a problem.

But the attribute doesn't work.

#[ObservedBy([TransactionObserver::class])]
class Transaction extends Model

I need to declare the observer in the app service provider boot function.

Transaction::observe(TransactionObserver::class);

And now it works !

Snapey's avatar
Snapey
Best Answer
Level 122

Is your Laravel 10 the latest available? registering Observers with attributes might have been introduced into Laravel 10 mid-version with a point release.

If it works locally and not on production, make sure you are deploying from the composer lock file so that you have the same version in production as locally.

1 like
vincent15000's avatar

@Snapey Thank you ... yes I have deployed from the composer lock file.

Well ... no problem now ... I have declared the observers in the app service provider and it works fine ;).

Please or to participate in this conversation.