Пример #1
0
        // SCHEDULER
        // SCHEDULER
        // SCHEDULER

        /// <summary>
        /// Run a scheduler item.
        /// </summary>
        /// <param name="name"></param>
        private async Task RunItemAsync(string name)
        {
            using (SchedulerDataProvider schedDP = new SchedulerDataProvider()) {
                using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync($"{AreaRegistration.CurrentPackage.AreaName}_RunItem_{name}")) {
                    SchedulerItemData evnt = await schedDP.GetItemAsync(name);

                    if (evnt == null)
                    {
                        throw new Error(this.__ResStr("errItemNotFound", "Scheduler item '{0}' does not exist."), name);
                    }
                    if (evnt.RunOnce)
                    {
                        evnt.Enabled = true;
                    }
                    if (!evnt.Enabled)
                    {
                        throw new Error(this.__ResStr("errItemDisabled", "Scheduler item '{0}' is currently disabled and cannot be scheduled."), evnt.Name);
                    }
                    evnt.Next   = DateTime.UtcNow.AddSeconds(-1);
                    evnt.Errors = null;
                    UpdateStatusEnum status = await schedDP.UpdateItemAsync(evnt);

                    await lockObject.UnlockAsync();

                    if (status != UpdateStatusEnum.OK)
                    {
                        throw new Error(this.__ResStr("errItemUpdFail", "Scheduler item '{0}' couldn't be updated."), evnt.Name);
                    }

                    Dispatch();// run the scheduler now
                }
            }
        }
        // API
        // API
        // API

        public async Task <ExchangeRateData> GetItemAsync()
        {
            ConfigData config = await ConfigDataProvider.GetConfigAsync();

            string jsFileName = GetJSFileName();

            using (ILockObject lockObject = await FileSystem.FileSystemProvider.LockResourceAsync(jsFileName)) {
                ExchangeRateData data = await DataProvider.GetAsync(KEY);

                if (data != null && data.SaveTime.Add(config.RefreshInterval) < DateTime.UtcNow)
                {
                    data = null;
                }
                if (data != null && !await FileSystem.FileSystemProvider.FileExistsAsync(jsFileName))
                {
                    data = null;
                }
                if (data == null)
                {
                    data = await GetExchangeRatesAsync();
                }
                await lockObject.UnlockAsync();

                return(data);
            }
        }
        public async Task <bool> RemovePageDefinitionAsync(Guid pageGuid)
        {
            PageDefinition page;

            using (ILockObject lockObject = await LockDesignedPagesAsync()) {
                page = await LoadPageDefinitionAsync(pageGuid);

                if (page == null)
                {
                    return(false);
                }
                await Manager.StaticPageManager.RemovePageAsync(page.Url);

                await DataProvider.RemoveAsync(pageGuid);
                await RemoveCachedPageAsync(pageGuid);

                DesignedPagesDictionaryByUrl designedPagesUrl = await GetDesignedPagesWithoutLockAsync();
                await RemoveDesignedPageAsync(designedPagesUrl, page.Url);

                await lockObject.UnlockAsync();
            }
            await Auditing.AddAuditAsync($"{nameof(PageDefinitionDataProvider)}.{nameof(RemovePageDefinitionAsync)}", page.Url, page.PageGuid,
                                         "Remove Page",
                                         DataBefore : page,
                                         DataAfter : null,
                                         ExpensiveMultiInstance : true
                                         );

            return(true);
        }
        private async Task <PageDefinition> CreatePageDefinitionAsync(PageDefinition page)
        {
            using (ILockObject lockObject = await LockDesignedPagesAsync()) {
                PageDefinition.DesignedPage desPage = new PageDefinition.DesignedPage()
                {
                    PageGuid = page.PageGuid, Url = page.Url,
                };
                DesignedPagesDictionaryByUrl designedPagesByUrl = await GetDesignedPagesWithoutLockAsync();

                if (designedPagesByUrl.ContainsKey(desPage.Url.ToLower()))
                {
                    throw new Error(this.__ResStr("pageUrlErr", "A page with Url {0} already exists.", desPage.Url));
                }
                if (!await DataProvider.AddAsync(page))
                {
                    throw new Error(this.__ResStr("pageGuidErr", "A page with Guid {0} already exists.", desPage.PageGuid));
                }
                await SetCachedPageAsync(page);
                await AddDesignedPageAsync(designedPagesByUrl, page.Url.ToLower(), desPage);

                await lockObject.UnlockAsync();
            }
            await Auditing.AddAuditAsync($"{nameof(PageDefinitionDataProvider)}.{nameof(CreatePageDefinitionAsync)}", page.Url, page.PageGuid,
                                         "Create Page",
                                         DataBefore : null,
                                         DataAfter : page,
                                         ExpensiveMultiInstance : true
                                         );

            return(page);
        }
Пример #5
0
        public async Task <bool> AddItemsAsync(List <SearchData> list, string pageUrl, PageDefinition.PageSecurityType pageSecurity, string pageDescription, string pageSummary, DateTime pageCreated, DateTime?pageUpdated, DateTime searchStarted, string customData)
        {
            if (!IsUsable)
            {
                return(false);
            }
            bool status = false;

            using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync($"{AreaRegistration.CurrentPackage.AreaName}_{nameof(SearchDataProvider)}")) {
                if (pageUpdated != null && (DateTime)pageUpdated < pageCreated)
                {
                    pageCreated = (DateTime)pageUpdated;
                }
                using (SearchDataUrlDataProvider searchUrlDP = new SearchDataUrlDataProvider()) {
                    SearchDataUrl searchUrl = await searchUrlDP.GetItemByUrlAsync(pageUrl);

                    if (searchUrl == null)
                    {
                        searchUrl = new SearchDataUrl {
                            DatePageCreated = pageCreated,
                            DatePageUpdated = pageUpdated,
                            PageTitle       = pageDescription.Truncate(SearchDataUrl.MaxTitle),
                            PageUrl         = pageUrl,
                            PageSecurity    = pageSecurity,
                            PageSummary     = pageSummary.Truncate(SearchDataUrl.MaxSummary),
                            CustomData      = customData,
                        };
                        if (!await searchUrlDP.AddItemAsync(searchUrl))
                        {
                            throw new InternalError("Unexpected error adding SearchDataUrl for url {0}", pageUrl);
                        }
                    }
                    else
                    {
                        searchUrl.PageTitle       = pageDescription.Truncate(SearchDataUrl.MaxTitle);
                        searchUrl.PageSummary     = pageSummary.Truncate(SearchDataUrl.MaxSummary);
                        searchUrl.DatePageCreated = pageCreated;
                        searchUrl.DatePageUpdated = pageUpdated ?? pageCreated;
                        searchUrl.PageSecurity    = pageSecurity;
                        searchUrl.CustomData      = customData;
                        UpdateStatusEnum updStatus = await searchUrlDP.UpdateItemAsync(searchUrl);

                        if (updStatus != UpdateStatusEnum.OK)
                        {
                            throw new InternalError("Unexpected error updating SearchDataUrl for url {0} - {1}", pageUrl, updStatus);
                        }
                    }
                    foreach (SearchData data in list)
                    {
                        data.SearchDataUrlId = searchUrl.SearchDataUrlId;
                        data.DateAdded       = searchStarted;
                        await AddItemAsync(data);
                    }
                }
                status = true;
                await lockObject.UnlockAsync();
            }
            return(status);
        }
Пример #6
0
        private async Task MarkUpdatedAsync(int searchDataUrlId)
        {
            using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync($"{AreaRegistration.CurrentPackage.AreaName}_{nameof(SearchDataProvider)}")) {
                await DataProviderIOMode.MarkUpdatedAsync(searchDataUrlId);

                await lockObject.UnlockAsync();
            }
        }
Пример #7
0
        public override async Task FlushAsync()
        {
            using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync(LogFile)) {
                await FlushAsyncNoLock();

                await lockObject.UnlockAsync();
            }
        }
 public void WriteToLogFile(string category, Logging.LevelEnum level, int relStack, string text)
 {
     YetaWFManager.Syncify(async() => {  // Logging is sync by definition (this is only used for startup logging)
         using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync(LogFile)) {
             await FileSystem.FileSystemProvider.AppendAllTextAsync(LogFile, text + "\r\n");
             await lockObject.UnlockAsync();
         }
     });
 }
Пример #9
0
        public async Task <ActionResult> RemoveAll()
        {
            using (SearchDataProvider searchDP = new SearchDataProvider()) {
                using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync($"{AreaRegistration.CurrentPackage.AreaName}_{nameof(SearchDataProvider)}")) {
                    await searchDP.RemoveItemsAsync(null);/* ALL */

                    await lockObject.UnlockAsync();
                }
                return(Reload(null, Reload: ReloadEnum.ModuleParts));
            }
        }
        private async Task <List <Guid> > GetDesignedGuidsAsync()
        {
            using (PageDefinitionDataProvider pageDP = new PageDefinitionDataProvider()) {
                using (ILockObject lockObject = await pageDP.LockDesignedPagesAsync()) {
                    List <Guid> pages = (from p in await pageDP.GetDesignedPagesWithoutLockAsync() select p.Value.PageGuid).ToList();
                    await lockObject.UnlockAsync();

                    return(pages);
                }
            }
        }
            public async Task InitAsync()
            {
                using (ILockObject lockObject = await FileSystem.FileSystemProvider.LockResourceAsync(LogFile)) {
                    if (await FileSystem.FileSystemProvider.FileExistsAsync(LogFile))
                    {
                        await FileSystem.FileSystemProvider.DeleteFileAsync(LogFile);
                    }
                    await FileSystem.FileSystemProvider.CreateDirectoryAsync(Path.GetDirectoryName(LogFile));

                    await lockObject.UnlockAsync();
                }
            }
Пример #12
0
        public override async Task ClearAsync()
        {
            Package package = Package.GetPackageFromAssembly(GetType().Assembly);

            using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync(LogFile)) {
                try {
                    await FileSystem.FileSystemProvider.DeleteFileAsync(LogFile);
                } catch (Exception) { }
                await FileSystem.FileSystemProvider.CreateDirectoryAsync(Path.GetDirectoryName(LogFile));

                LogCache = new List <string>();
                await lockObject.UnlockAsync();
            }
        }
        public static async Task <RetrieveInitialInstallLogInfo> RetrieveInitialInstallLogAsync()
        {
            RetrieveInitialInstallLogInfo info = new RetrieveInitialInstallLogInfo();

            info.Ended = false;
            if (!SiteDefinition.INITIAL_INSTALL || SiteDefinition.INITIAL_INSTALL_ENDED)
            {
                info.Ended = true;
            }
            bool success = false;

            while (!success)
            {
                try {
                    // This is horrible, polling until the file is no longer in use.
                    // The problem is we can't use statics or some form of caching as this is called by multiple separate requests
                    // and the Package package itself is replaced while we're logging, so we just use a file to hold all data.
                    // unfortunately even the lockObject is lost when the Package package is replaced. Since this is only used
                    // during an initial install, it's not critical enough to make it perfect...
                    using (ILockObject lockObject = await FileSystem.FileSystemProvider.LockResourceAsync(LogFile)) {
                        if (await FileSystem.FileSystemProvider.FileExistsAsync(LogFile))
                        {
                            info.Lines = await FileSystem.FileSystemProvider.ReadAllLinesAsync(LogFile);

                            success = true;
                        }
                        await lockObject.UnlockAsync();
                    }
                } catch (Exception) {
                    if (YetaWFManager.IsSync())
                    {
                        Thread.Sleep(new TimeSpan(0, 0, 0, 0, 50));// wait a while - this is bad, only works because "other" instance has lock
                    }
                    else
                    {
                        await Task.Delay(new TimeSpan(0, 0, 0, 0, 50));// wait a while
                    }
                }
            }
            if (info.Lines.Count == 0)
            {
                return(info);
            }
            if (info.Lines.Last() == "+++DONE")
            {
                info.Ended = true;
            }
            return(info);
        }
Пример #14
0
        public async Task RemoveOldItemsAsync(DateTime searchStarted)
        {
            if (!IsUsable)
            {
                return;
            }
            using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync($"{AreaRegistration.CurrentPackage.AreaName}_{nameof(SearchDataProvider)}")) {
                List <DataProviderFilterInfo> filters = null;
                filters = DataProviderFilterInfo.Join(filters, new DataProviderFilterInfo {
                    Field = nameof(SearchData.DateAdded), Operator = "<", Value = searchStarted
                });
                await RemoveItemsAsync(filters);
                await RemoveUnusedUrlsAsync();

                await lockObject.UnlockAsync();
            }
        }
        public async Task ReplaceItemAsync(MenuInfo data)
        {
            MenuInfo origMenu = Auditing.Active ? await GetItemAsync(data.ModuleGuid) : null;

            using (ILockObject lockObject = await YetaWF.Core.IO.Module.LockModuleAsync(data.ModuleGuid)) {
                await RemoveItemAsync(data.ModuleGuid);
                await AddItemAsync(data);

                await lockObject.UnlockAsync();
            }
            // audit code doesn't really work with this nested structure so skip for now
            //await Auditing.AddAuditAsync($"{nameof(MenuInfoDataProvider)}.{nameof(ReplaceItemAsync)}", "Menu", data.ModuleGuid,
            //    "Replace Menu",
            //    DataBefore: origMenu,
            //    DataAfter: data,
            //    ExpensiveMultiInstance: true
            //);
        }
Пример #16
0
        public override void SaveMessage(LogRecord record)
        {
            string text = string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}({7})-{8}: {9},{10},{11},{12} - {13}:{14}",
                                        DateTime.Now /*Local Time*/, record.Category, record.SessionId, record.SiteIdentity, record.IPAddress, record.RequestedUrl, record.UserName, record.UserId, record.ReferrerUrl,
                                        record.ModuleName,
                                        record.Class,
                                        record.Method,
                                        record.Namespace,
                                        record.Level, record.Info);

            text = text.Replace("\n", "\r\n");

            YetaWFManager.Syncify(async() => {  // logging is sync by default
                using (ILockObject lockObject = await YetaWF.Core.IO.Caching.LockProvider.LockResourceAsync(LogFile)) {
                    LogCache.Add(text);
                    if (LogCache.Count >= MAXRECORDS)
                    {
                        await FlushAsyncNoLock();
                    }
                    await lockObject.UnlockAsync();
                }
            });
        }
        public async Task SavePageDefinitionAsync(PageDefinition page)
        {
            if (page.Temporary)
            {
                throw new InternalError("Page {0} is a temporary page and can't be saved", page.PageGuid);
            }

            PageDefinition oldPage = null;

            using (ILockObject lockObject = await LockDesignedPagesAsync()) {
                page.Updated = DateTime.UtcNow;
                CleanupUsersAndRoles(page);
                await SaveImagesAsync(page.PageGuid, page);

                oldPage = await LoadPageDefinitionAsync(page.PageGuid);

                if (oldPage == null)
                {
                    throw new Error(this.__ResStr("pageDeleted", "Page '{0}' has been deleted and can no longer be updated."), page.Url);
                }

                Guid newPage = await FindPageAsync(page.Url);

                if (newPage != Guid.Empty && newPage != page.PageGuid)
                {
                    throw new Error(this.__ResStr("pageRename", "A page with Url '{0}' already exists."), page.Url);
                }

                UpdateStatusEnum status = await DataProvider.UpdateAsync(page.PageGuid, page.PageGuid, page);

                switch (status)
                {
                case UpdateStatusEnum.OK:
                    break;

                case UpdateStatusEnum.NewKeyExists:
                    throw new InternalError("Unexpected UpdateStatusEnum.NewKeyExists in SavePageDefinition");

                case UpdateStatusEnum.RecordDeleted:
                    throw new InternalError("Unexpected UpdateStatusEnum.RecordDeleted in SavePageDefinition");
                }
                await SetCachedPageAsync(page);

                await Manager.StaticPageManager.RemovePageAsync(page.Url);

                if (newPage == Guid.Empty)
                {
                    DesignedPagesDictionaryByUrl designedPagesByUrl = await GetDesignedPagesWithoutLockAsync();

                    designedPagesByUrl.Remove(oldPage.Url.ToLower());

                    PageDefinition.DesignedPage desPage = new PageDefinition.DesignedPage()
                    {
                        PageGuid = page.PageGuid, Url = page.Url,
                    };
                    await AddDesignedPageAsync(designedPagesByUrl, page.Url.ToLower(), desPage);
                }
                await lockObject.UnlockAsync();
            }
            await Auditing.AddAuditAsync($"{nameof(PageDefinitionDataProvider)}.{nameof(SavePageDefinitionAsync)}", oldPage.Url, oldPage.PageGuid,
                                         "Save Page",
                                         DataBefore : oldPage,
                                         DataAfter : page,
                                         ExpensiveMultiInstance : true
                                         );
        }