Exemplo n.º 1
0
        private void ProcessImport(Int64 resource, Int64 resource_plugin, Dictionary <String, Object> connectorConf, List <PluginConnectorBaseDeployPackageMapping> mapping)
        {
            StringBuilder importLog = new StringBuilder();

            importLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Starting import thread...");

            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Starting import thread...");

            Int64 count = 0;

            try
            {
                if (connectorConf == null)
                {
                    throw new Exception("connectorConf is null");
                }

                if (mapping == null)
                {
                    throw new Exception("mapping is null");
                }

                String id = Guid.NewGuid().ToString();

                /*
                 * JsonGeneric records = new JsonGeneric();
                 * records.function = "ProcessImport";
                 * records.fields = new String[] { "resource", "uri", "importid", "registryid", "dataname", "datavalue", "datatype" }; */

                JsonGeneric records = new JsonGeneric();
                records.function = "ProcessImportV2";
                records.fields   = new String[] { "resource_plugin", "package" };

                JsonGeneric structRecords = new JsonGeneric();
                structRecords.function = "ProcessStructImport";
                structRecords.fields   = new String[] { "resource_plugin", "package" };

                String uri = plugin.GetPluginId().AbsoluteUri.ToLower();

                ImportPackageUserEvent newPackage = new ImportPackageUserEvent(delegate(PluginConnectorBaseImportPackageUser pkg)
                {
                    count++;
                    records.data.Add(new String[] { resource_plugin.ToString(), JSON.SerializeToBase64(pkg) });

                    if (records.data.Count >= 500)
                    {
                        try
                        {
                            SaveToSend(records, resource_plugin.ToString());
                            records.data.Clear();
                        }
                        catch { }
                    }

#if DEBUG
                    TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "}>ProcessImport> Package generated from resource plugin " + resource_plugin.ToString() + ". ID: " + pkg.pkgId);
#endif
                });


                ImportPackageStructEvent newStructPackage = new ImportPackageStructEvent(delegate(PluginConnectorBaseImportPackageStruct pkg)
                {
                    count++;
                    structRecords.data.Add(new String[] { resource_plugin.ToString(), JSON.SerializeToBase64(pkg) });

                    if (structRecords.data.Count >= 500)
                    {
                        try
                        {
                            SaveToSend(structRecords, resource_plugin.ToString());
                            structRecords.data.Clear();
                        }
                        catch { }
                    }
                });


                /*
                 * RegistryEvent reg = new RegistryEvent(delegate(String importId, String registryId, String dataName, String dataValue, String dataType)
                 * {
                 *  count++;
                 *  records.data.Add(new String[] { resource.ToString(), uri, importId, registryId, dataName, dataValue, dataType });
                 *
                 *  //Contabiliza a quantidade de registros para separar em vários arquivos
                 *  if (records.data.Count >= 30000)
                 *  {
                 *      //Após 30000 registros monitora a troca de registryId para salvar o arquivo
                 *      //Evitando que o mesmo registryId tenha dados em arquivos diferentes
                 *      //Isso evita problemas no servidor
                 *
                 *      if (lastRegistryId != registryId)
                 *      {
                 *          try
                 *          {
                 *              SaveToSend(records, importId);
                 *              records.data.Clear();
                 *          }
                 *          catch { }
                 *      }
                 *  }
                 *
                 *  lastRegistryId = registryId;
                 * });*/

                LogEvent log = new LogEvent(delegate(Object sender, PluginLogType type, string text)
                {
                    TextLog.Log("PluginStarter", "{" + ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri + "} " + type + ", " + text);
                });


                LogEvent2 log2 = new LogEvent2(delegate(Object sender, PluginLogType type, Int64 entityId, Int64 identityId, String text, String additionalData)
                {
                    logProxy.AddLog(LogKey.Plugin_Event, "Proxy", resource_plugin, resource.ToString(), ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri, (UserLogLevel)((Int32)type), entityId, identityId, text, additionalData);


#if DEBUG
                    TextLog.Log("PluginStarter", "{" + ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri + "} Type: " + type + ", Entity Id: " + entityId + ", Identity Id: " + identityId + ", Data: " + text + additionalData);
#endif
                });


                plugin.ImportPackageUser   += newPackage;
                plugin.ImportPackageStruct += newStructPackage;
                plugin.Log  += log;
                plugin.Log2 += log2;

                plugin.ProcessImport(resource_plugin.ToString(), id, connectorConf, mapping);

                plugin.ImportPackageUser   -= newPackage;
                plugin.ImportPackageStruct -= newStructPackage;
                plugin.Log  -= log;
                plugin.Log2 -= log2;

                newPackage = null;
                log        = null;
                uri        = null;

                //Salva os registros remanescentes
                if (records.data.Count > 0)
                {
                    SaveToSend(records, id + "-user");
                }

                if (structRecords.data.Count > 0)
                {
                    SaveToSend(structRecords, id + "-struct");
                }

                if (records.data.Count > 0)
                {
                    SaveToSend(records, resource_plugin.ToString());
                }

                importLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Imported " + count + " items...");
                TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Imported " + count + " items...");
            }
            catch (Exception ex)
            {
                logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, 0, 0, "Erro on import thread: " + ex.Message, "");
                importLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Erro on import thread: " + ex.Message);
                throw ex;
            }
            finally
            {
                TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Finishing import thread");
                importLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Finishing import thread...");

                if (count > 0)
                {
                    logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Info, 0, 0, "Import executed", importLog.ToString());
                }
                else
                {
#if DEBUG
                    //Mesmo log anterior, porém para mostrar quando estiver em debug
                    logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Info, 0, 0, "Import executed", importLog.ToString());
#endif
                }

                importLog.Clear();
                importLog = null;


                //Salva os logs para envio
                logProxy.SaveToSend(resource_plugin.ToString() + "log");
            }
        }
Exemplo n.º 2
0
        private void ProcessDeploy(Int64 resource, Int64 resource_plugin, Dictionary <String, Object> connectorConf, List <PluginConnectorBaseDeployPackageMapping> mapping)
        {
            StringBuilder deployLog = new StringBuilder();

            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Starting deploy thread...");
            deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Starting deploy thread...");

            JsonGeneric notify = new JsonGeneric();

            notify.function = "notify";
            notify.fields   = new String[] { "source", "resource", "uri", "entityid", "identityid" };

            JsonGeneric deleted = new JsonGeneric();

            deleted.function = "deleted";
            deleted.fields   = new String[] { "source", "resource", "uri", "entityid", "identityid" };

            JsonGeneric records = new JsonGeneric();

            records.function = "ProcessImportV2";
            records.fields   = new String[] { "resource_plugin", "package" };

            ImportPackageUserEvent newPackage = new ImportPackageUserEvent(delegate(PluginConnectorBaseImportPackageUser pkg)
            {
                records.data.Add(new String[] { resource_plugin.ToString(), JSON.SerializeToBase64(pkg) });

                try
                {
                    SaveToSend(records, resource_plugin.ToString());
                    records.data.Clear();
                }
                catch { }

                deployLog.AppendLine("Package generated from resource plugin " + resource_plugin.ToString() + ". ID: " + pkg.pkgId);

                TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "}>ProcessDeploy> Package generated from resource plugin " + resource_plugin.ToString() + ". ID: " + pkg.pkgId);
            });


            List <FileInfo> files = null;

            try
            {
                System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(plugin.GetType());
                DirectoryInfo dirFrom          = new DirectoryInfo(Path.Combine(basePath, "In\\" + Path.GetFileNameWithoutExtension(asm.Location) + "\\rp" + resource_plugin));
                if (!dirFrom.Exists)//Diretório inexistente
                {
                    deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Path not found " + dirFrom.FullName);
                    return;
                }

                //Ordena os arquivos, do mais antigo para o mais novo
                sortOndate sod = new sortOndate();
                files = new List <FileInfo>();
                files.AddRange(dirFrom.GetFiles("*.iamdat"));
                files.Sort(sod);

                foreach (FileInfo f in files)
                {
                    List <PluginConnectorBaseDeployPackage> fData = null;
                    try
                    {
                        deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Loading file " + f.Name);

                        try
                        {
                            fData = LoadFile(f);
                        }
                        catch (Exception ex)
                        {
                            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Error reading file " + f.FullName.Replace(basePath, "") + ", " + ex.Message);
                            logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, 0, 0, "Error reading file " + f.FullName.Replace(basePath, "") + ", " + ex.Message, "");
                        }

                        if (fData == null)
                        {
                            continue;
                        }

                        if (fData.Count == 0)
                        {
                            throw new Exception("Package is empty");
                        }

                        TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} [" + resource_plugin + "]" + fData.Count + " packages in " + f.Name);
                        deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + fData.Count + " packages in " + f.Name);

                        LogEvent log = new LogEvent(delegate(Object sender, PluginLogType type, String text)
                        {
                            TextLog.Log("PluginStarter", "{" + ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri + "}>Log> " + type + ", " + text);
                        });

                        LogEvent2 log2 = new LogEvent2(delegate(Object sender, PluginLogType type, Int64 entityId, Int64 identityId, String text, String additionalData)
                        {
                            logProxy.AddLog(LogKey.Plugin_Event, "Proxy", resource_plugin, resource.ToString(), ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri, (UserLogLevel)((Int32)type), entityId, identityId, text, additionalData);
#if DEBUG
                            TextLog.Log("PluginStarter", "{" + ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri + "}>Log2> " + (((UserLogLevel)((Int32)type)).ToString()) + " entityId = " + entityId + ", identityId = " + identityId + ", " + text + additionalData);
#endif
                        });

                        NotityChangeUserEvent log3 = new NotityChangeUserEvent(delegate(Object sender, Int64 entityId, Int64 identityId)
                        {
                            notify.data.Add(new String[] { "Proxy", resource.ToString(), ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri, entityId.ToString(), identityId.ToString() });
                        });

                        NotityChangeUserEvent log4 = new NotityChangeUserEvent(delegate(Object sender, Int64 entityId, Int64 identityId)
                        {
                            deleted.data.Add(new String[] { "Proxy", resource.ToString(), ((PluginConnectorBase)sender).GetPluginId().AbsoluteUri, entityId.ToString(), identityId.ToString() });
                        });

                        plugin.ImportPackageUser += newPackage;
                        plugin.Log               += log;
                        plugin.Log2              += log2;
                        plugin.NotityChangeUser  += log3;
                        plugin.NotityDeletedUser += log4;

                        //Somente realiza a importação após o deploy se for o Deploy Only, ou seja, a publicação sobre demanda de um usuário estecífico
                        Boolean doImportAfterLogin = (fData.Count <= 5);

                        try
                        {
                            foreach (PluginConnectorBaseDeployPackage pkg in fData)
                            {
                                try
                                {
                                    deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] EntityId = " + pkg.entityId + ", IdentityId = " + pkg.identityId + ", Pkg id: " + pkg.pkgId + ", user deleted? " + pkg.deleted);

                                    if (pkg.deleted)
                                    {
                                        plugin.ProcessDelete(resource_plugin.ToString(), pkg, connectorConf, mapping);
                                    }
                                    else
                                    {
                                        plugin.ProcessDeploy(resource_plugin.ToString(), pkg, connectorConf, mapping);
                                        if (doImportAfterLogin)
                                        {
#if DEBUG
                                            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} [" + resource_plugin + "] Doing import after deploy");
#endif

                                            plugin.ProcessImportAfterDeploy(resource_plugin.ToString(), pkg, connectorConf, mapping);
#if DEBUG
                                            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} [" + resource_plugin + "] Finihing import after deploy");
#endif
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} [" + resource_plugin + "] EntityId = " + pkg.entityId + ", IdentityId = " + pkg.identityId + ",  Error on ProcessDeploy thread of file " + f.FullName.Replace(basePath, "") + ", " + ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : ""));

                                    logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, pkg.entityId, pkg.identityId, "error on ProcessDeploy thread of file " + f.FullName.Replace(basePath, "") + ", " + ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : ""), "");
                                    deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] EntityId = " + pkg.entityId + ", IdentityId = " + pkg.identityId + ",  Error on ProcessDeploy thread of file " + f.FullName.Replace(basePath, "") + ", " + ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : ""));
                                }
                            }
                        }
                        finally
                        {
                            plugin.Log               -= log;
                            plugin.Log2              -= log2;
                            plugin.NotityChangeUser  -= log3;
                            plugin.NotityDeletedUser -= log4;
                            plugin.ImportPackageUser -= newPackage;

                            log  = null;
                            log2 = null;
                            log3 = null;
                            log4 = null;
                        }

                        //Salva as notificações
                        if (notify.data.Count > 0)
                        {
                            SaveToSend(notify, resource_plugin.ToString() + "notify");
                        }

                        //Salva as exclusões
                        if (deleted.data.Count > 0)
                        {
                            SaveToSend(deleted, resource_plugin.ToString() + "deleted");
                        }

                        if (records.data.Count > 0)
                        {
                            SaveToSend(records, resource_plugin.ToString());
                        }


                        try
                        {
                            f.Delete();

                            try
                            {
                                if (dirFrom.GetFiles("*.iamdat").Length == 0)
                                {
                                    dirFrom.Delete();
                                }

                                if (dirFrom.Parent.GetFiles("*.iamdat").Length == 0)
                                {
                                    dirFrom.Parent.Delete();
                                }
                            }
                            catch { }
                        }
                        catch (Exception ex) {
                            deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Error deleting file " + f.FullName.Replace(basePath, "") + ", " + ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : ""));
                        }
                    }
                    catch (Exception ex)
                    {
                        logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, 0, 0, "Erro on deploy thread of file " + f.FullName.Replace(basePath, "") + ", " + ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : ""), "");
                        deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Error on deploy thread of file " + f.FullName.Replace(basePath, "") + ", " + ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : ""));
                    }
                    finally
                    {
                        if (fData != null)
                        {
                            foreach (PluginConnectorBaseDeployPackage p in fData)
                            {
                                p.Dispose();
                            }
                        }
                    }
                }

                files.Clear();
            }
            catch (Exception ex)
            {
                logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, 0, 0, "Erro on deploy thread: " + ex.Message, "");
                deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Erro on deploy thread: " + ex.Message);
                throw ex;
            }
            finally
            {
                //Salva as notificações
                if (notify.data.Count > 0)
                {
                    SaveToSend(notify, resource_plugin.ToString() + "notify");
                }

                TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Finishing deploy thread...");
                deployLog.AppendLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Finishing deploy thread...");

                if (files != null)
                {
                    files.Clear();
                }
                files = null;


                logProxy.AddLog(LogKey.Proxy_Event, "Proxy", resource_plugin, resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Info, 0, 0, "Deploy executed", deployLog.ToString());

                deployLog.Clear();
                deployLog = null;


                //Salva os logs para envio
                logProxy.SaveToSend(resource_plugin.ToString() + "log");


                newPackage = null;
            }
        }