예제 #1
0
        private void DownloadImageProductSpGoc(MqChangeImage mqProduct, JobClient updateProductJobClient, JobClient downloadImageProductJobClient, JobClient deleteThumbJobClient)
        {
            string path                   = pathImageSPGOC;
            var    productAdapter         = new DBTableAdapters.ProductTableAdapter();
            var    historyDownloadAdapter = new DBTableAdapters.History_DownloadImageProductTableAdapter();
            var    productTable           = new DB.ProductDataTable();

            productAdapter.Connection.ConnectionString         = connectionString;
            historyDownloadAdapter.Connection.ConnectionString = connectionString;
            //bien dem lay du lieu loi tu sql = 3 thì dừng
            int dem = 0;

            //lấy dữ liệu từ sql
            while (checkstop)
            {
                try
                {
                    productAdapter.FillBy_ID(productTable, mqProduct.ProductId);
                    break;
                }
                catch (Exception ex)
                {
                    if (dem == 3)
                    {
                        historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Get product from Database error." + ex, false);
                        Log.ErrorFormat("SP Gốc : ProductID = {0} Get product from Database error. \r\n {1}", mqProduct.ProductId, ex);
                        return;
                    }
                    dem++;
                    Thread.Sleep(30000);
                }
            }
            if (productTable.Rows.Count > 0)
            {
                string nameProduct = productTable.Rows[0]["Name"].ToString();
                string imageUrl    = productTable.Rows[0]["ImageUrls"].ToString();
                if (string.IsNullOrEmpty(nameProduct) || string.IsNullOrEmpty(imageUrl))
                {
                    Log.Error(string.Format("SP Gốc : ProductID = {0} Name or ImageUrl is null or empty.", mqProduct.ProductId));
                    return;
                }
                else
                {
                    string filename = Common.UnicodeToKoDauAndGach(nameProduct);
                    if (filename.Length > 100)
                    {
                        filename = filename.Substring(0, 99);
                    }
                    string folder    = Common.GetFolderSaveImageRootProduct(filename);
                    bool   fileSaved = Common.SaveFileDownloadImage(imageUrl, path + folder, filename + ".jpg", mqProduct.ProductId, 6619858476258121218);
                    if (fileSaved)
                    {
                        string pathsave  = Common.GetImagePath(folder, filename);
                        int    demupdate = 0;
                        while (checkstop)
                        {
                            try
                            {
                                productAdapter.UpdateImagePathAndValid(pathsave, 0, 0, true, mqProduct.ProductId);

                                #region Send Message Update solr and redis
                                Job job = new Job();
                                job.Data = BitConverter.GetBytes(mqProduct.ProductId);
                                job.Type = 2;
                                updateProductJobClient.PublishJob(job, updateProductJobExpirationMS);
                                //Log.InfoFormat("Send message To RabbitMq {0} with ID = {1}", rabbitMQServerName, productId);
                                #endregion

                                #region Xóa ảnh Thumb nếu có
                                //push message lên service xóa ảnh thumb
                                Job deletejob = new Job();
                                var thumb     = new MqThumbImageInfo
                                {
                                    ProductId   = mqProduct.ProductId,
                                    FolderImage = folder,
                                    ImageName   = filename,
                                    TypeProduct = 2
                                };
                                deletejob.Data = MqThumbImageInfo.GetMess(thumb);
                                deleteThumbJobClient.PublishJob(deletejob);
                                #endregion
                                #region Log Finished
                                historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, true, "SP Gốc", false);
                                #endregion
                                break;
                            }
                            catch (Exception ex)
                            {
                                Thread.Sleep(10000);
                                demupdate++;
                                if (demupdate == 1)
                                {
                                    Log.Error(string.Format("SP Gốc : CompanyID = {0} Product = {1}, Update ImagePath Error 1", 6619858476258121218, mqProduct.ProductId), ex);
                                }
                                else if (demupdate == numbererror)
                                {
                                    historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Update sql fail or push message fail.", false);
                                    Log.Error(string.Format("SP Gốc : CompanyID = {0} Product = {1}, Update ImagePath Error {2}", 6619858476258121218, mqProduct.ProductId, numbererror), ex);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Product not in Database.", false);
                Log.ErrorFormat("SP Gốc : ProductID = {0} Khong ton tai trong Database.", mqProduct.ProductId);
            }
        }