示例#1
0
        private async Task <int> UpdateAsync(string jobID, string appID, string userID, string jobType, string jobName, Expression <Action> methodCall, bool isSync)
        {
            if (methodCall == null)
            {
                throw new ArgumentNullException("methodCall");
            }

            var callExpression = methodCall.Body as MethodCallExpression;

            if (callExpression == null)
            {
                throw new ArgumentException("Expression body must be 'System.Linq.Expressions.MethodCallExpression' type.", "methodCall");
            }

            Type type;

            if (callExpression.Object != null)
            {
                var value = DALHelpers.GetExpressionValue(callExpression.Object);
                if (value == null)
                {
                    throw new InvalidOperationException("Expression object can not be null.");
                }

                type = value.GetType();
            }
            else
            {
                type = callExpression.Method.DeclaringType;
            }

            var methodInfo = callExpression.Method;
            var args       = callExpression.Arguments.Select(DALHelpers.GetExpressionValue).ToArray();

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (methodInfo == null)
            {
                throw new ArgumentNullException("method");
            }
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            DALHelpers.Validate(type, "type", methodInfo, "method", args.Length, "args");

            var invokeMeta = new InvokeMeta(type, methodInfo);

            //Save InvokeMeta and args
            var now = DateTime.Now;
            var job = new Job();

            job.JobID      = jobID;
            job.AppID      = appID;
            job.UserID     = userID;
            job.JobType    = jobType;
            job.JobName    = string.IsNullOrWhiteSpace(jobName) ? type.Name + "." + methodInfo.Name : jobName;
            job.InvokeMeta = JsonConvert.SerializeObject(invokeMeta, SerializerSettings.Settings);
            job.Parameters = Helpers.Encrypt(JsonConvert.SerializeObject(DALHelpers.SerializeArguments(args), SerializerSettings.Settings), encryptionKey); //ENCRYPT it!!!
            job.Created    = now;
            job.Score      = (new DateTimeOffset(now)).ToUnixTimeSeconds();

            var count = 0;

            //var filter = Builders<Job>.Filter.Eq(j => j.JobID, jobID);
            //var collection = database.GetCollection<Job>(JobCollectionName);

            //var result = isSync ? collection.ReplaceOne(filter, job) : await collection.ReplaceOneAsync(filter, job);
            //if (result.IsAcknowledged)
            //    count = (int)result.ModifiedCount;

            return(count);
        }
示例#2
0
        public static Job CreateJobFromExpression(string encryptionKey, string appID, string userID, string jobType, string jobName, LambdaExpression methodCall)
        {
            if (methodCall == null)
            {
                throw new ArgumentNullException("methodCall");
            }

            var callExpression = methodCall.Body as MethodCallExpression;

            if (callExpression == null)
            {
                throw new ArgumentException("Expression body must be 'MethodCallExpression' type.", "methodCall");
            }

            var type       = callExpression.Method.DeclaringType;
            var methodInfo = callExpression.Method;

            if (callExpression.Object != null)
            {
                var objectValue = GetExpressionValue(callExpression.Object);
                if (objectValue == null)
                {
                    throw new InvalidOperationException("Expression object should be not null.");
                }

                type = objectValue.GetType();

                methodInfo = type.GetNonOpenMatchingMethod(callExpression.Method.Name, callExpression.Method.GetParameters().Select(x => x.ParameterType).ToArray());
            }

            var args = callExpression.Arguments.Select(GetExpressionValue).ToArray();

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (methodInfo == null)
            {
                throw new ArgumentNullException("method");
            }
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            Validate(type, "type", methodInfo, "method", args.Length, "args");

            var invokeMeta = new InvokeMeta(type, methodInfo);

            //Save InvokeMeta and args
            var job = new Job();

            job.AppID      = appID;
            job.UserID     = userID;
            job.JobType    = jobType;
            job.JobName    = string.IsNullOrWhiteSpace(jobName) ? type.Name + "." + methodInfo.Name : jobName;
            job.InvokeMeta = JsonConvert.SerializeObject(invokeMeta, SerializerSettings.Settings);
            job.Parameters = Helpers.Encrypt(JsonConvert.SerializeObject(SerializeArguments(args), SerializerSettings.Settings), encryptionKey); //ENCRYPT it!!!
            job.Created    = DateTime.Now;

            return(job);
        }