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 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 #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
                }
            }
        }