Beispiel #1
0
 protected async Task AddLog(TelegtoolsLog log)
 {
     using (var ctx = new TelegtoolsContext())
     {
         ctx.Logs.Add(log);
         await ctx.SaveChangesAsync();
     }
 }
Beispiel #2
0
 public async Task <TelegtoolsJob> GetJobAsync(long jobId, bool includeLogs = false)
 {
     using (var ctx = new TelegtoolsContext())
     {
         return(includeLogs ?
                await ctx.Jobs.Include(j => j.Logs).SingleAsync(j => j.Id == jobId)
             : await ctx.Jobs.SingleAsync(j => j.Id == jobId));
     }
 }
Beispiel #3
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);
        }
Beispiel #4
0
        public async Task SaveAsync(TelegramPost telegramPost)
        {
            if (telegramPost == null)
            {
                throw new ArgumentNullException(nameof(telegramPost));
            }

            using (var ctx = new TelegtoolsContext())
            {
                ctx.TelegramPosts.Add(telegramPost);
                await ctx.SaveChangesAsync();
            }
        }
Beispiel #5
0
        public void Save(TelegramPost telegramPost)
        {
            if (telegramPost == null)
            {
                throw new ArgumentNullException(nameof(telegramPost));
            }

            using (var ctx = new TelegtoolsContext())
            {
                ctx.TelegramPosts.Add(telegramPost);
                ctx.SaveChanges();
            }
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public override async Task <TelegtoolsJob> ExecuteJobAsync(long jobId)
        {
            var job = await GetJobAsync(jobId);

            if (job == null || job.Status != TelegtoolsJobStatus.Created)
            {
                throw new ArgumentException($"There is no job with id '{jobId}' in 'created' status.", nameof(jobId));
            }

            if (string.IsNullOrWhiteSpace(job.ChannelName))
            {
                throw new ArgumentException($"The job with id '{jobId}' has no 'Channel Name'!", nameof(jobId));
            }

            job.Status    = TelegtoolsJobStatus.InProcess;
            job.StartDate = DateTime.UtcNow;
            job           = await UpdateJob(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
                        {
                            await AddLog(logEx);
                        }
                        catch { /* ignored */ }
                    }
                }

                job.Status = TelegtoolsJobStatus.Done;
            }
            catch (Exception ex)
            {
                job.Status = TelegtoolsJobStatus.Interrupted;

                var logEx = new TelegtoolsLog(job.ChannelName, postId)
                {
                    TelegtoolsJobId = job.Id,
                    Level           = TraceLevel.Error,
                    Message         = ex.ToString()
                };

                try
                {
                    await AddLog(logEx);
                }
                catch { /* ignored */ }
            }
            finally
            {
                job.EndDate = DateTime.UtcNow;
                job         = await UpdateJob(job);
            }

            return(job);
        }
Beispiel #8
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
                }
            }
        }