<commit_before>class GenerateFlatPostJob < ApplicationJob
  queue_as :high

  def self.enqueue(post_id)
    # frequent tag check
    lock_key = lock_key(post_id)
    return if $redis.get(lock_key)
    $redis.set(lock_key, true)

    perform_later(post_id)
  end

  def perform(post_id)
    Rails.logger.info("[GenerateFlatPostJob] updating flat post for post #{post_id}")
    return unless (post = Post.find_by_id(post_id))

    lock_key = self.class.lock_key(post_id)

    begin
      replies = post.replies
        .select('replies.*, characters.name, characters.screenname, icons.keyword, icons.url, users.username')
        .joins(:user)
        .left_outer_joins(:character)
        .left_outer_joins(:icon)
        .ordered

      view = ActionView::Base.new(ActionController::Base.view_paths, {})
      view.extend ApplicationHelper
      content = view.render(partial: 'posts/generate_flat', locals: {replies: replies})

      flat_post = post.flat_post
      flat_post.content = content
      flat_post.save

      $redis.del(lock_key)
    rescue Exception => e<commit_msg>Use .save! on misc<commit_after>class GenerateFlatPostJob < ApplicationJob
  queue_as :high

  def self.enqueue(post_id)
    # frequent tag check
    lock_key = lock_key(post_id)
    return if $redis.get(lock_key)
    $redis.set(lock_key, true)

    perform_later(post_id)
  end

  def perform(post_id)
    Rails.logger.info("[GenerateFlatPostJob] updating flat post for post #{post_id}")
    return unless (post = Post.find_by_id(post_id))

    lock_key = self.class.lock_key(post_id)

    begin
      replies = post.replies
        .select('replies.*, characters.name, characters.screenname, icons.keyword, icons.url, users.username')
        .joins(:user)
        .left_outer_joins(:character)
        .left_outer_joins(:icon)
        .ordered

      view = ActionView::Base.new(ActionController::Base.view_paths, {})
      view.extend ApplicationHelper
      content = view.render(partial: 'posts/generate_flat', locals: {replies: replies})

      flat_post = post.flat_post
      flat_post.content = content
      flat_post.save!

      $redis.del(lock_key)
    rescue Exception => e
{'fraction_non_alphanumeric': 0.10829493087557604, 'fraction_numerical': 0.0, 'mean_word_length': 3.299009900990099, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1060449', 'n_tokens_mistral': 720, 'n_tokens_neox': 708, 'n_words': 134}