Ejemplo n.º 1
0
        /// <summary>
        /// 单个文件上传
        /// </summary>
        /// <param name="newBucket"></param>
        /// <param name="path"></param>
        /// <param name="newOss"></param>
        private void SingleFileUpload(IOss newOss, string newBucket, string path)
        {
            var resume = JsonConvert.DeserializeObject <FormaterResume>(File.ReadAllText(path));

            dynamic response;

            var account = new ResumeRefrence
            {
                Id     = resume.Reference.Id,
                Source = resume.Reference.Source
            };

            #region 准备上传简历到数据库

            try
            {
                response = HttpAPI.PrepareUploadResume(JsonConvert.SerializeObject(account));

                if (response.Code.ToString() != "0")
                {
                    this.AsyncSetLog(this.tbx_Log, $"准备上传简历API,响应信息:{JsonConvert.SerializeObject(response)}");

                    pathQueue.Enqueue(path);

                    return;
                }
            }
            catch (Exception ex)
            {
                pathQueue.Enqueue(path);

                this.AsyncSetLog(this.tbx_Log, $"{DateTime.Now} 准备上传简历到数据库异常,异常消息:{ex.Message}");

                return;
            }

            #endregion

            #region   简历到数据库

            try
            {
                response = HttpAPI.UploadResume(JsonConvert.SerializeObject(resume));

                if (response.Code.ToString() != "0")
                {
                    var failpath = ConfigurationManager.AppSettings["51Job_UploadFailFiles_Path"] + companyName + Path.GetFileName(path);

                    if (File.Exists(failpath))
                    {
                        File.Delete(failpath);
                    }

                    File.Move(path, failpath);

                    this.AsyncSetLog(this.tbx_Log, $"上传简历失败!响应信息:{JsonConvert.SerializeObject(response)}");

                    return;
                }
                else
                {
                    this.AsyncSetLog(this.tbx_Log, $"上传简历成功!已上传:{Interlocked.Increment(ref count)},还剩余:{pathQueue.Count} 份待上传!");
                }
            }
            catch (Exception ex)
            {
                pathQueue.Enqueue(path);

                this.AsyncSetLog(this.tbx_Log, $"{DateTime.Now} 上传简历到数据库异常,异常消息:{ex.Message}");

                return;
            }
            finally
            {
                HttpAPI.FinishUploadResume(JsonConvert.SerializeObject(account));
            }

            #endregion

            #region   简历到 OSS

            var tag = response.Reference.Tag.ToString();

            var id = response.Reference.ResumeId.ToString();

            var cellphone = response.Reference.Cellphone.ToString();

            var email = response.Reference.Email.ToString();

            if (tag == "C" || tag == "U")
            {
                if (cellphone != "0")
                {
                    resume.Cellphone = cellphone;

                    resume.Email = email;
                }

                using (var stream = new MemoryStream(GZip.Compress(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(resume)))))
                {
                    try
                    {
                        newOss.PutObject(newBucket, $"{ConfigurationManager.AppSettings["Oss.New.ResumePath"]}{id}", stream);
                    }
                    catch (Exception ex)
                    {
                        this.AsyncSetLog(this.tbx_Log, "上传 OSS 失败!异常信息:" + ex.Message);

                        retryResumeQueue.Enqueue(new Tuple <string, FormaterResume, string>(id, resume, path));

                        return;
                    }
                }
            }

            var uploadSuccessPath = ConfigurationManager.AppSettings["51Job_UploadSuccessFiles_Path"] + companyName + Path.GetFileName(path);

            if (File.Exists(uploadSuccessPath))
            {
                File.Delete(uploadSuccessPath);
            }

            File.Move(path, uploadSuccessPath);

            #endregion
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 单个文件上传
        /// </summary>
        /// <param name="newBucket"></param>
        /// <param name="path"></param>
        /// <param name="newOss"></param>
        private void SingleFileUpload(IOss newOss, string newBucket, string path)
        {
            var resume = JsonConvert.DeserializeObject <Resume>(File.ReadAllText(path));

            dynamic response;

            string tag;

            var account = new ResumeRefrence
            {
                Id     = resume.Reference.Id,
                Source = resume.Reference.Source
            };

            #region 准备上传简历到数据库

            try
            {
                response = HttpAPI.PrepareUploadResume(JsonConvert.SerializeObject(account));

                if (response.Code.ToString() != "0")
                {
                    this.AsyncSetLog(this.tbx_Log, $"准备上传简历API,响应信息:{JsonConvert.SerializeObject(response)}");

                    HttpAPI.FinishUploadResume(JsonConvert.SerializeObject(account));

                    pathQueue.Enqueue(path);

                    return;
                }
            }
            catch (Exception ex)
            {
                pathQueue.Enqueue(path);

                this.AsyncSetLog(this.tbx_Log, $"{DateTime.Now} 准备上传简历到数据库异常,异常消息:{ex.Message}");

                HttpAPI.FinishUploadResume(JsonConvert.SerializeObject(account));

                return;
            }

            #endregion

            #region   简历到数据库

            try
            {
                var times = 0;

                while (true)
                {
                    response = HttpAPI.UploadResume(JsonConvert.SerializeObject(resume));

                    if (response.Code.ToString() != "0" && times++ == 0)
                    {
                        HttpAPI.FinishUploadResume(JsonConvert.SerializeObject(account));

                        continue;
                    }

                    break;
                }

                tag = response.Reference.Tag.ToString();

                if (response.Code.ToString() != "0")
                {
                    var failpath = ConfigurationManager.AppSettings["Zhaopin_UploadFailFiles_Path"] + companyName + Path.GetFileName(path);

                    if (File.Exists(failpath))
                    {
                        File.Delete(failpath);
                    }

                    File.Move(path, failpath);

                    this.AsyncSetLog(this.tbx_Log, $"上传简历失败!响应信息:{JsonConvert.SerializeObject(response)}");

                    return;
                }
                else
                {
                    if (tag == "C")
                    {
                        Interlocked.Increment(ref insertCount);
                    }

                    if (tag == "U")
                    {
                        Interlocked.Increment(ref updateCount);
                    }

                    this.AsyncSetLog(this.tbx_Log, $"上传简历成功!已上传:{Interlocked.Increment(ref successCount)} C=>{insertCount} U=>{updateCount} 还剩余:{pathQueue.Count} 份待上传!");
                }
            }
            catch (Exception ex)
            {
                pathQueue.Enqueue(path);

                this.AsyncSetLog(this.tbx_Log, $"{DateTime.Now} 上传简历到数据库异常,异常消息:{ex.Message}");

                return;
            }
            finally
            {
                HttpAPI.FinishUploadResume(JsonConvert.SerializeObject(account));
            }

            #endregion

            #region   简历到 OSS

            var id = response.Reference.ResumeId.ToString();

            if (tag == "C" || tag == "U")
            {
                using (var stream = new MemoryStream(GZip.Compress(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(resume)))))
                {
                    try
                    {
                        newOss.PutObject(newBucket, $"{ConfigurationManager.AppSettings["Oss.New.ResumePath"]}{id}", stream);
                    }
                    catch (Exception ex)
                    {
                        this.AsyncSetLog(this.tbx_Log, "上传 OSS 失败!异常信息:" + ex.Message);

                        retryResumeQueue.Enqueue(new Tuple <string, Resume, string>(id, resume, path));

                        return;
                    }
                }
            }

            var uploadSuccessPath = ConfigurationManager.AppSettings["Zhaopin_UploadSuccessFiles_Path"] + companyName;

            if (!Directory.Exists(uploadSuccessPath))
            {
                Directory.CreateDirectory(uploadSuccessPath);
            }

            uploadSuccessPath += Path.GetFileName(path);

            if (File.Exists(uploadSuccessPath))
            {
                File.Delete(uploadSuccessPath);
            }

            File.Move(path, uploadSuccessPath);

            #endregion
        }