lakewoodtrees's avatar

Variable throughout process

Hi,

I'm creating a backend Laravel app that will run a whole bunch of tasks on a schedule. Part of what I'm doing is logging the tasks and it's child actions.

For example, we have a task to:

  • take a file from sourceHost
  • parse it's contents
  • store the contents into the database
  • write a report to a file
  • upload the file to destinationHost

This task will run through a lot of single functions from different classes. In each one of these functions, I have a logger that logs informational data. For example, log how many files were found on the sourceHost. The logs are stored, and if we ever want some insight into what happened, we can look back at the logs.

What I'm trying to achieve, is that when the task kicks off, it sets a variable with $taskName = 'getOrdersFile' and then throughout the process, have that $taskName variable available so that I can add it to my log entries.

I know that I can keep on passing that variable to every class and function, but I am looking for a way to define this at the begining and then have it available all throughout the execution of this process.

I don't want to just set an ENV, because we could ne running multiple processes at the same time.

SO what I'm looking to do is to wrap each process and allow each process to have it's own definition $taskName.

I know I may be asking a broad question, I'm not asking for you to write this out for me, I'm a newbie in Laravel and I'm looking for directions or pointers.

0 likes
6 replies
lakewoodtrees's avatar

Thanks @snapey

From reading the documentation, it looks like this would help only if the class is set to receive those arguments. In my example, if I have a class that takes a filename and sourceHost connection, and in that class I want to access the taskName, I won't be able to unless I set the class to accept that param.

lakewoodtrees's avatar

@Snapey that's exactly what I'm trying to figure out :) - how can I make it that variable "global" so that in my class i can write something like if ($global()->taskName)

Snapey's avatar
Snapey
Best Answer
Level 122

@lakewoodtrees use the app container?

    $app()->bind('taskName', function(){
		return 'getOrdersFile';
	});

then you can use

	app('taskName');

Whether this would work depends on how you start the tasks and if they might share the same process from one set of tasks to another.

1 like

Please or to participate in this conversation.