/// <summary>
 /// 执行job
 /// </summary>
 /// <param name="jobItem"></param>
 /// <returns></returns>
 public bool StartBackgroudJob(HttpJobItem jobItem)
 {
     try
     {
         if (string.IsNullOrEmpty(jobItem.Data))
         {
             return(true);
         }
         using (var connection = JobStorage.Current.GetConnection())
         {
             var hashKey = CodingUtil.MD5(jobItem.JobName + ".runtime");
             using (var tran = connection.CreateWriteTransaction())
             {
                 tran.SetRangeInHash(hashKey, new List <KeyValuePair <string, string> >
                 {
                     new KeyValuePair <string, string>("Data", jobItem.Data)
                 });
                 tran.Commit();
             }
         }
         return(true);
     }
     catch (Exception ex)
     {
         Logger.ErrorException("HttpJobDispatcher.StartBackgroudJob", ex);
         return(false);
     }
 }
示例#2
0
        /// <summary>
        /// 执行job
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task StartBackgroudJob(DashboardContext context)
        {
            try
            {
                var jobItemBody = await GetRequestBody <HttpJobItem>(context);

                var jobItem = jobItemBody.Item1;
                if (jobItem == null || string.IsNullOrEmpty(jobItem.JobName))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    await context.Response.WriteAsync($"invalid request body:{jobItemBody.Item2}");

                    return;
                }

                if (string.IsNullOrEmpty(jobItem.Data))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NoContent;
                    return;
                }

                using (var connection = JobStorage.Current.GetConnection())
                {
                    var hashKey = CodingUtil.MD5((!string.IsNullOrEmpty(jobItem.RecurringJobIdentifier)?jobItem.RecurringJobIdentifier:jobItem.JobName) + ".runtime");
                    using (var tran = connection.CreateWriteTransaction())
                    {
                        tran.SetRangeInHash(hashKey, new List <KeyValuePair <string, string> >
                        {
                            new KeyValuePair <string, string>("Data", jobItem.Data)
                        });
                        tran.Commit();
                    }
                }

                context.Response.StatusCode = (int)HttpStatusCode.NoContent;
                return;
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.StartBackgroudJob", ex);
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                await context.Response.WriteAsync(ex.Message);
            }
        }