<%start = params[:start].to_i %>
<%failed = Resque::Failure.all(start, 20)%>
<% index = 0 %>
<% date_format = "%Y/%m/%d %T %z" %>

<h1>Failed Jobs</h1>
<%unless failed.empty?%>
<form method="POST" action="<%=u 'failed/clear'%>">
  <input type='submit' name='' value='Clear Failed Jobs' />
</form>
<form method="POST" action="<%=u 'failed/requeue/all'%>">
  <input type='submit' name='' value='Retry Failed Jobs' />
</form>
<%end%>

<p class='sub'>Showing <%=start%> to <%= start + 20 %> of <b><%= size = Resque::Failure.count %></b> jobs</p>

<ul class='failed'>
  <%for job in failed%>
    <% index += 1 %>
    <li>
      <dl>
        <% if job.nil? %>
        <dt>Error</dt>
        <dd>Job <%= index%> could not be parsed; perhaps it contains invalid JSON?</dd>
        <% else %>
        <dt>Worker</dt>
        <dd>
          <a href="<%= u(:workers, job['worker']) %>"><%= job['worker'].split(':')[0...2].join(':') %></a> on <b class='queue-tag'><%= job['queue'] %></b > at <b><span class="time"><%= Time.parse(job['failed_at']).strftime(date_format) %></span></b>
          <% if job['retried_at'] %>
            <div class='retried'>
              Retried <b><span class="time"><%= Time.parse(job['retried_at']).strftime(date_format) %></span></b>
              <a href="<%= u "failed/remove/#{start + index - 1}" %>" class="remove" rel="remove">Remove</a>
            </div>
          <% else %>
            <div class='controls'>
              <a href="<%= u "failed/requeue/#{start + index - 1}" %>" rel="retry">Retry</a>
              or
              <a href="<%= u "failed/remove/#{start + index - 1}" %>" rel="remove">Remove</a>
            </div>
          <% end %>
        </dd>
        <dt>Class</dt>
        <dd><code><%= job['payload'] ? job['payload']['class'] : 'nil' %></code></dd>
        <dt>Arguments</dt>
        <dd><pre><%=h job['payload'] ? show_args(job['payload']['args']) : 'nil' %></pre></dd>
        <dt>Exception</dt>
        <dd><code><%= job['exception'] %></code></dd>
        <dt>Error</dt>
        <dd class='error'>
          <% if job['backtrace'] %>
            <a href="#" class="backtrace"><%= h(job['error']) %></a>
            <pre style='display:none'><%=h job['backtrace'].join("\n") %></pre>
          <% else %>
            <%=h job['error'] %>
          <% end %>
        </dd>
        <% end %>
      </dl>
      <div class='r'>
      </div>
    </li>
  <%end%>
</ul>

<%= partial :next_more, :start => start, :size => size %>

