Sha256: 238ae7f6d40410c79e013c195bee24e468fcdf0d245acafe1e2c5169c6c4be01

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

# Jobs

Jobs are Ruby classes that process [tasks](/guides/tasks) and look as follows:

```ruby
class DummyJob < Wayfarer::Base
  route { to :index }

  def index
  end
end
```

Here is how to enqueue a task for a URL:

```ruby
DummyJob.crawl("https://example.com")
```

This is the same as calling the Active Job API directly and passing a task
and a random batch:

```ruby
task = Wayfarer::Task.new("https://example.com", SecureRandom.uuid)
DummyJob.perform_later(task)
```

A batch can be specified with `::crawl`, too:

```ruby
DummyJob.crawl("https://example.com", batch: "my-batch")
```

## Current task

Jobs consume [tasks](../tasks) from a message queue. The currently processed
task is accessible like so:

```ruby
class DummyJob < Wayfarer::Base
  route { to :index }

  def index
    task.url   # => "https://example.com"
    task.batch # => "my-batch"
  end
end
```

## Current page

A task's URL contents get fetched into a [page](../pages) object if the task URL
matched a route:

```ruby
class DummyJob < Wayfarer::Base
  route { to :index, host: "example.com" }

  def index
    page.url         # => "https://example.com"
    page.body        # => "<html>..."
    page.status_code # => 200
    page.headers     # { "Content-Type" => ... }
  end
end
```

## URL parameters

Jobs can extract data from URLs with their router:

```ruby
class DummyJob < Wayfarer::Base
  route do
    path "/users/:id/profile"
  end

  def index
    params[:id] # => "42"
  end
end

DummyJob.crawl("https://example.com/users/42/profile")
```


## User agent

The HTTP client or automated browser that fetched the URL is available:

```ruby
Wayfarer.config.network.agent = :ferrum # Chrome DevTools Protocol

class DummyJob < Wayfarer::Base
  route { to :index }

  def index
    browser.save_screenshot("capture.png")
  end
end
```

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wayfarer-0.4.6 docs/guides/jobs.md
wayfarer-0.4.5 docs/guides/jobs.md
wayfarer-0.4.4 docs/guides/jobs.md