Is it possible to determine if a script is running within a task?
TasksService-&gt;getRunningTask() will tell you if a task is currently running. However, it doesn't tell you whether a particular chunk of code is being executed by that Task or by some other function.

The most straightforward solution I can think of would be to write yourself a little helper method to check whether the call stack includes a Task class:

static function inTask()
{
   foreach(debug_backtrace() as $caller)
   {
      if (substr($caller['file'], -8) === 'Task.php') return true;
   }
   return false;
}


Add that to your plugin class, and then you can call it anywhere you need:

if (MyPlugin::inTask()) ...