Пример #1
0
        protected async Task <TelegtoolsJob> UpdateJob(TelegtoolsJob job)
        {
            using (var ctx = new TelegtoolsContext())
            {
                ctx.Jobs.Attach(job);
                ctx.Entry(job).State = EntityState.Modified;
                await ctx.SaveChangesAsync();
            }

            return(job);
        }
Пример #2
0
        public async Task <TelegtoolsJob> CreateJobAsync(string channelName, int fromPostId, int toPostId)
        {
            var job = new TelegtoolsJob(channelName, fromPostId, toPostId);

            using (var ctx = new TelegtoolsContext())
            {
                //var existJob = await ctx.Jobs.Where(j => j.ChannelName == channelName)
                //    .AnyAsync(j => HangingStatuses.Contains(j.Status));

                //if (existJob)
                //    throw new Exception("There is already an unfinished job for this channel.");

                ctx.Jobs.Add(job);
                await ctx.SaveChangesAsync();
            }

            return(job);
        }
Пример #3
0
        /*
         * public static async Task GetPostsAndSaveAsync(
         *  TelegtoolsContext context,
         *  string channelName,
         *  int startPostId,
         *  int endPostId)
         * {
         *  if (chennelTool == null) throw new ArgumentNullException(nameof(chennelTool));
         *
         *  var job = new TelegtoolsJob(channelName);
         *  context.Jobs.Add(job);
         *  await context.SaveChangesAsync();
         *  int postId = -1;
         *
         *  try
         *  {
         *      for (postId = startPostId; postId <= endPostId; postId++)
         *      {
         *          var telegramPost = await chennelTool.GetPostAsync(channelName, postId);
         *          var log = new TelegtoolsLog(channelName, postId);
         *
         *          if (telegramPost == null)
         *          {
         *              log.Level = TraceLevel.Warning;
         *              log.Message = $"Could not read post {postId}.";
         *              job.Logs.Add(log);
         *          }
         *          else
         *          {
         *              context.TelegramPosts.Add(telegramPost);
         *              log.Level = TraceLevel.Info;
         *              log.Message = $"Post {postId} has been saved.";
         *              job.Logs.Add(log);
         *          }
         *
         *          try
         *          {
         *              await context.SaveChangesAsync();
         *          }
         *          catch (Exception ex)
         *          {
         *              var logEx = new TelegtoolsLog(channelName, postId);
         *              logEx.Level = TraceLevel.Error;
         *              logEx.Message = ex.ToString();
         *              job.Logs.Add(log);
         *          }
         *      }
         *  }
         *  catch (Exception ex)
         *  {
         *      var logEx = new TelegtoolsLog(channelName, postId);
         *      logEx.Level = TraceLevel.Error;
         *      logEx.Message = ex.ToString();
         *      job.Logs.Add(logEx);
         *  }
         *  finally
         *  {
         *      job.EndDate = DateTime.UtcNow;
         *      job.Status = TelegtoolsJobStatus.Done;
         *      await context.SaveChangesAsync();
         *  }
         * }
         * //*/

        public async Task GetPostsAndSaveAsync(TelegtoolsJob job)
        {
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }

            if (string.IsNullOrWhiteSpace(job.ChannelName))
            {
                throw new ArgumentException(nameof(job.ChannelName), nameof(job));
            }

            int postId = -1;

            try
            {
                for (postId = job.FromPostId; postId <= job.ToPostId; postId++)
                {
                    var telegramPost = await _channelTools.GetPostAsync(job.ChannelName, postId);

                    var log = new TelegtoolsLog(job.ChannelName, postId)
                    {
                        TelegtoolsJobId = job.Id
                    };

                    if (telegramPost == null)
                    {
                        log.Level   = TraceLevel.Warning;
                        log.Message = $"Could not read post {postId}.";
                    }
                    else
                    {
                        log.Level   = TraceLevel.Info;
                        log.Message = $"Post {postId} has been saved.";
                    }

                    try
                    {
                        using (var ctx = new TelegtoolsContext())
                        {
                            if (telegramPost != null)
                            {
                                ctx.TelegramPosts.Add(telegramPost);
                            }
                            ctx.Logs.Add(log);
                            await ctx.SaveChangesAsync();
                        }
                    }
                    catch (Exception ex)
                    {
                        var logEx = new TelegtoolsLog(job.ChannelName, postId)
                        {
                            TelegtoolsJobId = job.Id,
                            Level           = TraceLevel.Error,
                            Message         = ex.ToString()
                        };

                        try
                        {
                            using (var ctx = new TelegtoolsContext())
                            {
                                ctx.Logs.Add(logEx);
                                await ctx.SaveChangesAsync();
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var logEx = new TelegtoolsLog(job.ChannelName, postId)
                {
                    TelegtoolsJobId = job.Id,
                    Level           = TraceLevel.Error,
                    Message         = ex.ToString()
                };

                try
                {
                    using (var ctx = new TelegtoolsContext())
                    {
                        ctx.Logs.Add(logEx);
                        await ctx.SaveChangesAsync();
                    }
                }
                catch
                {
                    // ignored
                }
            }
            finally
            {
                job.EndDate = DateTime.UtcNow;
                job.Status  = TelegtoolsJobStatus.Done;

                try
                {
                    using (var ctx = new TelegtoolsContext())
                    {
                        ctx.Jobs.Attach(job);
                        await ctx.SaveChangesAsync();
                    }
                }
                catch
                {
                    // ignored
                }
            }
        }