function addSources(taskID, serializedException, varargin)
%addSources Add task sources to the pool.

% Copyright 2013-2021 The MathWorks, Inc.

pool = gcp('nocreate');
if isempty(pool)
dctSchedulerMessage(2, 'parallel.internal.queue.addSources - no pool.');
return;
end

% Short-circuit in the case where auto-attach is disabled.
client = pool.hGetClient();
if ~client.ParallelJob.AutoAttachFiles
dctSchedulerMessage(2, ['parallel.internal.queue.addSources ', ...
'- auto attach files is disabled.']);
return;
end

% Find our future.
future = hGetFutureByID(pool.FevalQueue, taskID);

if isempty(future)
% No longer outstanding. Log and return.
dctSchedulerMessage(2, 'parallel.internal.queue.addSources - no task ID %d.', taskID);
return
end

missingFunction = future.Function;
if ischar(missingFunction)
missingFunction = str2func(missingFunction);
end

dctSchedulerMessage(2, ...
'parallel.internal.queue.addSources - adding for task %d: %s.', ...
taskID, func2str(missingFunction));

% Convert the task function
import parallel.internal.apishared.AttachedFiles
fcnOrFcns = AttachedFiles.convertFunctionHandleForDependencyAnalysis(missingFunction);

% Look at varargin - second element might be a serialized exception
additionalFcnsFromException = {};
try
if nargin == 3 && iscell(varargin{1}) && numel(varargin{1}) == 2
if serializedException
% PCT_Java_Transition_TODO g2463809 remove unnecessary argument and branch
serializedOutput = varargin{1}{2};
output = distcompdeserialize(serializedOutput);
else
output = varargin{1}{2};
end
if ~isempty(output) && isa(output, 'MException')
[~, additionalFcnsFromException] = ...
parallel.internal.pool.missingSourcesFromException(output);
end
end
catch E
dctSchedulerMessage(1, 'parallel.internal.queue.addSources encountered:', E);
end

if ~iscell(fcnOrFcns)
fcns = {fcnOrFcns};
else
fcns = fcnOrFcns;
end

parallel.internal.pool.attachDependentFilesToPool(pool, [fcns, additionalFcnsFromException]);
end