Beispiel #1
0
from ndscheduler.corescheduler import job


class PythonJob(job.JobBase):
    @classmethod
    def meta_info(cls):
        return {
            'job_class_string':
            '%s.%s' % (cls.__module__, cls.__name__),
            'notes':
            ('This will run an executable script with a virtual environment interpretor. You can specify as many '
             'arguments as you want. This job will pass these arguments to the '
             'program in order.'),
            'arguments': [{
                'type': 'string',
                'description': 'Executable path'
            }],
            'example_arguments':
            '["/usr/local/my_program", "--file", "/tmp/abc", "--mode", "safe"]'
        }

    def run(self, *args, **kwargs):
        return {'returncode': call(args, **kwargs)}


if __name__ == "__main__":
    # You can easily test this job here
    job = PythonJob.create_test_instance()
    job.run('EMAIL_ENV=test', 'python', '-m', 'hr_recipients.email_manual')
Beispiel #2
0
            '%s.%s' % (cls.__module__, cls.__name__),
            'notes':
            'This will print a string in your shell. Check it out!',
            'arguments': [
                # argument1
                {
                    'type': 'string',
                    'description': 'First argument'
                },

                # argument2
                {
                    'type': 'string',
                    'description': 'Second argument'
                }
            ],
            'example_arguments':
            '["first argument AAA", "second argument BBB"]'
        }

    def run(self, argument1, argument2, *args, **kwargs):
        print('Hello from AwesomeJob! Argument1: %s, Argument2: %s' %
              (argument1, argument2))
        return [argument1, argument2]


if __name__ == "__main__":
    # You can easily test this job here
    job = AwesomeJob.create_test_instance()
    job.run(123, 456)
Beispiel #3
0
            'for APNS cert file path.',
            'arguments': [
                # APNS device token
                {
                    'token': 'string',
                    'description': 'Device token'
                },
                # APNS Title's Alert Text
                {
                    'alert': 'string',
                    'description': 'What do you want to send?'
                },
            ],
            'example_arguments': ('["da1232badh2", "Hello from scheduler"]')
        }

    def run(self, token, alert="Hello World", *args, **kwargs):
        print('Sending %s to %s' % (alert, token))

        cert_file = os.environ[
            'APNS_CERT_PATH'] or 'simple_scheduler/jobs/apns-cert.pem'
        apns = APNs(use_sandbox=False, cert_file=cert_file)
        # Send a notification
        payload = Payload(alert=alert, sound="default", badge=0)
        apns.gateway_server.send_notification(token, payload)


if __name__ == "__main__":
    job = APNSJob.create_test_instance()
    job.run('da1232badh2', 'Hello World')
Beispiel #4
0
        def timeout_callback():
            jobs_json = self.get_results()
            possible_states = ['pending', 'running', 'finished']
            job_state = next(
                (state for state in possible_states
                 for job in jobs_json[state] if job['id'] == jobid),
                'not found')
            return {
                'errored':
                True,
                'response':
                f'Timed out waiting for response from scrapy for job {jobid}. '
                f'Scrapyd job status = {job_state}'
            }

        if 'status' not in create_job_json or create_job_json['status'] != 'ok':
            raise Exception('scrapy job failed: ' + create_job_json)

        result = PubSub.subscribe(jobid,
                                  lambda result: result,
                                  timeout=TIMEOUT * 60,
                                  timeout_func=timeout_callback)
        if result['errored']:
            raise Exception('Failure during scrapy processing: ' + str(result))
        return result


if __name__ == "__main__":
    job = ScrapyJob.create_test_instance()
    job.run('history')
Beispiel #5
0
                },
                # Request Type
                {
                    'type':
                    'string',
                    'description':
                    'What request type do you want? '
                    '(currently supported: GET/DELETE)'
                },
            ],
            'example_arguments':
            ('["http://localhost:8888/api/v1/jobs", "GET"]'
             '["http://localhost:8888/api/v1/jobs/ba12e", "DELETE"]')
        }

    def run(self, url, request_type, *args, **kwargs):
        print('Calling GET on url: %s' % (url))

        session = requests.Session()
        result = session.request(request_type,
                                 url,
                                 timeout=self.TIMEOUT,
                                 headers=None,
                                 data=None)
        return result.text


if __name__ == "__main__":
    job = CurlJob.create_test_instance()
    job.run('http://localhost:888/api/v1/jobs')
Beispiel #6
0
class ShellJob(job.JobBase):
    @classmethod
    def meta_info(cls):
        return {
            'job_class_string':
            '%s.%s' % (cls.__module__, cls.__name__),
            'notes':
            ('This will run an executable program. You can specify as many '
             'arguments as you want. This job will pass these arguments to the '
             'program in order.'),
            'arguments': [{
                'type': 'string',
                'description': 'Executable path'
            }],
            'example_arguments':
            '["/usr/local/my_program", "--file", "/tmp/abc", "--mode", "safe"]'
        }

    def run(self, *args, **kwargs):
        process = Popen(args, stdout=subprocess.PIPE)
        out, err = process.communicate()
        if err is not None:
            raise OSError(f"Process returned {out.decode('utf-8')}")
        return {'result': 0}


if __name__ == "__main__":
    # You can easily test this job here
    job = ShellJob.create_test_instance()
    job.run('ls', '-l')
Beispiel #7
0
                'You have to set Environment variable SIMPLE_SCHEDULER_SLACK_URL first.'
            )
        else:
            session = requests.Session()
            adapter = requests.adapters.HTTPAdapter(
                max_retries=self.MAX_RETRIES)
            session.mount('http://', adapter)
            session.mount('https://', adapter)

            message += ' // `sent from %s`' % socket.gethostname()
            payload = {
                'channel': channel,
                'username': name,
                'text': message,
                'link_names': 1,
                "mrkdwn": 1,
                'icon_emoji': icon_emoji
            }
            session.request('POST',
                            url,
                            timeout=self.TIMEOUT,
                            headers={'content-type': 'application/json'},
                            data=json.dumps(payload))


if __name__ == "__main__":
    # You can easily test this job here
    job = SlackJob.create_test_instance()
    job.run('#slack-bot-test', 'ndscheduler', ':satisfied:',
            'Standup, team! @channel')
Beispiel #8
0
            ],
            'example_arguments': ('["http://localhost:8888/api/v1/jobs", "GET"]'
                                  '["http://localhost:8888/api/v1/jobs/ba12e", "DELETE"]')
        }

    def run(self, url, request_type, username, password, login_endpoint, data={}):
        print('Calling %s on url: %s' % (request_type, url))

        session = requests.Session()
        authorization = None
        if username is not None and password is not None:
            token = session.post(login_endpoint,
                                 json={'email': username, 'password': password}).content.decode()
            authorization = f'Bearer {token}'
        if request_type == 'POST':
            result = session.request(request_type, url,
                                     timeout=self.TIMEOUT,
                                     headers={'Content-Type': 'application/json',
                                              'Authorization': authorization},
                                     json=data)
            return result.text
        else:
            result = session.request(request_type, url, timeout=self.TIMEOUT,
                                     headers={'Authorization': authorization})
            return result.text


if __name__ == "__main__":
    job = CurlJob.create_test_instance()
    job.run('http://localhost:8888/api/v1/jobs', 'GET', {})