Beispiel #1
0
        private void StartAgents(Dictionary <String, Object> connectorConf)
        {
            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Starting agent thread...");

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

                LogEvent2 log2 = new LogEvent2(delegate(Object sender, PluginLogType type, Int64 entityId, Int64 identityId, String text, String additionalData)
                {
#if DEBUG
                    TextLog.Log("PluginStarter", "{" + ((PluginBase)sender).GetPluginId().AbsoluteUri + "}>Log2> Type: " + type + ", Entity Id: " + entityId + ", Identity Id: " + identityId + ", Data: " + text + additionalData);
#endif

                    logProxy.AddLog(LogKey.Plugin_Event, "Proxy", 0, "0", ((PluginBase)sender).GetPluginId().AbsoluteUri, (UserLogLevel)((Int32)type), entityId, identityId, text, additionalData);
                    logProxy.SaveToSend("agentlog");
                });

                plugin.Log  += log;
                plugin.Log2 += log2;

                plugin.Start(connectorConf);
            }
            catch (Exception ex)
            {
                logProxy.AddLog(LogKey.Proxy_Event, "Proxy", 0, "", plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, 0, 0, "Erro on agent thread: " + ex.Message, "");
                throw ex;
            }
        }
Beispiel #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;
            }
        }
Beispiel #3
0
        static void ProcessDeploy(Int64 resource, Dictionary <String, Object> connectorConf, Dictionary <String, String> mapping)
        {
            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Starting deploy thread...");


            JsonGeneric notify = new JsonGeneric();

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


            try
            {
                System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(plugin.GetType());
                DirectoryInfo dirFrom          = new DirectoryInfo(Path.Combine(basePath, "In\\" + Path.GetFileNameWithoutExtension(asm.Location) + "\\" + resource));
                if (!dirFrom.Exists) //Diretório inexistente
                {
                    return;
                }

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

                foreach (FileInfo f in files)
                {
                    try
                    {
                        List <PluginBaseDeployPackage> fData = null;
                        try
                        {
                            fData = LoadFile(f);
                        }
                        catch (Exception ex)
                        {
                            TextLog.Log("PluginStarter", "{" + plugin.GetPluginId().AbsoluteUri + "} Error reading file " + f.FullName.Replace(basePath, "") + ", " + ex.Message);
                            logProxy.AddLog("Proxy", 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 + "]" + fData.Count + " packages in " + f.Name);

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

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

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

                        plugin.Log              += log;
                        plugin.Log2             += log2;
                        plugin.NotityChangeUser += log3;

                        try
                        {
                            foreach (PluginBaseDeployPackage pkg in fData)
                            {
                                try
                                {
                                    plugin.ProcessDeploy(pkg, connectorConf, mapping);
                                }
                                catch (Exception ex)
                                {
                                    logProxy.AddLog("Proxy", resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, pkg.entityId, 0, "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;

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


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

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

                        try
                        {
                            f.Delete();

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

                            if (dirFrom.Parent.GetFiles("*.iamdat").Length == 0)
                            {
                                dirFrom.Parent.Delete();
                            }
                        }
                        catch { }
                    }
                    catch (Exception ex)
                    {
                        logProxy.AddLog("Proxy", 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 : ""), "");
                    }
                }

                files.Clear();
            }
            catch (Exception ex)
            {
                logProxy.AddLog("Proxy", resource.ToString(), plugin.GetPluginId().AbsoluteUri, UserLogLevel.Error, 0, 0, "Erro on deploy thread: " + ex.Message, "");
                throw ex;
            }
            finally
            {
                //Salva as notificações
                if (notify.data.Count > 0)
                {
                    SaveToSend(notify, resource.ToString() + "notify");
                }

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

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