Click or drag to resize

IInfrascaleSdkListenerListen Method (ActionJobSessionContract, ActionJobSessionContract, ActionJobEventsContract, ActionJobSessionContract)

Create an instance of the JobSessionAggregatedListener for listen all Job callbacks without specifiyng callback types. Use other specified Jobs related listeners, if you know real type of the listened Job.

Namespace:  SOS.SDK.Contracts
Assembly:  SOS.SDK (in SOS.SDK.dll) Version: 7.6.1.5742
Syntax
C#
JobSessionAggregatedListener Listen(
	Action<JobSessionContract> startedHandler = null,
	Action<JobSessionContract> progressHandler = null,
	Action<JobEventsContract> eventsHandler = null,
	Action<JobSessionContract> completedHandler = null
)

Parameters

startedHandler (Optional)
Type: SystemActionJobSessionContract
A delegate for handle JobStarted callbacks.
progressHandler (Optional)
Type: SystemActionJobSessionContract
A delegate for handle JobProgress callbacks.
eventsHandler (Optional)
Type: SystemActionJobEventsContract
A delegate for handle JobEvents callbacks.
completedHandler (Optional)
Type: SystemActionJobSessionContract
A delegate for handle JobCompleted callbacks.

Return Value

Type: JobSessionAggregatedListener
JobSessionAggregatedListener, subscribed to the current Infrascale Service callbacks.

Return Value

Type: JobSessionAggregatedListener
JobSessionEventsListenerT, subscribed to the current Infrascale Service callbacks.
Remarks
Set the FilterJobId property for start listening. Dispose Listener after Job Completion for unsubscribe from Infrascale Service callbacks.
Examples
Run Online Backup using stored settings
using System;
using System.Diagnostics;
using System.Security.Principal;
using SOS.SDK.Contracts.DataContracts;
using SOS.SDK.Contracts.DataContracts.Jobs;
using SOS.SDK.Contracts.DataContracts.Jobs.Backup;
using SOS.SDK.Contracts.DataContracts.Requests;
using SOS.SDK.Tools.Creators;
using SOS.SDK.Tools.Runners;

namespace SOS.SDK.Test.Examples.OnlineBackup
{
    public class RunOnlineBackupExamples
    {
        private string _testAccount = @"sdk.test@sosonlinebackup.com";
        private string _testAccountPassword = @"qweqwe";
        private InfrascaleClient _client;
        private IdentityContract _identity;

        public void SignIn()
        {
            if (_client == null)
            {
                _client = new InfrascaleClient();
                _client.Connect();
                _identity = null;
            }

            if (_identity == null)
            {
                var signInResponse = _client.SignIn(new SignInRequest
                {
                    Credentials = new CredentialsContract { Login = _testAccount, Password = _testAccountPassword }
                });

                _identity = signInResponse.Identity;

                _client.SubscribeCallback(new SubscribeCallbackRequest {Identity = _identity});
            }
        }

        public void RunBackupWithMonitoringAndUsingRawContractsAndUseStoredSettings()
        {
            SignIn();

            // It's safe to make this request multiple times.
            // You can call them before starting each Job.
            _client.SubscribeCallback(new SubscribeCallbackRequest {Identity = _identity});

            using (var listener = _client.ListenOnlineBackupJob(
                OnStarted, OnProgress,
                OnEvents, OnCompleted))
            {
                // Used by Infrascale Server for impersonation under required user permissions
                var currentProcessName = Process.GetCurrentProcess().ProcessName;

                // Used by Infrascale Server for impersonation under required user permissions
                string currentUserName;
                using (var windowsIdentity = WindowsIdentity.GetCurrent())
                {
                    currentUserName = windowsIdentity.Name;
                }

                // Starting Online Backup Job using stored backup settings
                var session = _client.RunOnlineBackupJob(new RunOnlineBackupJobRequest
                {
                    Identity = _identity,
                    CallingProcessName = currentProcessName,
                    WindowsUser = currentUserName
                });

                listener.FilterJobId = session.JobId;

                listener.WaitForCompletion();
            }

            // Note: this call unsubscribes you from all Infrascale Server callbacks, not just from current session events.
            // Don't call this method, if you want to receive other sessions callbacks.
            _client.UnsubscribeCallback(new UnsubscribeCallbackRequest {Identity = _identity});

            SignOut();
        }

        private void OnStarted(OnlineBackupJobSessionContract started)
        {
            Console.WriteLine("Backup Job {0} is Started", started.JobId);
        }

        private void OnProgress(OnlineBackupJobSessionContract progress)
        {
            Console.WriteLine("JobID: {0}", progress.JobId);
            Console.WriteLine("\tJob state: {0}", progress.BackupState);
            Console.WriteLine("\tCompleted, %: {0}", progress.ProgressPercentage);

            Console.WriteLine("\tBackupSet Total: {0}", CountAndSizeToString(progress.Progress.BackupSet));
            Console.WriteLine("\tFailed:          {0}", CountAndSizeToString(progress.Progress.Failed));
            Console.WriteLine("\tModified:        {0}", CountAndSizeToString(progress.Progress.Modified));
            Console.WriteLine("\tProcessed:       {0}", CountAndSizeToString(progress.Progress.Total));
            Console.WriteLine("\tUnchanged:       {0}", CountAndSizeToString(progress.Progress.Unchanged));
            Console.WriteLine("\tUploaded:        {0}", CountAndSizeToString(progress.Progress.Uploaded));

            foreach (var progressFileState in progress.Progress.FileStates)
            {
                Console.WriteLine("\t{0}: {1} ({2} / {3})", progressFileState.Name, progressFileState.Status, progressFileState.CompletedSizeBytes, progressFileState.TotalSizeBytes);
            }
        }

        private void OnEvents(JobEventsContract events)
        {
            Console.WriteLine("JobID: {0}", events.JobId);
            Console.WriteLine("\tEvents:");

            foreach (var jobEventContract in events.Events)
            {
                Console.WriteLine("\t{0}: [{1}] {2}", jobEventContract.EventTime, jobEventContract.MessageType, jobEventContract.Message);
            }
        }

        private void OnCompleted(OnlineBackupJobSessionContract completed)
        {
            Console.WriteLine("JobID: {0}", completed.JobId);
            Console.WriteLine("\tBackup of {0} file(s) is completed.", completed.Progress.Total.Count);
        }

        private string CountAndSizeToString(CountAndSizeContract countAndSize)
        {
            return string.Format("{0} ({1} bytes)", countAndSize.Count, countAndSize.SizeBytes);
        }
    }
}
See Also

Reference