Skip to content

Quick Start

Hartono Halim edited this page Aug 23, 2017 · 20 revisions

Jobs that can be shifted and run by Shift server have to have these characteristics:

  • Must be a self contained jobs. It generally can't depend on any specific server resources. With multiple Shift servers running, there is no guarantee that certain job is running under certain server.
  • All configuration and information needed by the jobs are either passed through the serialized parameters or reachable in a global storage that the Shift servers can connect to.
  • No support for out or ref type parameters, such as MyMethod(ref myValue) or MyMethod(out myValue). The background jobs will be running in a completely different process and thread than the initial app that pushes the jobs to the queue.

Infrastructure Setup

.NET Framework

Requires .NET Framework 4.6.1

Storage

Shift requires a database to store all the jobs and progress data. Shift depends on Redis or Microsoft SQL Server 2008 and later.

To install Redis for windows, go to Redis github project site and I recommend using their latest release Redis-x64-.msi package to install Redis. A helpful client for Redis is the Redis Desktop Manager.

To install MongoDB, go to MongoDB and download the Community Server edition. Use the documentation to Install MongoDB Community Edition on Windows.

To use SQL Server, please run the SQL script to create the Shift database and tables. The SQL script is in create_db.sql.

Cache

As of ver. 1.0.8.1 the cache feature has been removed. Please use other than SQL server for performance improvements.

Shift Client

Use nuget.org to install Shift then build your application. It is best to have a single static Shift client per client app. The Shift client requires the DBConnectionString for storage connection.

Redis configuration example:

class Program
{
    private static JobClient jobClient; //the Shift job client
    static void Main(string[] args)
    {
        InitShiftClient();
        //More code ...
    }

    private static void InitShiftClient()
    {
        var config = new Shift.ClientConfig();
        config.DBConnectionString = "localhost:6379";
        config.StorageMode = "redis"; 
        jobClient = new JobClient(config);
    }
}

SQL server usage:

class Program
{
    private static JobClient jobClient; //the Shift job client
    static void Main(string[] args)
    {
        InitShiftClient();
        //More code ...
    }

    private static void InitShiftClient()
    {
        var config = new Shift.ClientConfig();
        config.StorageMode = "mssql"; 
        config.DBConnectionString = "Data Source=localhost\SQL2014;Initial Catalog=ShiftJobsDB;Integrated Security=SSPI;";
        //config.EncryptionKey = "[OPTIONAL_ENCRYPTIONKEY]"; //optional, will encrypt parameters in DB
        jobClient = new JobClient(config);
    }
}

Once the Shift client is initialized, it's time to add jobs to the Shift queue.

private static void AddJob()
{
    var job = new TestJob();
    var progress = new SynchronousProgress<ProgressInfo>();
    var cancelToken = (new CancellationTokenSource()).Token; 
        var pauseToken = (new PauseTokenSource()).Token;
    var jobID = jobClient.Add("Shift.Demo.Client", () => job.Start("Hello World", progress, cancelToken, pauseToken ));
}

public class TestJob
{
    public void Start(string value, IProgress<ProgressInfo> progress, CancellationToken cancelToken, PauseToken pauseToken)
    {
        var total = 10;

        var note = "";
        for (var i = 0; i < total; i++)
        {
            if (cancelToken.IsCancellationRequested)
            {
                cancelToken.ThrowIfCancellationRequested(); //throw OperationCanceledException
            }

                        pauseToken.WaitWhilePausedAsync().GetAwaiter().GetResult();
            
            note += i + " - " + value + "<br/> \n";

            var pInfo = new ProgressInfo();
            pInfo.Percent = (int)Math.Round(((i + 1) / (double)total) * 100.00, MidpointRounding.AwayFromZero); ;
            pInfo.Note = note;
            if (progress != null)
                progress.Report(pInfo);

            Thread.Sleep(2500); //do work here
        }

        return;
    }
}

As a reference, check out the console app Shift.Demo.Client that demonstrates a simple use of Shift client.

Shift Server

Use nuget.org to install Shift and all dependencies. Another option is to manually build Shift project and reference Shift.dll in your server container. The server container can be any type of windows .NET app. Example Shift servers containers using Windows service or Shift Azure WebJob are already provided.

Using the Redis storage option:

class Program
{
    private static JobServer jobServer;

    static void Main(string[] args)
    {
        InitShiftServer();

        //Additional code here....

        jobServer.StopServer(); //always stop server before terminating app
    }
    
    private static void InitShiftServer()
    {
        var config = new Shift.ServerConfig();
        config.AssemblyFolder = "client-assemblies\";
        config.MaxRunnableJobs = 100;
        config.ProcessID = "ShiftDemo123"; //demo/testing ID
        config.StorageMode = "redis"; 
        config.DBConnectionString = "localhost:6379";
        config.ProgressDBInterval = TimeSpan.Parse("00:00:00");

        jobServer = new JobServer(config);
    }
}

Using the SQL storage mode:

class Program
{
    private static JobServer jobServer;

    static void Main(string[] args)
    {
        InitShiftServer();

        //Additional code here....

        jobServer.StopServer(); //always stop server before terminating app
    }
    
    private static void InitShiftServer()
    {
        var config = new Shift.ServerConfig();
        config.AssemblyFolder = "client-assemblies\";
        config.MaxRunnableJobs = 100;
        config.ProcessID = "ShiftDemo123"; //demo/testing ID
        config.StorageMode = "mssql"; 
        config.DBConnectionString = "Data Source=localhost\SQL2014;Initial Catalog=ShiftJobsDB;Integrated Security=SSPI;";
        jobServer = new JobServer(config);
    }
}

As a reference, use the demo console app Shift.Demo.Server. Run both the demo client and server to see Shift in action.

For other configuration options and detailed explanations, to go Shift Client and Shift Server pages.