Skip to content

Shift Client

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

The client component allows client apps to add background jobs and send commands to Shift server to stop, delete, reset, and run jobs.

Adding jobs are as easy as using Linq lambda expression.

var job = new TestJob();
var jobID = jobClient.Add(() => job.Start("Hello World!"));

Complex long running job that report progress data can be added easily.

var job = new TestJob();
var progress = new SynchronousProgress<ProgressInfo>();
var cancelToken = (new CancellationTokenSource()).Token; 
var pauseToken = (new PauseTokenSource()).Token;
var jobID = jobClient.Add( () => job.Start("Hello World", progress, cancelToken, pauseToken) );

Please note that the progress object added by the Shift client is only a placeholder that tells the Shift server to expect a progress event call. The progress object will be replaced by a new server side SynchronousProgress instance before running the job.

The cancel and pause tokens are also placeholders to be replaced when the method is invoked by the server. Without those tokens, the server won't be able to stop or pause the job.

To update an existing job, use the Update method. Only non-running jobs are updateable. Updating a job also resets all updated job properties, start/end dates, progress data, and status.

var count = jobClient.Update(jobID, () => job.Start("Update Hello World!"));

Restrictions

Several restrictions must be observed when adding jobs to the queue:

  • Only public methods can be started by Shift server.
  • Unassigned generic type parameters are not supported.
  • No global methods, only class methods.
  • Out and Ref parameters are not supported.

Those restrictions are in place mainly because Shift server can run in a completely separate process from client app.

Async Method

Since ver. 1.0.8.2, Shift can add public async method to the background job.

The job with the async method:

public class TestJobAsync 
{
    public async Task StartAsync(string message)
    {
        Console.WriteLine(message);
        await Task.Delay(1000);
    }
}

To add the async method, do not use async lambda expression, treat it similar to the non-async method:

var job = new TestJobAsync();
var jobID = jobClient.Add( () => job.StartAsync("Hello World") );

Configuration

Configuration Description
DBConnectionString The storage connection string.
UseCache (obsolete) Removed in ver 1.0.8.1. If true then the CacheConfigurationString is required, default is false.
CacheConfigurationString (obsolete) Removed in ver 1.0.8.1. The cache configuration, required if UseCache is true

As of version 1.0.8.1, the Redis cache used in conjunction with Microsoft SQL server has been removed. For improvement in performance, use pure Redis, MongoDB, or DocumentDB instead.

Action and Commands

Shift client has several actions and commands. Action have immediate impact to jobs, however command must wait for the Shift server running the jobs to execute the command.

Action Method Description
Add Add() Add job to the server queue. Multiple methods with various parameters are supported.
Update Update(string jobID, Expression methodCall) Update existing job.
Reset ResetJobs(IList jobIDs) Reset job status and progress.
Delete DeleteJobs(IList jobIDs) Delete jobs in queue.
Command Method Description
Stop SetCommandStop(IList jobIDs) Stop running, paused, or non-running job.
Pause SetCommandPause(IList jobIDs) Pause running job.
Continue SetCommandContinue(IList jobIDs) Continue paused job.
Run Now SetCommandRunNow(IList jobIDs) Set the highest priority to non-running job in the queue.

There are also several methods to get detailed job information and progress.

Method Description
GetJob(int jobID) Return specific job instance based on jobID.
GetJobView(int jobID) Return jobView instance based on jobID.
GetJobStatusCount(string appID, string userID) Return total count of all job statuses (running, not running, completed, stopped, with errors). Both appID and userID are optional.
GetProgress(int jobID) Return the current progress of a job from DB.
GetCachedProgress(int jobID) (obsolete) Removed from ver 1.0.8.1. Return the current progress of a job from cache only.

The appID is required to identify jobs for multi-tenant application. The userID field is optional to allow status count by user's submitted jobs. Please, note that the Shift server does not run jobs by specific app or user, the Shift server use FIFO and polling to pick up jobs from the queue.

Batch Jobs

Shift does not have a built-in jobs chaining or batch transaction process. A workaround for this is to use a single wrapper job with multiple child jobs and then run the wrapper job in Shift. Please refer to Schedule Batch Continuation page.