Exemple #1
0
        private void InvokeAction(string prefix, TccWorkUnitContext <TContext> ctx, bool convertResults)
        {
            var            uri   = GetUri(ctx.Value);
            RpcClientProxy proxy = RpcProxyFactory.GetProxy <ITccRpcHostService>(uri);

            proxy.BeginInvoke <TArgs>(
                prefix + WorkName,
                ConvertArgs(ctx.Value),
                delegate(RpcClientContext context) {
                try {
                    var rets = context.EndInvoke <TResults>();
                    if (convertResults)
                    {
                        ConvertResults(rets, ctx.Value);
                    }
                    ctx.Return();
                } catch (Exception ex) {
                    ctx.ThrowException(ex);
                }
            }
                );
        }
Exemple #2
0
        internal void RunAction(TContext context, TccAction nextAction, Action <Exception> callback)
        {
            Action <TccWorkUnitContext <TContext> > Proc;
            TccWorkState succState;
            TccWorkState failState;

            Tracing.InfoFmt("WorkUnit<{0}>.Run{1} State:{2}", _workName, nextAction, _state);

            switch (nextAction)
            {
            case TccAction.Try:
                if (_state != TccWorkState.None)
                {
                    throw new Exception("Unexcepted WorkUnit State:" + _state);
                }
                _state    = TccWorkState.Trying;
                Proc      = Try;
                succState = TccWorkState.Tryed;
                failState = TccWorkState.TryFailed;
                break;

            case TccAction.Confirm:
                if (_state != TccWorkState.Tryed)
                {
                    throw new Exception("Unexcepted WorkUnit State:" + _state);
                }
                Proc      = Confirm;
                _state    = TccWorkState.Confirming;
                succState = TccWorkState.Confirmed;
                failState = TccWorkState.ConfirmFailed;
                break;

            case TccAction.Cancel:
                if (_state != TccWorkState.Tryed && _state != TccWorkState.TryFailed)
                {
                    throw new Exception("Unexcepted WorkUnit State:" + _state);
                }
                Proc      = Cancel;
                _state    = TccWorkState.Cancelling;
                succState = TccWorkState.Cancelled;
                failState = TccWorkState.CancelFailed;
                break;

            default:
                throw new Exception("NeverGoHere");
            }

            int hasReturned = 0;
            TccWorkUnitContext <TContext> ctx = new TccWorkUnitContext <TContext>(
                context,
                delegate(Exception ex) {
                if (ex != null)
                {
                    Tracing.ErrorFmt(ex, "WorkUnit<{0}>.Run{1} Failed in callback.", _workName, nextAction);
                }
                else
                {
                    Tracing.InfoFmt("WorkUnit<{0}>.Run{1} ok.", _workName, nextAction);
                }

                if (Interlocked.CompareExchange(ref hasReturned, 1, 0) == 0)
                {
                    if (ex == null)
                    {
                        _state = succState;
                        callback(null);
                    }
                    else
                    {
                        _error = ex;
                        _state = failState;
                        callback(ex);
                    }
                }
                else
                {
                    Tracing.ErrorFmt("WorkUnit<{0}>.Run{1} callback return more than once.", _workName, nextAction);
                }
            }
                );

            try {
                Proc(ctx);
            } catch (Exception ex) {
                if (Interlocked.CompareExchange(ref hasReturned, 1, 0) == 0)
                {
                    Tracing.ErrorFmt(ex, "WorkUnit<{0}>.Run{1} Failed.", _workName, nextAction);
                    _error = ex;
                    _state = failState;
                    callback(ex);
                }
                else
                {
                    Tracing.ErrorFmt("WorkUnit<{0}>.Run{1} return more than once.", _workName, nextAction);
                }
            }
        }
Exemple #3
0
 protected abstract void Cancel(TccWorkUnitContext <TContext> ctx);
Exemple #4
0
 protected abstract void Confirm(TccWorkUnitContext <TContext> ctx);
Exemple #5
0
 protected abstract void Try(TccWorkUnitContext <TContext> ctx);
Exemple #6
0
 protected override void Cancel(TccWorkUnitContext <TContext> ctx)
 {
     InvokeAction("cancel_", ctx, false);
 }
Exemple #7
0
 protected override void Confirm(TccWorkUnitContext <TContext> ctx)
 {
     InvokeAction("confirm_", ctx, false);
 }
Exemple #8
0
 protected override void Try(TccWorkUnitContext <TContext> ctx)
 {
     InvokeAction("try_", ctx, true);
 }