classdef (Hidden) JobStorageEventManager < handle
    % JobStorageEventManager - Issue events associated with changes to
    % cluster job storage.
    
    % Copyright 2016 The MathWorks, Inc.
        
    events (Hidden, ListenAccess = public, NotifyAccess = private)
        CreateJob
        CreateTasks
        DeleteJob
        DeleteTask
        CancelJob
        CancelTask
        SubmitJob
        JobQueued
        JobRunning
        JobFinished
    end

    properties (Constant, Access = public)
        CreateJobEventName = 'CreateJob';
        CreateTasksEventName = 'CreateTasks';
        DeleteJobEventName = 'DeleteJob';
        DeleteTaskEventName = 'DeleteTask';
        CancelJobEventName = 'CancelJob';
        CancelTaskEventName = 'CancelTask';
        SubmitJobEventName = 'SubmitJob';
        JobQueuedEventName = 'JobQueued';
        JobRunningEventName = 'JobRunning';
        JobFinishedEventName = 'JobFinished';
    end
    
    methods (Access = private)
        function obj = JobStorageEventManager()
        end
    end
    
    methods (Static, Access = public)
        function obj = getInstance()
            % Return singleton instance of JobStorageEventManager
            mlock;
            persistent instance;
            if isempty(instance)
                instance = parallel.internal.cluster.JobStorageEventManager();
            end
            obj = instance;
        end
    end
    
    methods (Access = public)
        function notifyCreateJob(obj, job)
            % Notify listeners of CreateJob event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.CreateJobEventName, eventData);
        end
        
        function notifyCreateTasks(obj, job)
            % Notify listeners of CreateTasks event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.CreateTasksEventName, eventData);
        end
                
        function notifyDeleteJob(obj, job)
            % Notify listeners of DeleteJob event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.DeleteJobEventName, eventData);
        end
        
        function notifyDeleteTask(obj, job)
            % Notify listeners of DeleteTask event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.DeleteTaskEventName, eventData);
        end
               
        function notifyCancelJob(obj, job)
            % Notify listeners of CancelJob event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.CancelJobEventName, eventData);
        end
        
        function notifyCancelTask(obj, job)
            % Notify listeners of CancelTask event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.CancelTaskEventName, eventData);
        end
        
        function notifySubmitJob(obj, job)
            % Notify listeners of SubmitJob event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.SubmitJobEventName, eventData);
        end
        
        function notifyJobQueued(obj, job)
            % Notify listeners of JobQueued event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.JobQueuedEventName, eventData);
        end
        
        function notifyJobRunning(obj, job)
            % Notify listeners of JobRunning event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.JobRunningEventName, eventData);
        end
        
        function notifyJobFinished(obj, job)
            % Notify listeners of JobFinished event
            eventData = parallel.internal.cluster.JobStorageEventData(job);
            notify(obj, obj.JobFinishedEventName, eventData);
        end
    end
end

