 
 factory = require './webpage-factory'
 executor = require './executor'
+loader = require './loader'
 
 ###
 The client program communicate with server and pull tasks to execute

          , 1000
      task.args.client_ip = task.client_ip
      logger.debug "Loaded task: #{JSON.stringify task}"
      ConfigLoader.getConfig(task.config_name)
    ).then((config) ->
      context.config = config
      if config is null and context.task isnt null

endpoint = $args.endpoint
workers = []

TaskAgent = (->
  acl = {} # key: rule, value: due to
  submitted = 0
  me =
    getTask: (worker_id) ->
      _acl = []
      for k, v of acl
        if (new Date() - v) > 0
          delete acl[k]
          continue
        _acl.push k
      # post endpoint, args: { op: 'get', crawler: crawler_name, worker: worker_id, acl: acl.keys.join(' ') }
      loader.request(endpoint, JSON.stringify(
        op: 'get'
        crawler: crawler_name
        worker: worker_id
        acl: _acl.join(' ')
        v: $args.version
      )).then((data) ->
        task = helpers.parseConfig(data)
        return Promise.resolve(null)  if task is null
        task.args = helpers.parseConfig task.args  if typeof task.args is 'string'
        if !task.config_name
          return Promise.resolve(new Error("Got invalid task: #{JSON.stringify task}"))
        Promise.resolve(task)
      )

    submitResult: (worker_id, task_id, status, result) ->
      # post endpoint, args: { op: 'submit', crawler: crawler_name, worker: worker_id, task_id: task_id, status: status, result: JSON.stringify(result) }
      loader.request(endpoint, JSON.stringify(
        op: 'submit'
        crawler: crawler_name
        worker: worker_id
        task_id: task_id
        status: status
        result: JSON.stringify result
      )).then((data) ->
        Promise.resolve(helpers.parseConfig(data))
        # due to memory leak issues of phantomjs. Restart the client every 1,000 tasks.
        submitted++
        if submitted > 1000
          for worker in workers
            worker.stop()
      )
    block: (config_name, till) ->
      acl['-' + config_name] = till
      Promise.resolve(config_name)
