0

I'm using laravel Scheduler, i want to set a subscription status to expired if a certain condition is met, and also need this to run every mins in the background..Figured task scheduler is the best option but couldn't figure out why the code is'nt executing after running php artisan schedule:work ..Below is my code Located in App/Console/Kernel.php (schedule function)

  $schedule->call(function () {
        $checkSubStatus = UserSubscription::where('status', 'approved')->where('users_id', auth()->user()->id)->first();
        $currentTime = Carbon::now();
        if($currentTime > $checkSubStatus->expiry_date) {
            $checkSubStatus->update([
                'status' => 'expired'
            ]);
        }
    })->everyMinute();

But works when i just delete the table, like so DB::table('users_subscription')->delete(); Please any help?

Dipo Deen
  • 11
  • 3
  • Is the task scheduler not working or the code inside the closure (function)? I mean, are you sure this code is executed? – Anthony Aslangul Aug 21 '21 at 14:12
  • The task scheduler is working once i run php artisan schedule:work but the code does'nt get executed, unless i try something else like delete a table – Dipo Deen Aug 21 '21 at 14:18
  • You're trying to get user's id with session information, but it won't work in queued or scheduled jobs because session does not exist in that time. because of this probably you're getting error in this block "auth()->user()->id" – Emre Kaya Aug 21 '21 at 14:30
  • Ohh myy..is there any alternative?? – Dipo Deen Aug 21 '21 at 14:37
  • You are super right , it works when i hard code an id in there, Is there a way to combat this? – Dipo Deen Aug 21 '21 at 14:38
  • @DipoDeen whether there's a solution to this is the wrong question. What if there are 10 users currently logged in your site. Which user would this refer to? Does it have to refer to any user in particular? Can't you just delete all expired subscriptions for any user? – apokryfos Aug 21 '21 at 14:50

1 Answers1

1

You have an exception caused by auth()->user()->id, since no user is logged when your closure is executed.

You can check the logs in storage/logs/laravel.log and if it is the case, the solution is just to avoid using any authentification mechanisms inside the scheduler.

As an alternative, you have to rethink what UserSubscription should be expired:

UserSubscription::where('status', 'approved')
->where('expiry_date', '<', now())
->update(['status' => 'updated');

No user anymore, you just expire every subscriptions where the date is before now.

Credit to Emre Kaya for finding the error.

Anthony Aslangul
  • 3,589
  • 2
  • 20
  • 30
  • In deed, i updated my answer. Emre Kaya, if you write an answer i'll be glad to delete mine. – Anthony Aslangul Aug 21 '21 at 14:45
  • You're right tho,i can see the error logs...Somethin about call to update function on null which indicates that the id is'nt working. is there any solution for this ? – Dipo Deen Aug 21 '21 at 14:48
  • I updated my answer, maybe it can help you, if you have any question tell me – Anthony Aslangul Aug 21 '21 at 14:52
  • Damnn..Best answer ever, Thanks so much, it works perfectly..how come i didnt think like this..lol.Most grateful – Dipo Deen Aug 21 '21 at 14:56
  • No problem :) I see you are new on SO, don't forget to validate the answer (here and in your other questions) so other people with the same kind of error can quickly understand this is what they are looking for. Have a nice day. – Anthony Aslangul Aug 21 '21 at 15:02