API - Version 2‎ > ‎

Writing Code for SimpleWorker




Writing Code

This section will explain how to develop client code to upload to this endpoint. The examples are in ruby, but would apply to any language. 

Let's start with an extremely simple example, let's say all I wanted to run was this:

puts "Hello World!"

That would be in a file called hello.rb.

Now let's get that running on SimpleWorker, so next I need to package it up.

Packaging Code

Code must be submitted as a zip file containing all of the necessary files to execute. 

# this method will create a zip file given an array of file names.
def package_code(files)
  fname = "package.zip"
  Zip::ZipFile.open(fname, 'w') do |f|
    files.each do |file|
      f.add(file, file)
    end
  end
  fname
end

zip_filename = package_code(['hello.rb'])

So now we have a zip file containing the code we want to execute on SimpleWorker.

Uploading Code

Now we need to upload this package to SimpleWorker so we can start using it for our tasks. For this, we use the Tasks Upload method of the API. In this example, I will use the ruby gem to make it easier.

@sw.upload_code('hello', zip_filename, 'hello.rb', :runtime=>'ruby')

This is a method that exists in the simple_worker gem already that takes a name ('hello'), the zip filename from the packaging step above, the name of the file to execute ('hello.rb') and the runtime to execute it with. 

Now that it's uploaded, we can queue up a job to use it.

Running a Job Using Your Code

Now we simply queue up a task that uses our 'hello' code.

@sw.queue('hello')



Putting it all Together


require 'simple_worker'
require 'yaml'

@config = YAML.load_file('config.yml')

SimpleWorker.configure do |config|
  config.token = @config['token']
  config.project_id = @config['project_id']
end

@sw = SimpleWorker.service
zip = @sw.package_code(Dir.glob('**/*'))
@sw.upload_code('hello', zip, 'hello.rb', :runtime=>'ruby')
response = @sw.queue('hello')
puts 'response: ' + response.inspect
status = @sw.wait_until_complete(response['tasks'][0]['id'])
p status
puts @sw.log(response['tasks'][0]['id'])

That's the very basics, but should give you an idea of how to get started. 



Loading the Task Data Payload

When your code is executed, it will be passed three program arguments:

Program Arguments

  • -id - The task id.
  • -payload - the filename containing the data payload for this particular task.
  • -d - the user writable directory that can be used while running your job.

Here is a ruby example of obtaining this information (this is from the simple_worker ruby gem):

task_data_file = nil
task_id = nil
ARGV.each do |arg|
  if arg == \"-d\"
    dirname = ARGV[i+1]
  end
  if arg == \"-id\"
    task_id = ARGV[i+1]
  end
  if arg == \"-payload\"
    task_data_file = ARGV[i+1]
  end
  i+=1
end 

# Now load in job data
require 'json'
job_data = JSON.load(File.open(task_data_file))






TODO: Hello #{name} example using data payload. 




Comments